Syspect
Syspect

Reputation: 921

How to run asp code, retrieved from the database

I have one single page in my ASP project, where upon clicking a different link, I retrieve ,from the database, the according HTML and load it. So I have some links, that are sitting in the page at all time (the part of the HTML that is not retrieved from the DB) but in some divs I get the HTML, and in there I also have links, that I'd like to run as ASP LinkButton controls.

This is, f.e., that always stays:

<div id="mainCenter">
    <asp:Literal ID="ltlContentCenter" runat="server" />
</div>

So in my code behind I have this:

protected void Page_Load(object sender, EventArgs e) {
    ltlContentCenter.Text = getPageCenter("media"); //function that retrieves
    ltlContentSide.Text = getPageSide("media");    // the HTML from the database
}

What I have in the DB, corresponding, is:

<div class="onCenterSmall">
    <asp:LinkButton ID="LinkButton10" Text="test" CommandArgument="test" OnCommand="loadPage_Command"
                runat="server" />
</div>
<div class="onCenterSmall"></div>
<div class="onCenterSmall"></div>

That chunk of HTML just gets put in between the <div id="mainCenter"><!-- in here --></div> on the page. And as you can see by yourself I get the result as string. For the HTML code that's fine, but for the <asp:LinkButton... that doesn't quite work. Any suggestions how I can do all the work in the same way (or at least to preserve the logic) but to get the LinkButton control working ?

Upvotes: 0

Views: 284

Answers (2)

Ross Jones
Ross Jones

Reputation: 1023

I know this is not going to answer your question, but just a few thoughts:

  1. Why would you need to use a link button? Why not just use a standard html anchor tag

  2. If it is because you need to execute some code how do you plan to deal with the code behind? Are you going to assume all html stored inside the DB inherits from the same class.

It just has to be said that this sounds like a really terrible idea though. What problem are you trying to solve?

Upvotes: 0

Alex
Alex

Reputation: 391

I have my doubts with the approach that you're taking (why are you storing fragments of ASP.NET markup in your database?) but if you must, I think Page.ParseControl is your friend. See MSDN: TemplateControl.ParseControl Method (String).

You can use Page.ParseControl to build a Control out of the input string, which you can then append to the Controls collection of any control (such as a PlaceHolder control).

Upvotes: 1

Related Questions