OnSelectedItemChanged event problems

Last post 12-19-2006 11:20 AM by haoest. 15 replies.

Sort Posts:

  • OnSelectedItemChanged event problems

    12-18-2006, 5:56 AM
    • Loading...
    • thedigit
    • Joined on 10-25-2006, 10:19 AM
    • Posts 103

    Hi,

     

    I'm using the above event on a dropdown list, and it doesnt ever seem to be firing.

     

    What I want is the page to perform a databinding on another dropdownlist when a selection is made in the current dropdownlist.

     

     

    1    <asp:DropDownList ID="DDL1" runat="server" OnSelectedIndexChanged="ChangeDDL" EnableViewState="true"></asp:DropDownList>
     
        protected void ChangeDDL(object sender, EventArgs e)
    {
    //Do other DDL Population functions }
      
     
     Help is very much appreciated
  • Re: OnSelectedItemChanged event problems

    12-18-2006, 9:11 AM
    • Loading...
    • stiletto
    • Joined on 07-10-2003, 4:42 AM
    • Louisville, KY
    • Posts 3,154
    From your code it appears that you're dynamically adding the items to your first DDL.  If this is the case, then you have to add the items during every postback.  If you're already doing that, then make sure your DataBind doesn't have any code that sets the selected item during the PostBack.
  • Re: OnSelectedItemChanged event problems

    12-18-2006, 9:23 AM
    • Loading...
    • thedigit
    • Joined on 10-25-2006, 10:19 AM
    • Posts 103

    Hi,

     

    I'm populating my ddls in my page load section. I've checked and I'm not setting the selected item.

     

    Still I am getting no post back when I select something in ddl1 

  • Re: OnSelectedItemChanged event problems

    12-18-2006, 9:39 AM
    • Loading...
    • stiletto
    • Joined on 07-10-2003, 4:42 AM
    • Louisville, KY
    • Posts 3,154
    Ok, then let's flip the logic.  Try adding the items only if it's not a PostBack.  We'll see if Viewstate persists the DDL items...
  • Re: OnSelectedItemChanged event problems

    12-18-2006, 9:45 AM
    • Loading...
    • thedigit
    • Joined on 10-25-2006, 10:19 AM
    • Posts 103

    Hrm,

    No luck I'm afraid, even with the populate dropdownlist method in:

     

     if (!Page.IsPostBack)
            {
             //populate method here
            }
      

     

  • Re: OnSelectedItemChanged event problems

    12-18-2006, 10:47 AM
    • Loading...
    • haoest
    • Joined on 10-25-2005, 4:20 PM
    • Posts 401

    Show relevent part of your .aspx file please?

    Or if the items are dynamically added, show your code please.

     

    Debugger is my best friend. (http://haoest.info)
  • Re: OnSelectedItemChanged event problems

    12-18-2006, 10:59 AM
    • Loading...
    • thedigit
    • Joined on 10-25-2006, 10:19 AM
    • Posts 103
     
    1        protected void Page_Load(object sender, EventArgs e)
    2        {
    3            if (!Page.IsPostBack)
    4            {
    5                PopulateVendorCombo();
    6                Vendors.DataBind();
    7                PopulateOtherCombo(1);   //Databinding for the second ddl takes place in here
    8            }
    9        }
    10   
    11   public void PopulateVendorCombo()
    12       {
    13   
    14           AppSettingsReader MeReader = new AppSettingsReader();
    15           string connectionString = ((string)(MeReader.GetValue("project1.ConnectionString", typeof(string))));
    16   
    17           System.Data.SqlClient.SqlConnection cnVend = new System.Data.SqlClient.SqlConnection(connectionString);
    18           System.Data.SqlClient.SqlCommand cmPop = new System.Data.SqlClient.SqlCommand("rbk_GetVendors", cnVend);
    19           System.Data.SqlClient.SqlDataReader drVend;
    20           try
    21           {
    22               cnVend.Open();
    23               drVend = cmPop.ExecuteReader(CommandBehavior.CloseConnection);
    24               
    25               DataSet hello= new DataSet();
    26   
    27               DataTable dt = new DataTable();
    28   
    29               hello.Tables.Add(dt);
    30               hello.Tables[0].Columns.Add("VendorDescription");
    31               hello.Tables[0].Columns.Add("VendorID");
    32   
    33               while (drVend.Read())
    34               {
    35                   ListItem vendor = new ListItem((string)drVend[1], Convert.ToString(drVend[0]));
    36                   
    37                   dt.Rows.Add(vendor);
    38               }
    39               drVend.Close();
    40               cnVend.Close();
    41               cnVend.Dispose();
    42               Vendors.DataSource=hello.Tables[0];
    43               Vendors.DataTextField = "VendorDescription";
    44               Vendors.DataValueField = "VendorID";
    45           }
    46           catch (Exception ex)
    47           {
    48   
    49           }
    50           finally
    51           {
    52               if (cnVend.State != ConnectionState.Closed)
    53               {
    54                   cnVend.Close();
    55               }
    56           }
    57       }
    58   
    59       protected void ChangeDDL(object sender, EventArgs e)
    60       {
    61           int x = 2;
    62           PopulateOtherCombo(x);
    63       }
    
     
  • Re: OnSelectedItemChanged event problems

    12-18-2006, 1:53 PM
    • Loading...
    • stiletto
    • Joined on 07-10-2003, 4:42 AM
    • Louisville, KY
    • Posts 3,154
    You're not going to have the changed index in the Page_Load event.  You should always get the original selected index there.
  • Re: OnSelectedItemChanged event problems

    12-18-2006, 3:20 PM
    Answer
    • Loading...
    • haoest
    • Joined on 10-25-2005, 4:20 PM
    • Posts 401

    I think the problem is here (line 35-37):

                       ListItem vendor = new ListItem((string)drVend[1], Convert.ToString(drVend[0]));
                       dt.Rows.Add(vendor);
    

     You added an array of size 1 into the Rows collection. The first dimension of your table is VendorDescription, which you later had as DataTextField of the drop down list. This caused the drop down list to have different VendorDescription as Text, but always "" (empty values) as value. When you do a postback with the same empty value, OnSelectedItemChanged event is not going to trigger because nothing was changed. To verify, open the rendered html code on browser side, find the <select> tag that was rendered by your drop down list, you will probably find that all the options have empty strings as values. If this is so, then I was right. And the solution is simply changing line 35-37 into something like this:

      

                       string text = (string)drVend[1];
                       string value = Convert.ToString(drVend[0]));
                       dt.Rows.Add( new object[]{text, value} ); // now you have a row of 2-dimensions
    
     

    Debugger is my best friend. (http://haoest.info)
  • Re: OnSelectedItemChanged event problems

    12-19-2006, 12:34 AM
    • Loading...
    • jessjing
    • Joined on 09-26-2006, 8:03 AM
    • Posts 872

    hi,

    try this

      protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ArrayList values = new ArrayList();
            values.Add("a");
            values.Add("b");
            values.Add("c");

            DropDownList2.DataSource = values;
            DropDownList2.DataBind();
        }

    btw set AutoPostBack=true

    <asp:DropDownList ID="DDL1" runat="server" OnSelectedIndexChanged="ChangeDDL" EnableViewState="true"  AutoPostBack="true"></asp:DropDownList>

     
        
  • Re: OnSelectedItemChanged event problems

    12-19-2006, 1:11 AM

    hi digit

    you've not set autopostback=true for first dropdown.

    one more thing, why are you looping through reader then adding values to datatable, you should directly fetch values in dataset. keep your code short and smart.

    thanks,

    satish.

    Kind Attn: If a reply to your post helped you, kindly mark it as Answered.
    __________________________________________________
    Please save Animals Help World Society For Protection Of Animals,
    Protect these speechless creatures of GOD
  • Re: OnSelectedItemChanged event problems

    12-19-2006, 8:35 AM
    • Loading...
    • thedigit
    • Joined on 10-25-2006, 10:19 AM
    • Posts 103
    Thanks guys, I'm understanding everything you're saying, however after setting up a couple of break points and debugging, I've noticed that the OnSelectedItemChanged event never fires, even with autopostback set to true. What happens is the page goes through it's load routine and misses that event..hence it looks the same as before, nothing is done.
  • Re: OnSelectedItemChanged event problems

    12-19-2006, 8:50 AM
    • Loading...
    • thedigit
    • Joined on 10-25-2006, 10:19 AM
    • Posts 103

    I have put the control on a seperate page with nothing else apart from the other stuff in the control

    And same as before I have an autopostback=true set. The following events are the only ones triggered.

     

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BuildDataGrid();
                PopulateVendorCombo();
                Vendors.DataBind();
            }
        }
    
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }
      
  • Re: OnSelectedItemChanged event problems

    12-19-2006, 10:19 AM
    • Loading...
    • haoest
    • Joined on 10-25-2005, 4:20 PM
    • Posts 401

    did you try the solution I gave earlier in this thread?

     

    Debugger is my best friend. (http://haoest.info)
  • Re: OnSelectedItemChanged event problems

    12-19-2006, 10:44 AM
    Answer
    • Loading...
    • thedigit
    • Joined on 10-25-2006, 10:19 AM
    • Posts 103

    Many apologies,

    Yes that did the trick- perfect! I'm not going to be off to see how to change that to an ajax component, as postback is annoying on this particular type of control.

    Thanks again. Time to learn about update panels :-)
     

Page 1 of 2 (16 items) 1 2 Next >