Tuesday, March 1, 2011

Working with ASP.NET DropDownList item not working - need an event?

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">eRate</asp:ListItem>
    </asp:DropDownList>

thanks in advanced!!

From stackoverflow
  • 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

    Etienne : Where must i place the OnSelectedIndexChanged="dropSelect_OnSelectedIndexChanged" ??
    James : +1 Pretty much what I would have said
    James : @Etienne in the asp control on in your markup i.e.
    awe : You might also want to set `AutoPostback="true"` on the ``, otherwise the event is not triggered until the next postback (i.e. a button click. Buttons has AutoPostback set to true as default).
    Etienne : Thanks, what must i place in my code to have this open in a new window?
    James : 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
    Dejan.S : 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.redirect response.write("");

0 comments:

Post a Comment