0

I have two dropdownlists:

First one:

<asp:DropDownList ID="Drddl" class="form-control form-control-sm" Height="30" runat="server"
    Width="350" Enabled="False" AppendDataBoundItems="true" AutoPostBack="true" DataTextField="FullName" DataValueField="Name">
</asp:DropDownList>

and gets filled on page load :

If Not Page.IsPostBack Then
    FillDrData()
End If

The second:

<asp:DropDownList ID="Drddl" class="form-control form-control-sm" Height="30" runat="server" Width="350" Enabled="False" AppendDataBoundItems="true" AutoPostBack="true" DataTextField="FullName" DataValueField="Name"></asp:DropDownList>

and gets filled on SelectedIndexChanged of first one:

Private Sub Drddl_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Drddl.SelectedIndexChanged
    FillAgenceData()
End Sub

My question is:

Everything is working fine until I click submit button that read selected items from both dropdownlists and save it in SQL database. The Agenciesddl selecteditem always return to 1.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
bnoor
  • 21
  • 2

2 Answers2

0

Be mindful of the postbacks of each dropdownlist. Quick and dirty way would be to set the selected values in ViewState or Session variables then get the variables on Submit. See below for rough example (pardon the c#)

Private void Drddl_SelectedIndexChanged(sender As Object, e As EventArgs)
{
    ViewState["DrddlVal"] = Drddl.SelctedValue;
}

private void submit(sender As Object, e As EventArgs)
{
   string dbparmval = ViewState["DrddlVal"].ToString();
}
JobesK
  • 347
  • 1
  • 2
  • 6
0

As usual the answer is very simple.

the DataValueField property of DropDownList must be UNIQUE when SelectedIndexChanged event triggered, the DropDownList goes to first value if he find more than one item has same value.

thanks for this article: enter link description here

thanks everyone..

Community
  • 1
  • 1
bnoor
  • 21
  • 2