5

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.

3
  • I hate it when people just link me MSDN articles, but these should be really useful for what you need to do. You can create every control that is available in design time programmatically. These links should show you how to do it, rather than inserting HTML strings into your HTML mark-up. msdn.microsoft.com/en-us/library/kyt0fzt1(v=vs.110).aspx msdn.microsoft.com/en-us/library/… Commented Nov 18, 2013 at 16:59
  • I really don't mind, what you have commented will undoubtedly lead me to a more cleaner solution. Commented Nov 18, 2013 at 17:02
  • If you have any issues, just comment and I will try and get back to this later on, currently in work :) Commented Nov 18, 2013 at 17:10

1 Answer 1

4

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.
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.