Patrick Keane
Patrick Keane

Reputation: 663

How do I use a C# string in my .aspx webpage?

I have passed a value through a url to a C# .net page using query string. The url of the page looks like this:

http://contoso.com/products.aspx?field1=value1

And in C#, I have this to catch it:

String myValue = Request.QueryString["field1"];

What im looking to do is use this value in the page, something like this:

<h1><%# Eval("myValue") %></h1>

How would I go about doing this? Obviously this HTML code doesn't work. I have exhausted some google searches on the subject so any information would be appreciated!!

Upvotes: 2

Views: 4675

Answers (3)

Jeffrey Lott
Jeffrey Lott

Reputation: 7409

In your aspx file, you can define a text field like so:

 <h1 Id="label" runat="server"/>

Then, in your code behind file add:

label.InnerText = Request.QueryString["field1"];

Upvotes: 0

Smileek
Smileek

Reputation: 2782

Try to add runat="server" and id to your h1 tag, so you can use it in cs-file.
HTML:

<h1 id="myHeader" runat="server"></h1>

CS:

myHeader.InnerText = myValue;

Upvotes: 1

Dave Zych
Dave Zych

Reputation: 21887

You can either create a Property on your page and use code tags, or set the h1 tag as runat="server" and set the value like that.

Property:

public string MyString{ get; set; }

public void Page_Load(object sender, EventArgs e)
{
    MyString = Request.QueryString["field1"];
}

Then in your markup:

<h1><%= MyString %></h1>

Alternatively, using the runat="server" method on the h1 tag:

Markup:

<h1 id="myH1" runat="server"></h1>

Code:

myH1.InnerText = Request.QueryString["field1"].ToString();

Upvotes: 2

Related Questions