Trouble getting selected value from DropDownList when posted back


I have a DropDownList inside a Repeater with AutoPostBack=True. I need to get the selectedvalue of the DropDownList so that I can populate another DropDownList based on its value. I was trying to do this in the Page_Load method but the value is coming back as null. Here's what I was trying. What am I doing wrong?
Thanks 
<dropdownlist id="OfficeDDL" runat="server" autopostback="true"></dropdownlist>br />

            
 

            
 
 
if (Page.IsPostBack)
            { 

                DropDownList OfficeDDL = (DropDownList)EmployeeOfficeAddressRepeater.FindControl("OfficeDDL");

                string sValue = OfficeDDL.SelectedValue.ToString();

                Trace.Warn(sValue);

       
            }
 
1
rkeslar
2/4/2009 9:37:55 PM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 4 Replies
👁️‍🗨️ 2220 Views



It's to early for you to access the control that's inside a databaound control at Page_Load event because the Repeater is not bounded yet.. Use ItemDataBound event instead when referencing a control that resides within a Repeater something like:
  
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList1");
                if (ddl != null)
                {
                        //do something
                }
            }

} 
 

Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
-1
vinz
2/4/2009 10:17:52 PM

vinz:

It's to early for you to access the control that's inside a databaound control at Page_Load event because the Repeater is not bounded yet.. Use ItemDataBound event instead when referencing a control that resides within a Repeater something like:

  

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DropDownList ddl = (DropDownList)e.Item.FindControl(&quot;DropDownList1&quot;);
                if (ddl != null)
                {
                        //do something
                }
            }

} 

 


 
Well execution made it there and my dropdownlist wasnt null but when I tried to trace the selectedvalue I got an empty string.
 But even when the page loads the first time theres an empty string as the value. I need to pass this value to another method though and I cant figure out why it's and emptry string.
-1
rkeslar
2/4/2009 10:44:49 PM

Hi rkeslar,
The following is cascading DropDownList controls inside the gridview.
  • Populate each of the DropDownList controls.
    • Pre-select the appropriate value in each list to match what's currently in the data.
    • When the user selects a new value in the master dropdown, repopulate the detail dropdown with a new set of dependent values.
    • When the row is saved, read the values from the dropdown lists and use them to set the values of parameters that will be passed to the database query that updates the database.
    • Here is the link, please read it.

      http://www.mikepope.com/blog/displayblog.aspx?permalink=1708


      Sincerely,
      Hua Jun Li
      Please remember to mark the replies as answers if they help and unmark them if they provide no help.

            
          
1
Hua
2/9/2009 6:52:01 AM

 I wonder if when we declare one dropdownlist as another whether we inherit the properties like selectedValue as well.

Bind GridView to Dynamically created DataTable | Bind DataTable to DropDownList

My Blog
1
shahed
2/9/2009 7:27:17 AM
Reply: