3

I need to Read/Display some front-end code/elements from Code Behind. Scenario: I have a page, in which I have two (2) <div>, the top <div> and the bottom <div>.

I have two buttons inside top <div> as given below and the <div> at bottom is blank. Below is the code snippet:

Default.aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="navbar">
                <asp:Button ID="btnHome" runat="server" Text="Home"
                    OnClick="btnHome_Click" />
                <asp:Button ID="btnProducts" runat="server" Text="Products"
                    OnClick="btnProducts_Click" />
            </div>
            <div id="contentArea">
            </div>
        </form>
    </body>
</html>

When I click the home button, some HTML elements should become part of the Default.aspx page inside a <div> given below:

<div id=contentArea">
</div>

The above code should look like the code below:

<div id="contentArea">
    <h1>Home</h1>
    <p>Home Elements here</p>
</div>

When I click Product button, so that the code look like:

<div id="contentArea">
    <h1>Products</h1>
    <p>Products Elements here</p>
    <asp:HyperLink ID="lnkMicrosoft" runat="server" Text="Go to Microsoft"
        NavigateUrl="http://www.microsoft.com"></asp:HyperLink>
    <asp:HyperLink ID="lnkGoogle" runat="server" Text="Go to Google"
        NavigateUrl="http://www.google.com"></asp:HyperLink>
</div>
2
  • if you want to manipulate contentArea, you should make it runat="server" Commented Jan 31, 2017 at 19:16
  • 1
    Thanks for such a fully working advice. Commented Feb 1, 2017 at 5:30

1 Answer 1

11

You can use Panel to display html contents which renders as div tag.

For example,

ASPX

<div id="navbar">
    <asp:Button ID="btnHome" runat="server" Text="Home" OnClick="btnHome_Click" />
    <asp:Button ID="btnProducts" runat="server" Text="Products" 
        OnClick="btnProducts_Click" />
</div>
<asp:Panel ID="contentArea" runat="server">
</asp:Panel>

Code Behind

protected void btnHome_Click(object sender, EventArgs e)
{
    var h1 = new HtmlGenericControl("h1") {InnerText = "Home"};
    contentArea.Controls.Add(h1);

    var p = new HtmlGenericControl("p") {InnerText = "Home Elements here"};
    contentArea.Controls.Add(p);
}

protected void btnProducts_Click(object sender, EventArgs e)
{
    var h1 = new HtmlGenericControl("h1") {InnerText = "Products"};
    contentArea.Controls.Add(h1);

    var p = new HtmlGenericControl("p") {InnerText = "Products Elements here"};
    contentArea.Controls.Add(p);

    var lnkMicrosoft = new HyperLink
    {
        Text = "Go to Microsoft",
        NavigateUrl = "http://www.microsoft.com"
    };
    contentArea.Controls.Add(lnkMicrosoft);

    var lnkGoogle = new HyperLink
    {
        Text = "Go to Google",
        NavigateUrl = "http://www.google.com"
    };
    contentArea.Controls.Add(lnkGoogle);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Bundle of Thanks Dear, Like it and Love it, its working and fulfill my requirements. God Bless you ever
This answer suited my needs a little better: stackoverflow.com/a/20855024/1415929

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.