Adding an ASP Control to a Page from Code Behind InnerHTML

I am trying to add a button to a webpage starting with code. I have one empty div on my main page that displays on and off when necessary. However, the content I want to create is dynamic, because the contents of the div may vary depending on the conditions.

I realized that in my ASP Control I use / (backslash), which overrides my HTML. The problem I'm facing right now is to figure out how I can get around this with code, is there a way to add ASP Controls to a web page? I am open to suggestions outside of InnerHtml.

I create my button like this (in my code behind):

      string buttonout = string.Format("<asp:Button ID=\"helpButton_0\" CommandArgument=\"0\" CssClass=\"HelpButton\" runat=\"server\" Text=\"?\"/>");
      innercontent[0] = string.Format("<table><tr><td>Lead Passenger Information</td></tr><tr><td>Here by deafaul we used your detaisl from your profile, if you're not the lead passenger (As in you're booking for someone else) then please change details.</td></tr><tr><td>{0}</td></tr></table>"+ buttonout);

As stated above, the reason this is not working is because of InnerHtml hating backslashes, I think.

I have a solution to this; and that by adding more divs to the page.

        <div id="HelpBoxDiv" runat="server" Visible="false">
            <div id="HelpBoxDiv_Top" runat="server" Visible="true">
            </div>
            <div id="HelpBoxDiv_Bottom" runat="server" Visible="true">
                <asp:Button ID="button_HelpBox_false" runat="server" />
            </div>
        </div>

Then I will add InnerHtml to _Top Div, and not to the HelpBoxDiv that I am currently doing now. However, this solution will not teach me anything.

I hesitate to ask questions here, because I know that a big question was asked, and I'm sure it was before, but I did not find a solution. Any help is greatly appreciated.

Thanks.

+4
source share
1 answer

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

ASP.Net, .

-

ASPX

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

protected void Page_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);
}

private void button_Command(object sender, CommandEventArgs e)
{
    // Handle dynamic button command event.
}
+3

All Articles