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 

<asp:DropDownList ID="OfficeDDL" runat="server" AutoPostBack="true" /><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. 3 followers. Follow

4 Replies
546 Views

Similar Articles

[PageSpeed] 45

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("DropDownList1");
                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:

    Similar Artilces:

    Getting the Selected Item of dropdownlist inside Web User Control on a Web Form
    Ive got a dropdownlist control inside a web user control called Entity.ascx. This web user control is on a webform.aspx. In Entity.ascx class I created a SelectedEntity property that returns the selected item in the dropdownlist. Inside webform.aspx when btnGo is pushed I set a string and pass it in the url. I am trying to get the entity they selected via the property (SelectedEntity) inside Entity.ascx. As the SelectedEntity property tries to return the selected item in the dropdownlist I get this error: System.NullReferenceException: Object reference not set to an instance...

    Dropdownlist not posting back selected values.
    My data base looks like this. My dropdownlist has the vacancy_num as the DataTextField and  description as DataValueField. If I select a vacancy_num and do a post back to place the 'description' name as a label using the DataValueField for the value, The vacancy_num ALWAYS selects the first vacancy_num in the sequence. Example: I choose B1000/2. Postback returns B1000/1. I choose B4000/3, postback returns B4000/1. Why does it not keep the value I selected?? I have noticed that if I replace the DataValueField to point to the vacancy_num it works fine. But then I don't have a descri...

    DropDownList value not post back in ajax form
    I encountered a bug with ajax form with DropDownList.The selected value in DropDownList can't be posted to web server.This bug only appears in IE (mine is 7.0.5730.11), but not in FF / chrome.  Here is the sample code // *************** CONTROLLER  ***************         public ActionResult Test(int? page)        {            return View();        } // *************** CONTROLLER  *...

    Getting a listbox to show the value selected from a posted form
    I have a form with a Listbox on it. How do I get the form returned from the post action with the selected data in the listbox. Also what is the difference between a listbox and a dropdownlistbox ? A Listbox shows more than one item, visibly, at a time - - - A DropDownList shows only one item - - you must click on it, so it will 'Drop Down' and show you other items in the list. Check out: http://aspnet101.com/aspnet101/aspnet/codesample.aspx?code=listboxmultiple David WierMCP/ASPInsiderASPNet101.com - where to look first!Please Vote for ASPNet101 - 'Best Community Resource...

    How to retain and get the selected value of the drop down upon post back
    Hi I have a form view. Inside the formview I have a drop down. I did not bind the drop down with the ODS. Instead I do it manually on the code behind. If the user selects a value I display a textbox beside the drop down and allow the user to enter the value. Upon onblur event of the textbox I do a postback and in the page load i check whethere the page has been posted. If so I add this value to the datatable and once again bind it to the combo box and set the selected value to the text box value.    If the user clicks the save button and when I look for the selected value it says...

    getting values from a web form to windows form
    Hot Requirement ..... Is there any possible of getting values from a web form to windows form ????? Yes One way i to use the WebRequest and WebResponse classes of the System.Net namespace. For example:  WebRequest request = WebRequest.Create("URL of the web page); WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string content = reader.ReadLine(); ....    Reza Nassabehwww.professionalcsharp.comDon't forget to click "Mark as Answer" on the post that helped you. That way fut...

    Posting to Web Form with DropDownList from another form
    I have a page with a drop down list that displays data on post back from the drop down list selected index change event. (call this form1) I need the ability to post to this page from another form (call this form2). there are about 6 options in form1. form2 will contain only 2 of these options displayed as simple hyperlinks. when the user clicks these links from form2 it needs to simply force a post back with that value from form2. id like to do this without javascript and id like to use the drop down list server control. anyone have a quick simple way to accomplish this? than...

    Form: Focus on selected values in dropdownlists, Refresh after selection and hidding formfields based on selection.
    Hello again,I have a form with several dropdown lists. Depending on the selected value in the dropdown lists other fields become available in that same form. It is working in classic ASP in combination of JavaScript. I want to migrate it to ASP.NET. I have posted a part of the form at the end of this post.My questions:1. I use <option <%If iFood="0" then%>selected<%End If%> value="0">No value selected</OPTION> to select the selected item when the form is reloaded after the selecting. I know that this also works in ASP.NET, but isn't there a typical ASP....

    How Select one dropdownlist value using down arrows corresponding other dropdownlist value are select
    I am using two dropdownlist box.One is used for country selection. other one is used for state selection.I am using dropdown selectedindexchanged event. when i click country dropdownlist corresponding state is selected in state dropdownlist box. How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.R.Karthikeyan   kartmca: How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.Generally, we don't implmenet...

    Dropdownlist selected value passes first value in list no matter what value is selected. why ?
     HI . I have a dropdownlist. Databinds ok but when I must pass a value as a Selected value it always passes the first value in the list even if I select another value. I tried to set the ddl to enableviewstate false but it doesn't change anything. Any ideas ? Code below, thanksLoading the ddl:       productCategories = CatalogAdmin.Admin_Product_Categories_NotIn(productId);        for (int i = 0; i < productCategories.Rows.Count; i++)        {       &...

    how to get SELECTED VALUE of parent dropdownlist to target dropdownlist using cascading
    Hi All, I am using C# , asp.net2.0 , Ajax1.0 i am filling dropdownlist using cascading ajax extender. i taken help of ajax site and build a code to fill the control. my question is how do i get parent selected item value. my web service code to fill target value public CascadingDropDownNameValue[] GetProd(string knownCategoryValues , string category) {StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //string subgrp ; //int sub1; //if (!kv.ContainsKey("Prod") || !Int32.TryParse(kv["Prod"], out sub1)) //// if (...

    How to take selected value of dropdownlist in web method using cascading dropdownlist
    Hi All,I am using C# asp.net 2.0 and ajax1.0.I am trying to fill dropdownlist using ajax cascading toolkit but I am not able to populate the dropdownlist.  My question is how do I take input value of parent dropdownlist in web service the fill the data that is in my senario I have subprod parent (fill on page load) and then Prod fill on subprod selection.  The web service method I used is  public CascadingDropDownNameValue[] GetProd(string knownCategoryValues, string category) { StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(known...

    Could PB .NET Web Form POST security Information to others web page??
    This is a multi-part message in MIME format. ------=_NextPart_000_00F9_01C9C4D5.8FB45C90 Content-Type: text/plain; charset="big5" Content-Transfer-Encoding: quoted-printable Hi,=20 Have anyone came across the requesion to POST security informations from = PB .NET web form web page to other web site?? I have tried many datys, Could any kind man give me some suggessions?? thanks & Best regards Leon ------=_NextPart_000_00F9_01C9C4D5.8FB45C90 Content-Type: text/html; charset="big5" Content-Transfer-Encoding: quoted-printable <!DOCTYPE HTML P...

    Getting DropDownlist's DataTextValue Selected Value Given the SelectedValue and/or Selected Item
    In the vb.net codebehind, I need to extract something from the Text (displayed) value of dropdownlist's selected item and I have  the SelectedValuand and SelectedItem in hand. How can I do this?  FindControl? Item? row?   Thanks. Well, its easier than you might think. SelectedItem has a "text" property. Thats the selected text right there :)  If you need the Text of the selection, then you have to use the SelectedItem.Text property.  If you want the value (not the displayed text) the you can directly use the SelectedValue. dim sText as String = dropdown...

    Web resources about - Trouble getting selected value from DropDownList when posted back - asp.net.web-forms

    Tag Synonyms - Stack Overflow
    Q&A for professional and enthusiast programmers

    Accidental entities - what about the UI?
    This post is a follow-up to my previous blog post " Accidental entities - you don't need that identity ". In that post, we followed a consultant ...

    Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
    Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

    Air Astana - Join Now
    Thank you for considering membership of Nomad Club – Kazakhstan’s first Frequent Flyer Program. You could soon be on your way to earning free ...

    Languages -> C# Articles, Tutorials, Examples: ASP Alliance
    .NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

    Blog - Avisra
    Avisra Web Development Home Blog Services Contact The Blog Latest Module Builder: How can we make it better? Sitefinity by Chris on July 2, 2013 ...

    Online Booking Tool Support
    This Online Booking Tool Support page might help you as you navigate through the process of booking travel through your OBT provided by CWT. ...

    @Html and beyond
    ... ); } return string .Format( “{0}{1}” , me.Member.Name, id); } The Html building methods are quite simple. Building a dropdownlist for a ...

    Top Ranked Articles
    Top Ranked Articles - Free source code and tutorials for Software developers and Architects.; Updated: 23 Feb 2013

    Unit Testing DateTime.Now Without Isolation
    public virtual void SeedWithYearsSince(DropDownList list, int year){ for (int index = year; index

    Resources last updated: 12/11/2015 9:27:14 PM