setting DIV content programmatically, how??

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hey

ASP.NET 2.0

In my webpage (.aspx) I have this tag:
<div id="test" class="pageTitle">Hello World</div>

I'm wondering how I in Page_Load event can change the "Hello World" text to
something else...?
First I need to get a reference to this div tag, so I'm trying this code,
I'm NOT sure it works:
System.Web.UI.HtmlControls tttt = (System.Web.UI.HtmlControls)
System.Web.UI.HtmlControls.HtmlGenericControl.cell.FindControl("test");

Any suggestions?

Jeff
 
Hi Jeff,

You need a runat="server" attribute to be able to access it
programatically from the Page_Load method.

For example:

ASPX
<div id="divChangeMe" runat="server">Hello World</div>

CODE BEHIND (In Page_Load)
divChangeMe.InnerHtml = "Goodby World!";

N.B. you may have to go from Source to Designer to get access to the
div tag in the code beind... At least i have to sometimes.

Hope this helps...

Justin
 
To use a control on server side you need to set runat=server. Then you can
refer to it just by id, no need to call FindControl.
 
Hey

ASP.NET 2.0

In my webpage (.aspx) I have this tag:
<div id="test" class="pageTitle">Hello World</div>

I'm wondering how I in Page_Load event can change the "Hello World" text to
something else...?
First I need to get a reference to this div tag, so I'm trying this code,
I'm NOT sure it works:
System.Web.UI.HtmlControls tttt = (System.Web.UI.HtmlControls)
System.Web.UI.HtmlControls.HtmlGenericControl.cell.FindControl("test");

Any suggestions?

Since everybody else has given you perfect answers here I'll just fill
in the fact that you can get ANY controls (inclusive the body tag,
title tag etc) to become a "runat='server'" tag...
This also gives you nice capabilities to use the ~ operator like for
instance:
<a href="~/MyPage.aspx" runat="server">xxx</a>
This will map to e.g. localhost/MyApp/MyPage.aspx when run in the
debugger...

Nifty little trick :)

Thomas
 
Back
Top