Skip to content Skip to sidebar Skip to footer

Adding A Asp Control To Page From Code Behind Within Innerhtml

I'm trying to add a button to the webpage, from the code behind. I have a single empty div on my main page that visible on and off, when needed. However the content I wish to creat

Solution 1:

I have realised that within my ASP Control I use a / (backslash) which cancels out my HTML. The problem I now have is understanding how I can get around this with code, is there a way to add ASP Controls to the web page? I am open to suggestions outside of the InnerHtml.

You cannot add ASP.Net server control like a literal string.

Instead, you want to add the dynamic server control to the following approach -

ASPX

<asp:PlaceHolderrunat="server"ID="PlaceHolder1"></asp:PlaceHolder>

Code Behind

protectedvoidPage_Init(object sender, EventArgs e)
{
    var button = new Button
    {
        ID = "helpButton_0",
        CommandArgument = "0",
        CssClass = "HelpButton",
        Text = "?"
    };
    button.Command += button_Command;
    PlaceHolder1.Controls.Add(button);
}

privatevoidbutton_Command(object sender, CommandEventArgs e)
{
    // Handle dynamic button's command event.
}

Post a Comment for "Adding A Asp Control To Page From Code Behind Within Innerhtml"