setting DIV content programmatically, how??

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
 
J

jwwishart

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
 
E

Eliyahu Goldin

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.
 
T

Thomas Hansen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top