Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
need some assistance. I am currently learning vb.net with sql.

I have two textboxes (txtValue and txtText) , a dropdownlist (ddl1) and save button (btnAdd). I insert value to txtValue and txtText (eg: txtValue: A and txtText: Apple). when i click button Add, The value will display in dropdownlist (like this; A - Apple) and both data will be save in the sqlserver (db).

im having issue where when i click button Add, the data is not display in the dropdownlist. But when i refresh my browser, the data will display in dropdownlist.

ASP.net
XML
<asp:DropDownList ID="ddl1" runat="server" CssClass="form-control" AutoPostBack="true"></asp:DropDownList>

     <label class="form-control-static">Value</label>
     <asp:TextBox ID="txtValue" runat="server" CssClass="form-control"></asp:TextBox>
      <label class="form-control-static">Text</label>
      <asp:TextBox ID="txtText" runat="server" CssClass="form-control"></asp:TextBox>

      <button id="btnAdd" runat="server" style="min-width: 80px;">Add</button>



VB.Net
VB
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Page.IsPostBack = False Then

            Dim rs As DataSet


            ddl1.Items.Clear()
            ddl1.Items.Add("")
            rs = db.ExecuteSelect("SELECT value, text FROM dropdownlist ", Session("CnnStr").ToString)
            For i = 0 To rs.Tables(0).Rows.Count - 1

                ddl1.Items.Add(rs.Tables(0).Rows(i).Item("value").ToString & " - " & rs.Tables(0).Rows(i).Item("text").ToString)

            Next
        End If
    End Sub



    Protected Sub btnAdd_ServerClick(sender As Object, e As EventArgs) Handles btnAdd.ServerClick
        db.ExecuteInsert("INSERT INTO dropdownlist (value, text ) VALUES( " & _
                             "'" & db.sqlstr(txtValue.Text) & "'," & _
                            "'" & db.sqlstr(txtText.Text) & "') ", _
                             Session("CnnStr").ToString)

    End Sub
Posted

1 solution

It looks like when you click Add, the DropDownList is doing a second PostBack and you are temporary losing the ViewState value. For the dropdownlist, set:
C#
AutoPostBack = "false".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900