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

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

EDIt:

IF so how can I tell 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);

            }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Yes

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

will make it a server side control. You can generate it in the code behind using
var myDiv = new HtmlGenericControl("div");

Edit: What you’re generating is a server side control, there is no need to add runat="server".

Edit per comment:

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

or
var myDiv = new HtmlGenericControl("div");
myDiv.Attributes.Add("onclick", "alert('hello');");

Method 2

You can make a div runat="server", give it an id and the reference it from C# if that’s what you’re after. However, why not just use an asp:panel, they do the same job essentially and the panel renders a div out in most scenarios

Method 3

asp:Panel is a DIV when it gets rendered

Method 4

it is definitely not be the most elegant solution, but adding a literal control and build up your div from code will do the trick.

Method 5

ASPX code

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

</div>

C# code
alert.InnerHtml= ANY HTML CODE  ;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x