开发者

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"] = "b开发者_如何转开发oth";
                test1.Controls.Add(div);

            }


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');");


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


asp:Panel is a DIV when it gets rendered


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.


ASPX code

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

</div>

C# code

alert.InnerHtml= ANY HTML CODE  ;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜