Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP Query String From DropDown

Tags:

c#

asp.net

I have a webpage: Menu.aspx, where I have the following controls of relevance:

  • a DropDownList with some values
  • a hyperlink, which redirects me to a page called Edit.aspx.

In my Edit.aspx page, I have a cancel button, which redirects the user back to the Menu page

What I would like to do is when my user clicks on the hyperlink to go to the Edit page,index of the DropDownList is preserved in a query string In my Menu.aspx page I have the following aspx code, but I am not sure how to proceed

<asp:HyperLink
    ID="lnkEdit"
    NavigateUrl='<%# "Edit.aspx?" + Eval("UserID") + ...not sure... %>'
</asp:HyperLink>

<asp:DropDownList
    ID="myDropDown"
    ...some <asp:ListItems/>
</asp:DropDownList>

EDIT: Clarified why Im using NavigateURL. Because my query string already does an Eval to determine the user ID.

like image 429
Rhs Avatar asked Jun 26 '26 07:06

Rhs


1 Answers

I would use a LinkButton control with a server-side OnClick event.

<asp:LinkButton ID="lbtn1" runat="server" OnClick="lbtn1_Click" 
    CommandArgument='<%#Eval("UserID") %>' />

Server side method:

public void lbtn1_Click(object sender, EventArgs e)
{
    LinkButton lbtn = (LinkButton)sender;
    string userID = lbtn.CommandArgument;
    string dropDownValue = myDropDown.SelectedValue;
    string navigateUrl = string.Format("Edit.aspx?userid={0}&dropdown={1}", 
        userID, dropDownValue);
    Response.Redirect(navigateUrl);
}

EDIT: As Royi Namir points out below, javascript is a better option if you can use that. This creates an unnecessary round trip to the server.

like image 150
Dave Zych Avatar answered Jun 28 '26 21:06

Dave Zych



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!