Rhs
Rhs

Reputation: 3318

ASP Query String From DropDown

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

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.

Upvotes: 3

Views: 1854

Answers (3)

msantiago
msantiago

Reputation: 346

Here's an easy way is to use Session

Example:

Session["SessionName"] = idDropDownList;

On another page, access only the content of the session

string idDropDownList = (string)Session["SessionName"];

I hope I helped.

Upvotes: 1

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14282

Try something like this

<asp:HyperLink ID="lnkEdit"
    NavigateUrl='<%# "Edit.aspx?" + Eval("UserID") + 
    "&menuid=" + myDropDown.SelectedValue %>'> MyText </asp:HyperLink>

and also set AutoPostBack to true on your drop down. Whenever you will change your dropdown, new selected value bind with hyperlink navigate url.

Upvotes: 1

Dave Zych
Dave Zych

Reputation: 21887

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.

Upvotes: 4

Related Questions