Results 1 to 6 of 6

Thread: combo problem

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    combo problem

    (Dreamweaver MX, ASP.NET, VB.NET)

    I have an edit page which uses a combo box.

    The user chooses an item from the combo and the value is written away to the underlying field.

    The problem I have is that everytime the user goes to the webpage the 1st item is always displayed.

    Is there any way I can check to see if there is a value in the underlying field and if there is force the combo to jump to that item.

    Thanksin advance

    Parksie

  2. #2
    Member HumanCompiler's Avatar
    Join Date
    Dec 2002
    Location
    Decatur, IN USA
    Posts
    52
    If you enable viewstate (.EnableViewState = True) and make sure that you're only filling the DropDownList the first time the page loads (If Not IsPostBack Then), then it will stay on the last item you selected, which is what you're going for, right?
    -Erik Porter
    .NET MVP

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    I'm not sure about this.

    WHen the user first goes to the page, lets say the underlying field contains "3" which refers to the 3rd item in the combo I want the combo to display the text representing the third item.

    I'll have to apologise but I'm new to all this .net code.

    Parksie

  4. #4
    Member HumanCompiler's Avatar
    Join Date
    Dec 2002
    Location
    Decatur, IN USA
    Posts
    52
    I'm sorry, but I'm not following what you're asking for.

    You know about the .DataTextField and .DataValueField properties, right?
    -Erik Porter
    .NET MVP

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    VB Code:
    1. <script language="vb" runat="server">
    2. Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    3.     Dim myExistingValue As String = "item2"
    4.     If myExistingValue <> "" Then
    5.         If Not myDropDownList.Items.FindByValue(myExistingValue) Is Nothing Then       
    6.             myDropDownList.Items.FindByValue(myExistingValue).Selected = True
    7.         End If
    8.     End If
    9. End Sub
    10. </script>
    11. <html>
    12.   <body>
    13.     <form id="SelectAnItem" runat="server">
    14.         <asp:DropDownList
    15.             ID="MyDropDownList"
    16.             Runat="server">
    17.             <asp:ListItem Value="item1">item1</asp:ListItem>   
    18.             <asp:ListItem Value="item2">item2</asp:ListItem>
    19.             <asp:ListItem Value="item3">item3</asp:ListItem>
    20.         </asp:DropDownList>
    21.     </form>
    22.   </body>
    23. </html>

  6. #6
    Member HumanCompiler's Avatar
    Join Date
    Dec 2002
    Location
    Decatur, IN USA
    Posts
    52
    change it to this...the changed lines are bold

    VB Code:
    1. <script language="vb" runat="server">
    2. Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    3.     [b]If Not IsPostBack Then[/b]
    4.         Dim myExistingValue As String = "item2"
    5.         If myExistingValue <> "" Then
    6.             If Not myDropDownList.Items.FindByValue(myExistingValue) Is Nothing Then       
    7.                 myDropDownList.Items.FindByValue(myExistingValue).Selected = True
    8.             End If
    9.         End If
    10.     [b]End If[/b]
    11. End Sub
    12. </script>
    13. <html>
    14.   <body>
    15.     <form id="SelectAnItem" runat="server">
    16.         <asp:DropDownList
    17.             ID="MyDropDownList"
    18.             Runat="server"
    19.             [b]AutoPostBack="True">[/b]
    20.             <asp:ListItem Value="item1">item1</asp:ListItem>   
    21.             <asp:ListItem Value="item2">item2</asp:ListItem>
    22.             <asp:ListItem Value="item3">item3</asp:ListItem>
    23.         </asp:DropDownList>
    24.     </form>
    25.   </body>
    26. </html>

    The way you had it, it would set the selected item to "item2" every time and would never change.
    -Erik Porter
    .NET MVP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width