tags:

views:

119

answers:

1

I have a web page with an empty div, id="PreAcquisitionDiv" runat="server"

In the C# code-behind, on Page_Load, I build up a table and set it to PreAcquisitionDiv.InnerHtml

My page displays OK. I cause a PostBack, intending to change the data in the table. In the code, the PreAcquisitionDiv.InnerHtml does change, but the table I put there the first time is still displayed on the page instead.

I have seen some warnings about using InnerHtml at all, but the examples of alternatives that I have seen are JavaScript.

I am using IE 8, but I observe the same behavior in Chrome.

Thank you for any suggestions!

For example, this code displays "SomeStuff" when it first displays the page; then still "SomeStuff" after the PostBack even though the InnerHtml was changed.

protected void Page_Load( object sender, EventArgs e )  
{ 
    StringBuilder html = new StringBuilder( @"<TABLE BORDER='1' WIDTH='100%'>" );  
    if( !IsPostBack ) 
    { 
        html.Append(@"<TR><TD>SomeStuff</TD></TR>" );  
    } 
    else  
    {  
        html.Append( @"<TR><TD>Some New Stuff</TD></TR>" );  
    } 
    html.Append( @"</TABLE>" );
    this.PreAcquisitionDiv.InnerHtml = html.ToString( );
}
A: 

If you are changing the HTML on post back are you running your function to set it on post back as well?

UPDATE: I copied and pasted your code and it works fine for me. Here is my entire code behind:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Sandbox
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder html = new StringBuilder(@"<TABLE BORDER='1' WIDTH='100%'>");
            if (!IsPostBack)
            {
                html.Append(@"<TR><TD>SomeStuff</TD></TR>");
            }
            else
            {
                html.Append(@"<TR><TD>Some New Stuff</TD></TR>");
            }
            html.Append(@"</TABLE>");
            this.PreAcquisitionDiv.InnerHtml = html.ToString();
        }

        protected void btnTest_OnClick(object sender, EventArgs e)
        {
        }
    }
}

And here is my entire ASPX page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sandbox._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="PreAcquisitionDiv" runat="server">

    </div>
    <asp:Button ID="btnTest" runat="server" OnClick="btnTest_OnClick" />
    </form>
</body>
</html>
Dustin Laine
Yes, I am running the method and, when I get to the statement, this.PreAcquisitionDiv.InnerHtml = html.ToString( ); the value does change. It's just that, when the page is re-displayed, the first values are still there and the new ones are not.
Kelly
Post some code, can't go any further without.
Dustin Laine
Edited and posted above; thank you.
Kelly
See my update in my answer.
Dustin Laine
Thank you. I created a new website and pasted in your code. I get two build errors: missing </html>, which is easy, but also "Could not load type Sandbox._Default." which I don't know what to do with.
Kelly
Sorry, the </html> was there but got snippet out of the code paste. I just created a new web application and pasted your code. That is a namespace issue, so you may need to change that. DOes not affect the functionality.
Dustin Laine
Thank you for your sample. I see that it works....
Kelly
If it helped mark as answer.
Dustin Laine