Monday, October 24, 2011

Create a hyperlink using a QueryString or Form parameter in C# ASP.NET

 

This is a very short code snippet which shows you how you can create a hyperlink using a QueryString or Form parameter received from a referring page.

Note that you would need to do two things in order for this to work:

Step 1
First, paste the code below in the HTML markup.  The code below assumes that you are using a querystring parameter called “ID” that was passed over from a previous page.

<asp:HyperLink Target="_blank" ID="HyperLinkViewFeedback" runat="server"
NavigateURL='<%# String.Concat("~/Feedback/ViewFeedback.aspx?ID=",Request.QueryString["ID"]) %>'>View Feedback</asp:HyperLink>

Step 2
Add the “Page.DataBind()” method to the OnLoad of the code-behind file.

protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}

After that, you should see the hyperlink with the ID automatically appended as follows:


2011-10-24_132412