How to handle onselectindexchanged method in UserControl(.ascx page)
-
Hi all, I have an usercontrol where i gave a radiobutton list.(rblTest) I am using this usercontrol in my aspx page.
RadioButtonList rblTest= (System.Web.UI.WebControls.RadioButtonList)myControl1.FindControl("rblTest");
I need to handle onselectindexchanged method of radiobutton list in aspx page. How to do that. Plz give ur suggestions. Thanks in advancecheers sangeet
-
Hi all, I have an usercontrol where i gave a radiobutton list.(rblTest) I am using this usercontrol in my aspx page.
RadioButtonList rblTest= (System.Web.UI.WebControls.RadioButtonList)myControl1.FindControl("rblTest");
I need to handle onselectindexchanged method of radiobutton list in aspx page. How to do that. Plz give ur suggestions. Thanks in advancecheers sangeet
To do that you must first declare in your "hosting" aspx page a procedure which will handle an event:
public void RadioButtonIndexChangedProc(object sender, System.EventArgs e) { RadioButtonList rbl = (RadioButtonList) sender; // do what you need with data. }
Next in the user control, in theSelectedIndexChange
event call that procedure:private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e) { // get e reference to page object WebForm1 page = (WebForm1) Page; // call method handling Index changing page.RadioButtonIndexChangedProc(sender, e); }
As you can see, you can access your hosting aspx page by usingPage
object, and by casting it to appropriate type (class of your hosting page, in my caseWebForm1
) you have access to all public class members. In case of reusing an usercontrol on diffrent web pages think about implementing one base class or implementing an Interface which will handleRadioButtonIndexChange
by every class which uses your usercontrol.-- Mariusz 'mAv' Wójcik master e-software engineer
-
To do that you must first declare in your "hosting" aspx page a procedure which will handle an event:
public void RadioButtonIndexChangedProc(object sender, System.EventArgs e) { RadioButtonList rbl = (RadioButtonList) sender; // do what you need with data. }
Next in the user control, in theSelectedIndexChange
event call that procedure:private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e) { // get e reference to page object WebForm1 page = (WebForm1) Page; // call method handling Index changing page.RadioButtonIndexChangedProc(sender, e); }
As you can see, you can access your hosting aspx page by usingPage
object, and by casting it to appropriate type (class of your hosting page, in my caseWebForm1
) you have access to all public class members. In case of reusing an usercontrol on diffrent web pages think about implementing one base class or implementing an Interface which will handleRadioButtonIndexChange
by every class which uses your usercontrol.-- Mariusz 'mAv' Wójcik master e-software engineer
OKi, in addition to my above answer, there is a solution which uses interface. First, we implement an Interface:
public interface IRadioButtonListEvent { void RadioButtonIndexChangedProc(object sender, System.EventArgs e); }
As you can see, our interface implements just one method, which will be called whenever selected item index changes. Now, in the User Control,RadioButtonList1_SelectedIndexChanged
should look like this:private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e) { IRadioButtonListEvent iPage = (IRadioButtonListEvent) Page; iPage.RadioButtonIndexChangedProc(sender, e); }
And finally, code for hosting web page. As i mentioned above, page must implement anIRadioButtonListEvent
interface.public class WebForm1 : System.Web.UI.Page, IRadioButtonListEvent ... public void RadioButtonIndexChangedProc(object sender, System.EventArgs e) { RadioButtonList rbl = (RadioButtonList) sender; // do whatever you need... }
-- Mariusz 'mAv' Wójcik master e-software engineer
-
To do that you must first declare in your "hosting" aspx page a procedure which will handle an event:
public void RadioButtonIndexChangedProc(object sender, System.EventArgs e) { RadioButtonList rbl = (RadioButtonList) sender; // do what you need with data. }
Next in the user control, in theSelectedIndexChange
event call that procedure:private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e) { // get e reference to page object WebForm1 page = (WebForm1) Page; // call method handling Index changing page.RadioButtonIndexChangedProc(sender, e); }
As you can see, you can access your hosting aspx page by usingPage
object, and by casting it to appropriate type (class of your hosting page, in my caseWebForm1
) you have access to all public class members. In case of reusing an usercontrol on diffrent web pages think about implementing one base class or implementing an Interface which will handleRadioButtonIndexChange
by every class which uses your usercontrol.-- Mariusz 'mAv' Wójcik master e-software engineer
Both of your solutions couple the user control and page together very strongly, which limits the flexiblity and reuse.
only two letters away from being an asset
-
Hi all, I have an usercontrol where i gave a radiobutton list.(rblTest) I am using this usercontrol in my aspx page.
RadioButtonList rblTest= (System.Web.UI.WebControls.RadioButtonList)myControl1.FindControl("rblTest");
I need to handle onselectindexchanged method of radiobutton list in aspx page. How to do that. Plz give ur suggestions. Thanks in advancecheers sangeet
Handle the onselectindexchanged event in your user control and create an event in your user control that any page can subscribe to that reflects the onselectindexchanged event. A technique called event bubbling. http://www.odetocode.com/Articles/94.aspx[^]
only two letters away from being an asset