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 event in two user control dropdown list.

How to handle event in two user control dropdown list.

Scheduled Pinned Locked Moved ASP.NET
questioncsharpwinformslinqdesign
12 Posts 5 Posters 0 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 Rajesh_K_Sharma

    Dear friends, I am trying to create two dropdown list user controls and these are dependent on each other like state is dependent on country. When I select county then I am unable to fill state dropdown. How can I fill state dropdown on selection of country? I have followed steps like this Country.ascx:- ]]> Code behind:- using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Country : System.Web.UI.UserControl { public event EventHandler SelectedIndexChanged; public string SelectedValue { get { return Convert.ToString(ViewState["Value"]); } set { ViewState["Value"] = value; } } public bool AutoPostBack { get { return Convert.ToBoolean(ViewState["AutopostBack"]); } set { ViewState["AutopostBack"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ddlCountry.Items.Insert(0, new ListItem ( "India","0")); ddlCountry.Items.Insert(1, new ListItem ("South Korea","1")); } } protected override void OnInit(EventArgs e) { base.OnInit(e); ddlCountry.AutoPostBack = true; ddlCountry.SelectedIndexChanged += new EventHandler(ddlCountry_SelectedIndexChanged); } void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) { OnSelectedIndexChanged(e); } protected void OnSelectedIndexChanged(EventArgs e) { if (SelectedIndexChanged != null) { SelectedValue = ddlCountry.SelectedValue.ToString(); SelectedIndexChanged(this, e); } } } State.ascx public partial class State : System.Web.UI.UserControl { public string CountrySelectedValue { get { return Convert.ToString(ViewState["Id"]); } set { ViewState["Id"] = value; } } protected void Page_Load(object sender, EventArgs e) { switch (CountrySelectedValue) { case "0": ddlState.Items.Add("Del

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

    Is there any reason why the dropdowns are separate user controls? Doesn't really seem necessary, IMO, and is adding to your complexity. Expose and event on the state control to be called from the country control so your sate dropdown can be populated.


    only two letters away from being an asset

    1 Reply Last reply
    0
    • L Lost User

      I suggest you to make one user control for Country/state selection In that case you can accomlpish the task as

      void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
      {
      switch (ddlCountry.SelectedValue)
      {
      case "0":
      ddlState.Items.Add("Delhi");
      ddlState.Items.Add("MP");
      break;
      case "1":
      ddlState.Items.Add("Sunae");
      ddlState.Items.Add("Seoul");
      break;
      default:
      ddlState.Items.Add("Select");
      break;
      }
      }

      In case you want to continue in the same manner as you are doing then try out the following 1. Create a public variable in state.ascx.cs as public int CountryId = 0; 2. In your function void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) { //Add lines State objState = (State)FindControl("State1"); objState.intCountryId = ddlCountry.selectedvalue } 3. In Page load event of state.ascx, check the value of intCountryid and fill the dropdown accordingly.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #4
      1. Not a very good design to expose member variables directly 2) Not a good design either as you are tying these two controls together, one can't used without the other. A decoupled, and more extensible and reliable design for this situation would use events to communicate between the two user controls.

      only two letters away from being an asset

      C 1 Reply Last reply
      0
      • N Not Active
        1. Not a very good design to expose member variables directly 2) Not a good design either as you are tying these two controls together, one can't used without the other. A decoupled, and more extensible and reliable design for this situation would use events to communicate between the two user controls.

        only two letters away from being an asset

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #5

        Also not a good design b/c the items being placed in the list are hard coded instead of coming from a data source. In short, yet another terrible answer given on the ASP.NET forums :(

        Christian Graus Driven to the arms of OSX by Vista.

        L 1 Reply Last reply
        0
        • C Christian Graus

          Also not a good design b/c the items being placed in the list are hard coded instead of coming from a data source. In short, yet another terrible answer given on the ASP.NET forums :(

          Christian Graus Driven to the arms of OSX by Vista.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          i am giving him a example, and examples are not supposed to be bind to datasource. Now it is to the user who wants to use it and how. He may choose database or may hardcode. I just picked lines of code from his original source

          R N 2 Replies Last reply
          0
          • L Lost User

            i am giving him a example, and examples are not supposed to be bind to datasource. Now it is to the user who wants to use it and how. He may choose database or may hardcode. I just picked lines of code from his original source

            R Offline
            R Offline
            Rajesh_K_Sharma
            wrote on last edited by
            #7

            I am agree with Amandeep . I will bind dropdown by datasource but this is simple example. If my problem solve then I will implement though datasource.

            rajesh

            N 1 Reply Last reply
            0
            • L Lost User

              i am giving him a example, and examples are not supposed to be bind to datasource. Now it is to the user who wants to use it and how. He may choose database or may hardcode. I just picked lines of code from his original source

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

              No you are not giving an example, you are giving a snippet. An example would be fully functional.


              only two letters away from being an asset

              L 1 Reply Last reply
              0
              • R Rajesh_K_Sharma

                I am agree with Amandeep . I will bind dropdown by datasource but this is simple example. If my problem solve then I will implement though datasource.

                rajesh

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

                Your problem has nothing to do with the datasource and the "example" provided is a very poor example of how to design the solution.


                only two letters away from being an asset

                1 Reply Last reply
                0
                • N Not Active

                  No you are not giving an example, you are giving a snippet. An example would be fully functional.


                  only two letters away from being an asset

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  Mark Nischalke wrote:

                  No you are not giving an example, you are giving a snippet. An example would be fully functional.

                  Yes, that's a snippet. What you expect from snippet, should it implement a patterns, have complete level of security with properties defined. OR it should give a idea how you can solve a problem. Atleast i gave a solution(poor), rather than arguing and criticizing.

                  N 1 Reply Last reply
                  0
                  • L Lost User

                    Mark Nischalke wrote:

                    No you are not giving an example, you are giving a snippet. An example would be fully functional.

                    Yes, that's a snippet. What you expect from snippet, should it implement a patterns, have complete level of security with properties defined. OR it should give a idea how you can solve a problem. Atleast i gave a solution(poor), rather than arguing and criticizing.

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

                    Amandeep Singh Bhullar wrote:

                    What you expect from snippet,

                    That it be accurate and useful

                    Amandeep Singh Bhullar wrote:

                    Atleast i gave a solution(poor),

                    Check the posts, so did I, except mine was not so poorly designed.


                    only two letters away from being an asset

                    1 Reply Last reply
                    0
                    • R Rajesh_K_Sharma

                      Dear friends, I am trying to create two dropdown list user controls and these are dependent on each other like state is dependent on country. When I select county then I am unable to fill state dropdown. How can I fill state dropdown on selection of country? I have followed steps like this Country.ascx:- ]]> Code behind:- using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Country : System.Web.UI.UserControl { public event EventHandler SelectedIndexChanged; public string SelectedValue { get { return Convert.ToString(ViewState["Value"]); } set { ViewState["Value"] = value; } } public bool AutoPostBack { get { return Convert.ToBoolean(ViewState["AutopostBack"]); } set { ViewState["AutopostBack"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ddlCountry.Items.Insert(0, new ListItem ( "India","0")); ddlCountry.Items.Insert(1, new ListItem ("South Korea","1")); } } protected override void OnInit(EventArgs e) { base.OnInit(e); ddlCountry.AutoPostBack = true; ddlCountry.SelectedIndexChanged += new EventHandler(ddlCountry_SelectedIndexChanged); } void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) { OnSelectedIndexChanged(e); } protected void OnSelectedIndexChanged(EventArgs e) { if (SelectedIndexChanged != null) { SelectedValue = ddlCountry.SelectedValue.ToString(); SelectedIndexChanged(this, e); } } } State.ascx public partial class State : System.Web.UI.UserControl { public string CountrySelectedValue { get { return Convert.ToString(ViewState["Id"]); } set { ViewState["Id"] = value; } } protected void Page_Load(object sender, EventArgs e) { switch (CountrySelectedValue) { case "0": ddlState.Items.Add("Del

                      A Offline
                      A Offline
                      Anshumas
                      wrote on last edited by
                      #12

                      why dont you perform this opration using javascript and ajax1.1?

                      Anshuman Singh

                      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