tags:

views:

89

answers:

1

I am working with ASP.NET

I have a DropDownList item and in there i have hyperlinks as values. What event must i use in my code behind to redirect the user to that URL when he selects the "eRate" item?

My code

   <asp:DropDownList ID="dropSelect" runat="server" Width="126px">
        <asp:ListItem>Please select</asp:ListItem>
        <asp:ListItem Value="http://www.erate.co.za"&gt;eRate&lt;/asp:ListItem&gt;
    </asp:DropDownList>

thanks in advanced!!

+3  A: 

add a onselectedindexchanged to the dropdown like this

OnSelectedIndexChanged="dropSelect_OnSelectedIndexChanged"

then codebehind you can do like this.

protected void dropSelect_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect(dropSelect.SelectedValue);
    }

you can do some extra null check and all that but this is the basic idea you can use

Dejan.S
Where must i place the OnSelectedIndexChanged="dropSelect_OnSelectedIndexChanged" ??
Etienne
+1 Pretty much what I would have said
James
@Etienne in the asp control on in your markup i.e. <asp:DropDownList ID="dropSelect" runat="Server" Width="126px" OnSelectedIndexChange="dropSelect_OnSelectedIndexChanged">
James
You might also want to set `AutoPostback="true"` on the `<asp:DropDownList>`, otherwise the event is not triggered until the next postback (i.e. a button click. Buttons has AutoPostback set to true as default).
awe
Thanks, what must i place in my code to have this open in a new window?
Etienne
You won't be able to do this using Response.Redirect as this is processed on the server side. Here is an answer that resolves this issue http://stackoverflow.com/questions/104601/asp-net-response-redirect-to-new-window
James
you have to do some javascript solution for that. the very basic one is this below. keep in mind that javascript might not work for everybody so you might consider working with the response.redirectresponse.write("<script>");response.write("window.open('dropSelect.SelectedValue','_blank')");response.write("</script>");
Dejan.S