How do I reference a 'selection' made within a placeholder.control.dropdownlist?

Last post 09-02-2008 8:34 PM by Matt-dot-net. 7 replies.

Sort Posts:

  • How do I reference a 'selection' made within a placeholder.control.dropdownlist?

    08-29-2008, 10:02 AM
    • Member
      90 point Member
    • MSUTech
    • Member since 04-11-2007, 2:32 PM
    • Posts 176

    Within my aspx.cs file I create the following dropdownlist within a placeholder:

     /** create the dropdownlist for this specific time through the loop **/

    DropDownList ddlSomeList = new DropDownList();

    ddlSomeList.ID = "ddlSomeList";

    /** add each control to the placeholder **/

    placeholderexpertise.Controls.Add(ddlSomeList);

     

    then during the following update statement, I want to reference what value the user has selected, I am trying the following, but, it does not work:

    strUpdateQuery1 = "UPDATE ThePersonUPLOAD SET date_modified=GetDate(),expertise=@expertise WHERE person_id=@person_id";

    SqlCommand UpdateReviewerCommand = new SqlCommand(strUpdateQuery1, CurrentConnection);

    UpdateReviewerCommand.Parameters.AddWithValue("@expertise", placeholderexpertise.ddlSomeList.SelectedValue);

  • Re: How do I reference a 'selection' made within a placeholder.control.dropdownlist?

    08-29-2008, 10:12 AM
    • Contributor
      4,008 point Contributor
    • Matt-dot-net
    • Member since 06-09-2007, 12:00 PM
    • Kentucky
    • Posts 772

    You can get the dropdownlist by using (DropDownList)placeholderexpertise.FindControl("ddlSomeList")

    Or you could make ddlSomeList a private member of your page class and you will have a reference to it in any event/method of your page.

  • Re: How do I reference a 'selection' made within a placeholder.control.dropdownlist?

    08-29-2008, 10:23 AM
    • Member
      90 point Member
    • MSUTech
    • Member since 04-11-2007, 2:32 PM
    • Posts 176

    How can I then reference the SelectedValue?

  • Re: How do I reference a 'selection' made within a placeholder.control.dropdownlist?

    08-29-2008, 11:09 AM
    • Member
      90 point Member
    • MSUTech
    • Member since 04-11-2007, 2:32 PM
    • Posts 176

    I am using the following code:

    DropDownList ddlexpNOW = (DropDownList)placeholderexpertise.FindControl("ddlSomeList");

    lblexp.Text = ddlexpNOW.SelectedValue;

    which gives me the following error:

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

  • Re: How do I reference a 'selection' made within a placeholder.control.dropdownlist?

    08-30-2008, 10:17 AM
    • Contributor
      4,008 point Contributor
    • Matt-dot-net
    • Member since 06-09-2007, 12:00 PM
    • Kentucky
    • Posts 772

    That means that FindControl was unable to find your control.  Are you really using "ddlSomeList?"   Replace this string with the ID you have given your drop down list.  I am guessing it is something like "ddlexpNOW."

     

  • Re: How do I reference a 'selection' made within a placeholder.control.dropdownlist?

    09-01-2008, 11:23 PM
    Answer

    Hi MSUTech,

    You should check your code.

    (1)You should put the following code inside Page_Load function.

        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList ddlSomeList = new DropDownList();

            ddlSomeList.ID = "ddlSomeList";

            /** add each control to the placeholder **/

            placeholderexpertise.Controls.Add(ddlSomeList);

        }

    (2)You should check your control id is right.

    DropDownList ddlexpNOW = (DropDownList)placeholderexpertise.FindControl("controIID");

    Or use the following method.

    lblexp.Text =Request.Form["ddlSomeList"].ToString();

     

     

    Sincerely,
    Hua Jun Li
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: How do I reference a 'selection' made within a placeholder.control.dropdownlist?

    09-02-2008, 1:54 PM
    • Member
      90 point Member
    • MSUTech
    • Member since 04-11-2007, 2:32 PM
    • Posts 176

    Thanks..... I have a question:

     

    THIS WORKS:

    lblexp.Text = Request.Form["ASPHELPddlSomeList"].ToString();

     

    But, the FindControl does NOT work, any thoughts on WHY?

    DropDownList ddlexpNOW = (DropDownList)placeholderexpertise.FindControl("ASPHELPddlSomeList");

    lblexp.Text = ddlexpNOW.SelectedValue;

     

    I still get this error:

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

  • Re: How do I reference a 'selection' made within a placeholder.control.dropdownlist?

    09-02-2008, 8:34 PM
    Answer
    • Contributor
      4,008 point Contributor
    • Matt-dot-net
    • Member since 06-09-2007, 12:00 PM
    • Kentucky
    • Posts 772

    FindControl is not recursive meaning that it will not find controls buried on the page inside a particular parent control at some arbitrary depth.  This is a performance consideration.  Therefore, FindControl will only find controls within the "naming container" or containing control.  So in your example, i can infer that placeholderexpertise is not the naming container control for ASPHELPddlSomeList.  You most surely have some other container within placeholderexpertise in which ASPHELPddlSomeList lives.

    Filed under:
Page 1 of 1 (8 items)