Is there a way to make a div server? So can I turn it into a control?

Is there a way to make a div server? So can I turn it into a control? In asp.net?

EDIt:

IF so, how can I say my code below to make div ID = test runat server?

while (reader.Read()) { System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div"); div.Attributes["class"] = "test"; //div.Style["float"] = "left"; div.ID = "test"; Image img = new Image(); img.ImageUrl = String.Format("{0}", reader.GetString(1)); // this line needs to be represented in sql syntax //img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg"; img.AlternateText = "Test image"; div.Controls.Add(img); div.Controls.Add(ParseControl(String.Format("&nbsp&nbsp "+"{0}", reader.GetString(0)))); div.Style["clear"] = "both"; test1.Controls.Add(div); } 
+6
html c #
source share
5 answers

Yes

 <div runat="server"></div> 

will make it server-side. You can generate it in code using

 var myDiv = new HtmlGenericControl("div"); 

Edit: what you create is a server-side control, there is no need to add runat="server" .

Change per comment:

 <div onclick="alert('hello')" runat="server"></div> 

or

 var myDiv = new HtmlGenericControl("div"); myDiv.Attributes.Add("onclick", "alert('hello');"); 
+19
source share

You can make div runat = "server", give it an identifier and a link to C #, if that is what you need. However, why not just use the asp panel: they do the same job essentially, and the panel displays the div in most scripts.

+14
source share

asp: Panel is a DIV when it gets rendered

+9
source share

this is certainly not the most elegant option, but adding a literal control and creating your div from the code will do the trick.

-1
source share

Aspx code

 <div id="alert" runat="server" style="float: left"> </div> 

C # code

 alert.InnerHtml= ANY HTML CODE ; 
-1
source share

All Articles