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. Pass DropDownList selected value to Label - FindControl problem?

Pass DropDownList selected value to Label - FindControl problem?

Scheduled Pinned Locked Moved ASP.NET
helpcsharphtmlquestion
4 Posts 3 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.
  • F Offline
    F Offline
    FionaDM
    wrote on last edited by
    #1

    I want to pass the value of the selected item in a dropdownlist to a label. The dropdownlist is inside a FormView and the label is outside in the main form. I'm trying to do this by implementing an event handler for the event OnSelectedIndexChanged. I cannot get it to work as I get an error: Object reference not set to an instance of an object w.r.t code: Label1.Text = myDDL.SelectedItem.Value.ToString(); Here's the ASP/HTML

    ... .... .... .... .... < ...

    Here's the event handler (c#) protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList myDropDownList = (DropDownList)FindControl("DropDownList1"); Label1.Text = myDropDownList.SelectedItem.Value.ToString(); } My FindControl clearly isn't doing what I think it should. I can get this to work if the dropdownlist is not inside a FormView where there is no need to use FindControl. E.g. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Value.ToString(); } But unfortunately I NEED to have the dropdownlist inside a FormView. Thanks Majella

    P M 2 Replies Last reply
    0
    • F FionaDM

      I want to pass the value of the selected item in a dropdownlist to a label. The dropdownlist is inside a FormView and the label is outside in the main form. I'm trying to do this by implementing an event handler for the event OnSelectedIndexChanged. I cannot get it to work as I get an error: Object reference not set to an instance of an object w.r.t code: Label1.Text = myDDL.SelectedItem.Value.ToString(); Here's the ASP/HTML

      ... .... .... .... .... < ...

      Here's the event handler (c#) protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList myDropDownList = (DropDownList)FindControl("DropDownList1"); Label1.Text = myDropDownList.SelectedItem.Value.ToString(); } My FindControl clearly isn't doing what I think it should. I can get this to work if the dropdownlist is not inside a FormView where there is no need to use FindControl. E.g. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Value.ToString(); } But unfortunately I NEED to have the dropdownlist inside a FormView. Thanks Majella

      P Offline
      P Offline
      Paddy Boyd
      wrote on last edited by
      #2

      Your drop down list will be rendered on the client with a name like FormViewName_DropDownList1.

      F 1 Reply Last reply
      0
      • F FionaDM

        I want to pass the value of the selected item in a dropdownlist to a label. The dropdownlist is inside a FormView and the label is outside in the main form. I'm trying to do this by implementing an event handler for the event OnSelectedIndexChanged. I cannot get it to work as I get an error: Object reference not set to an instance of an object w.r.t code: Label1.Text = myDDL.SelectedItem.Value.ToString(); Here's the ASP/HTML

        ... .... .... .... .... < ...

        Here's the event handler (c#) protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList myDropDownList = (DropDownList)FindControl("DropDownList1"); Label1.Text = myDropDownList.SelectedItem.Value.ToString(); } My FindControl clearly isn't doing what I think it should. I can get this to work if the dropdownlist is not inside a FormView where there is no need to use FindControl. E.g. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Value.ToString(); } But unfortunately I NEED to have the dropdownlist inside a FormView. Thanks Majella

        M Offline
        M Offline
        Mike Ellison
        wrote on last edited by
        #3

        How about something like this?

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
        DropDownList myDropDownList = (sender as DropDownList);
        if (myDropDownList != null)
        Label1.Text = myDropDownList.SelectedItem.Value.ToString();
        }

        1 Reply Last reply
        0
        • P Paddy Boyd

          Your drop down list will be rendered on the client with a name like FormViewName_DropDownList1.

          F Offline
          F Offline
          FionaDM
          wrote on last edited by
          #4

          Thanks - I had omitted to specify the container (FormView1). Was DropDownList myDropDownList = (DropDownList)FindControl("DropDownList1"); but should have been DropDownList myDropDownList = (DropDownList)FormView1.FindControl("DropDownList1"); Many thanks Majella

          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