DropDownList and TextBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • E11esar
    New Member
    • Nov 2008
    • 132

    DropDownList and TextBox

    Hi there.

    In an Asp.Net (C#) page, I am assigning a

    TextBox.Text to DropDownList.Se lectedItem.Text

    and this all works fine within the SelectedIndexCh anged event of the dropDownList.

    The problem I am looking for some feedback is where I want the first (top-most) item from the DropDownList, please?

    I have tried a button_click event and a few other ideas but I can't see how to assign the top-most (position 0) value of the dropDownList to the TextBox. Even if I click the top item the event is not triggered so I am obviously missing something really simple here...

    Any help please?

    Thank you.

    M :o)
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    It is pretty simple.
    When you populate the DropDownList, and you're adding the first item to that list, add that item's text to the text box :)

    Comment

    • E11esar
      New Member
      • Nov 2008
      • 132

      #3
      Hi there and thank you. I am currently populating the DropDownList with a DataReader and setting the datasource (databind) in that way.

      I think my problem may be even simpler as I have just realised that the first value (index 0) is always being selected now, so I think my AutioPostBack is causing issues here as I am populating the DropDownList in the OnLoad event..!

      So I guess the question now is which event is better here please?

      Thank you.

      M :o)

      Comment

      • E11esar
        New Member
        • Nov 2008
        • 132

        #4
        One solution

        Hi there.

        I just found "one" way to make this work.

        I have kept this in the PageLoad event but then enclosed my current logic within a

        Code:
          if(!(Page.IsPostBack)){...}
        and then at the point after the DropDownList is filled, I have inserted a dummy line at index 0 with:

        Code:
           dropdDownList.Items.Insert(0, new ListItem("--Select Item--",""));
        This is now behaving correctly.

        Thank you.

        M :o)

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I haven't actually used DataBind with a DropDownList before.

          I think the DropDownList should retain it's items between postbacks. You should only do have to populate it's items if it's the first time the page is being loaded (if IsPostBack = "false").

          If this is not the case, only bind to this source in the PreRender event.

          Comment

          • E11esar
            New Member
            • Nov 2008
            • 132

            #6
            Thank you for your help.

            M :o)

            Comment

            Working...