Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to handle onselectindexchanged method in UserControl(.ascx page)

How to handle onselectindexchanged method in UserControl(.ascx page)

Scheduled Pinned Locked Moved ASP.NET
designtutorial
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ramyasangeet
    wrote on last edited by
    #1

    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 advance

    cheers sangeet

    M N 2 Replies Last reply
    0
    • R ramyasangeet

      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 advance

      cheers sangeet

      M Offline
      M Offline
      Mariusz Wojcik
      wrote on last edited by
      #2

      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 the SelectedIndexChange 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 using Page object, and by casting it to appropriate type (class of your hosting page, in my case WebForm1) 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 handle RadioButtonIndexChange by every class which uses your usercontrol.

      -- Mariusz 'mAv' Wójcik master e-software engineer

      M N 2 Replies Last reply
      0
      • M Mariusz Wojcik

        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 the SelectedIndexChange 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 using Page object, and by casting it to appropriate type (class of your hosting page, in my case WebForm1) 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 handle RadioButtonIndexChange by every class which uses your usercontrol.

        -- Mariusz 'mAv' Wójcik master e-software engineer

        M Offline
        M Offline
        Mariusz Wojcik
        wrote on last edited by
        #3

        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 an IRadioButtonListEvent 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

        1 Reply Last reply
        0
        • M Mariusz Wojcik

          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 the SelectedIndexChange 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 using Page object, and by casting it to appropriate type (class of your hosting page, in my case WebForm1) 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 handle RadioButtonIndexChange by every class which uses your usercontrol.

          -- Mariusz 'mAv' Wójcik master e-software engineer

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • R ramyasangeet

            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 advance

            cheers sangeet

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups