简体   繁体   中英

Render Asp.net server controls dynamically

I have a string stored in DB as "<asp:panel id='test' runat='server' ></asp:panel>" . I am trying to get this data and put it in a div which have runat="server" in it using innerHtml property.

protected void Page_Load(object sender, EventArgs e)
{
    Layouts.InnerHtml = GetTemplate();
}

public string GetTemplate()
{
    CompassModel.Layout Layout;
    using (CompassEntities db = new CompassEntities())
    {
        Layout = (from q in db.Layouts
                        where q.LayoutID == 1
                        select q).SingleOrDefault();
    }

    return Layout.LayoutHTML;
}

when I am trying to access this dynamically added panel using findcontrol() method it returns null.

Is there any way to render it so that I can access panel at the runtime?

You have to look at asp server controls as objects and not markup. The markup is handled by .net once the object is rendered into html.

So just adding the .net markup to the page won't give you the desired effect.

What you need to do is create a new panel object and add that to the control collection of a placeholer, or panel, or any other control that will allow you to add to their collection.

Here's an example of what I'm talking about

pnlContainer.Controls.Add(new Panel{ID = "test"});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM