I have a form with 2 dropdownlists and a submit button.
I want to pass the values of the lists in a URL when the list is submitted but for some reason the initial value of the ddl is getting passed.
Here’s my code:
<form runat="server">
<asp:DropDownList runat="server" ID="ddlCategory" />
<asp:DropDownList runat="server" ID="ddlLocation" />
<asp:button runat="server" ID="searchTT" OnClick="searchmyTT" Text="SEARCH" />
</form>
Sub searchmyTT(Source As Object, e As EventArgs)
Response.Redirect("/?cat=" & ddlCategory.SelectedItem.Value & " loc=" & ddlLocation.SelectedItem.Value)
End Sub
This ends up being redirected to /?cat=%&loc=%
You are likely populating the DDL before the event code gets fired. When you populate its values, it will reset the selected value to the first index. Wrap your population code in something like:
For a reference of the ASP.net lifecycle see here: http://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events
The important ones in this case are Load and Control Events (note that Load happens before your control events)
EDIT: fix VB syntax – I’m a little rusty
EDIT2: add reference to ASP.Net Lifecycle