Deepak D
Deepak D

Reputation: 103

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?

Upvotes: 1

Views: 846

Answers (1)

Smeegs
Smeegs

Reputation: 9224

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

Upvotes: 3

Related Questions