Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net div still not accessible after runat="server"

Tags:

asp.net

I have a div element in my HTML. I added a id and runat attributes to the element:

<div id="footer" runat="server"> 

After rendering, viewing the HTML shows:

<div id="ctl00_footer">

However, I cannot access it from the page's .aspx.cs code:

footer.InnerHtml += "test";

How do I access that element from the C# code?

like image 673
user101579 Avatar asked Jan 17 '23 12:01

user101579


2 Answers

you can use FindControl("footer"), and cast it to HtmlGenericControl.

HtmlGenericControl footer = (HtmlGenericControl)FindControl("footer")
like image 168
Porco Avatar answered Jan 20 '23 01:01

Porco


One of the reasons can be that you don't have designer file for that page. So you can't access element by it's ID.

Just add class with name [your page name].aspx.designer.cs, open it, remove all code, save it, go to your view and click save - designer must generate code of all elements from your view. After this you can access element by ID.

like image 41
demo Avatar answered Jan 20 '23 01:01

demo