Adding a ASP Control to page from Code Behind within InnerHTML

故事扮演 提交于 2019-12-23 12:56:13

问题


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 create dynamically as the div content can change dependent on conditions.

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.

I'm creating my Button like so (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 Said above, The reason this doesn't work is because of InnerHtml hating backslashes, I think.

I do have a solution to this; and that's 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>

I would then add my Innerhtml to the _Top Div, instead of the HelpBoxDiv which I am currently doing now. However this solution doesn't teach me anything.

I am hesitant to ask questions here, as I know a lot of question have been asked and I am sure this one has before, but I didn't find a solution. Any help is much appreciated.

Thank you.


回答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:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>

Code Behind

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's command event.
}


来源:https://stackoverflow.com/questions/20053217/adding-a-asp-control-to-page-from-code-behind-within-innerhtml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!