Search code examples
asp.netif-statementasp.net-controls

IF SelectedItem.Equals ??? Then redirect Statement


I have a gridview in my website in am building in Visiual studio 2010. IM new to programming and have a query in reference to redirecting the user to another page based on the results of the gridview.

My gridview works perfectly fine and pulls back items via a selection made in a dropdown list. These items have and id assigned (1 for Weekly & 2 for Monthly).

My users selects the Weekly or Montly option from the dropdown and the grid view is populated with this data. (This part works perfectly fine).

Once the results are displayed i want the user to then press the 'Create' button and for them to be directed to the correct Weekly.aspx or Monthly.aspx page based on the selected item from the dropdown list.

So far i have the following code which seems to redirect the user to the Monthly.aspx page for either selection from the dropdown list.

Protected Sub btnCreate_Agenda_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate_Agenda.Click

    If DropDownList1.SelectedItem.Equals("1") Then
        Response.Redirect("Weekly.aspx")
    Else
        Response.Redirect("Monthly.aspx")
    End If

End Sub

Can anyone point me in the right direction?

In my page load event i would also like to make sure that for the webpage item 1 (Weekly) is the default selection. I have tried doing this by adding the following code to the page_load event but i have no just with the results.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    DropDownList1.SelectedItem.Equals("1")

End Sub

Solution

  • The SelectedItem property will return a ListItem object, so that will never be equal to the string "1". Use the SelectedValue property instead:

    If DropDownList1.SelectedValue = "1" Then
      Response.Redirect("Weekly.aspx")
    Else
      Response.Redirect("Monthly.aspx")
    End If
    

    Regarding setting the default, the SelectedItem property is a read-only property. Use the SelectedIndex or SelectedValue properties to select an item:

    DropDownList1.SelectedValue = "1"