DropDownList Redirect

Nate Bross

Contributor
Joined
Apr 6, 2005
Messages
601
Location
Chicago, IL
I know this is probably a really simple question, but I have an ASP.NET (v2.0) DropDownList and when the user selects a different item, I would like to redirect them to different page baste upon the item they selected.

This is the code I'm using, but for some reason DropDownList1.Text is ALWAYS = "".
Visual Basic:
    If Page.IsPostBack() Then
        If DropDownList1.Text <> "" Then
                Response.Redirect("default.aspx?PageAction=reviews&ShowOnly=" & DropDownList1.Text)
        End If
    End If

Any advice is appreciated.
 
Okay, I changed some code around and the new issue, is that instead of the DropDownList.Text value being "", now it is always the first ListItem in the DropDownList.

For Example, this DropDownList
Code:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Text="Item One" />
<asp:ListItem Text="Item Two" />
<asp:ListItem Text="Item Three" />
</asp:DropDownList>
always has a .Text value of "Item One"

Here's the Page_Load sub

Visual Basic:
    Protected Sub ReviewList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ReviewList.Load
        Dim db As New DBAccess()

        DropDownList1.DataSource = db.GetData("SELECT DISTINCT Name from Products Order By Name;")
        DropDownList1.DataTextField = "Name"
        DropDownList1.DataBind()

        If Page.IsPostBack() Then
            If DropDownList1.Text <> "" Then
                Response.Redirect("default.aspx?PageAction=reviews&ShowOnly=" & DropDownList1.Text)
            End If
        End If
    End Sub
 
Sorry, I posted too soon. Shortly after posting I had some inspiration to continue working on this stupid thing.

I feel really silly posting three times in half-hour and solving my own problem, but maybe this can help somebody else.

Here is the working code incase anyone else was having similar trouble.

Visual Basic:
    Protected Sub ReviewList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ReviewList.Load
        If Not Page.IsPostBack() Then
            Dim db As New DBAccess()

            DropDownList1.DataSource = db.GetData("SELECT DISTINCT Name from Products Order By Name;")
            DropDownList1.DataTextField = "Name"
            DropDownList1.DataBind()
        End If

        If Page.IsPostBack() Then
            If DropDownList1.Text <> "" Then
                Response.Redirect("default.aspx?PageAction=reviews&ShowOnly=" & DropDownList1.Text)
            End If
        End If

        If Request.QueryString("ShowOnly") <> "" Then
            DropDownList1.Text = Request.QueryString("ShowOnly")
        End If
    End Sub

I had to change the code so that it would only do a databind on the control if the Page.IsPostBack return's a false value.

Additionally the last if just makes sure the DDL's text value is the same as the one in the QueryString.
 
Back
Top