listbox and dropdownlist bug in VS2005 webdeveloper??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maghi85
    New Member
    • Jun 2007
    • 28

    listbox and dropdownlist bug in VS2005 webdeveloper??

    hi,
    i'm working on a website with lots of webcontrols on visual studio 2005
    i'm trying to read the text on a drop down list but it always gives me the initial text and not the text that is selected by the user?
    is this a bug or is there another way about it?
    ive tried this

    Code:
    Dim A as string = dropdownlist1.selecteditem.text 'gives initial text
    Dim A as string = dropdownlist1.text                    'also gives initial text
    ???
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Try selected value and see what happens.

    Comment

    • maghi85
      New Member
      • Jun 2007
      • 28

      #3
      Originally posted by kenobewan
      Try selected value and see what happens.
      doesn't work either... gives the initial value =(

      Comment

      • gomzi
        Contributor
        • Mar 2007
        • 304

        #4
        Originally posted by maghi85
        hi,
        i'm working on a website with lots of webcontrols on visual studio 2005
        i'm trying to read the text on a drop down list but it always gives me the initial text and not the text that is selected by the user?
        is this a bug or is there another way about it?
        ive tried this

        Code:
        Dim A as string = dropdownlist1.selecteditem.text 'gives initial text
        Dim A as string = dropdownlist1.text                    'also gives initial text
        ???
        if you are binding your dropdownlist1 in page load, then place that code inside ->

        if not ispostback then
        YOUR CODE HERE
        endif

        Your problem may be because, everytime postback occurs, the controls get binded again. so, you will have to prevent the controls from being binded again when postback occurs.

        Comment

        • maghi85
          New Member
          • Jun 2007
          • 28

          #5
          Originally posted by gomzi
          if you are binding your dropdownlist1 in page load, then place that code inside ->

          if not ispostback then
          YOUR CODE HERE
          endif

          Your problem may be because, everytime postback occurs, the controls get binded again. so, you will have to prevent the controls from being binded again when postback occurs.

          how do you place the code inside?

          Comment

          • gomzi
            Contributor
            • Mar 2007
            • 304

            #6
            Originally posted by maghi85
            how do you place the code inside?
            What do you mean by that?

            Comment

            • maghi85
              New Member
              • Jun 2007
              • 28

              #7
              Originally posted by gomzi
              What do you mean by that?
              you wrote

              if you are binding your dropdownlist1 in page load, then place that code inside ->

              if not ispostback then
              YOUR CODE HERE
              endif

              please explain

              Comment

              • gomzi
                Contributor
                • Mar 2007
                • 304

                #8
                Originally posted by maghi85
                you wrote

                if you are binding your dropdownlist1 in page load, then place that code inside ->

                if not ispostback then
                YOUR CODE HERE
                endif

                please explain
                I explained earlier too maghi85.
                Anyway....
                Normally, what happens is that when a page loads, the contents in the page_load sub get executed.
                i.e. in your case suppose you have placed the code for binding dropdownlist in the page_load area, then it gets binded with that data when the page is requested.
                Now, later on when a postback occurs, i.e. either through a button or autopostback of your dropdownlist or whatever, the execution starts from the page load before it goes to the event for that respective control.
                so, if you don't place your code inside "if not ispostback", then the dropdownlist(in your case) gets binded again with the data. And since it gets binded again, the default value also gets set again.
                so, finally when you try to receive the selected value through the event for the control what you get is the default value that has been set again in the page load sub(in the absence of 'if not ispostback').
                so, since you say that you are not getting the selected value through the event, the only problem which might be possible IMO is that you are binding the dropdownlist again.
                so check it out!

                wow!!! not written such a long explanation. am impressed!

                Regards,
                Gomzi.

                Comment

                • Wing
                  New Member
                  • Jun 2007
                  • 28

                  #9
                  An example of what Gomzi is trying to say without writing it for you

                  Code on default.aspx
                  Code:
                  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                              <asp:ListItem Value="1">Value 1</asp:ListItem>
                              <asp:ListItem Value="2">Value 2</asp:ListItem>
                              <asp:ListItem Value="3">Value 3</asp:ListItem>
                  </asp:DropDownList
                  Code on default.aspx.vb
                  Code:
                  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                          If Not IsPostBack Then
                              'Bind Data
                          End If
                  End Sub
                  
                  Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
                          Dim A As String = DropDownList1.SelectedItem.Text
                          Dim B As String = DropDownList1.SelectedValue
                          Response.Write(A & "<br>" & B)
                  End Sub
                  Written to the browser when the index is changed.
                  Code:
                  Value 2
                  2
                  Hope that helps you out more.

                  Comment

                  • maghi85
                    New Member
                    • Jun 2007
                    • 28

                    #10
                    thanks alot you guys
                    i haven't tried it yet
                    but im sure it'll work....
                    now I have a better understanding

                    Comment

                    Working...