Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 = "".

   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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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

<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

 

   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

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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.

 

   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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...