Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to apply dynamic text to an <h2> tag on a webform?

On the previous page, the user is going to click a button from a gridview. Each button click will produce a different result on the next page. I want to display text that will put information from a database into the header text. For example, if the user clicks the button next to the "03/04/2012 - 03/11/2012", I want the header on the next page to say: "Please enter data for 03/04/2012 - 03/11/2012".

Is this possible? Is this even recommended or am I better off using an asp:label control?

like image 375
Chris Avatar asked Mar 15 '12 17:03

Chris


1 Answers

Yes, this is possible and perfectly acceptable.

Use an embedded code block (sometimes called gator tags) <%=...%> in your markup. The = operator is equivalent to calling Response.Write, so it just outputs the value of whatever you give it inline.

<h2>Please enter data for <%=DateRange%></h2>

In this example, DateRange would be a property on your next page. If you're getting this date range data from a database as you say, then you would need to store it when you click on the grid view button and then retrieve it on the next page.

There is nothing wrong with doing it like this, but storing it in a database seems like overkill just to get the value on the next page. You can use one of the following instead:

  • Querystring
  • Session variables
  • Server.Transfer

A quick google search turned up this page which shows examples of all of these methods.

Regardless of which method you use, gator tags can still be used to set the value of the H2 on the next page.

Also, if you are using Asp.Net 4, then you can use <%: %>. It's Like <%= %> But it HtmlEncodes the output automatically.

like image 129
Robbie Avatar answered Sep 23 '22 01:09

Robbie