ASP.NET Webforms UpdatePanel duplicate contents

2019-08-15 01:50发布

问题:

I've been handed a huge Webforms project which I'm trying to understand, and I have a problem where an Update Panel is duplicating a lot of its content. The aspx code for the panel is huge, hundreds of lines long, but it basically looks like this simple example, only with lots more asp:TextBox and asp:ListBox.

<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" RenderMode="Block" UpdateMode="Conditional">
            <ContentTemplate>
                <div><table><tbody><tr><td>
                <label>Search</label><asp:TextBox ID="Search" runat="server"  />
                <asp:LinkButton runat="server" OnClick="find_Click" >Find</asp:LinkButton>
                </td></tr></tbody></table></div>
                <div id="a"><table><tbody><tr><td>
                <label>Result</label><asp:TextBox ID="Result" runat="server" />
                </td></tr></tbody></table></div>       
            </ContentTemplate>
        </asp:UpdatePanel>

and code behind like this.

public void find_Click(Object sender, EventArgs e)
{
    Result.Text = "oranges";
}

When you click the LinkButton, I would expect to see in the result the <div id="a"> section, but with the text 'oranges' in the TextBox. What you actually get is <div id="a"> with 'oranges', followed by the original <div id="a"> with the empty TextBox. The worst bit is that it doesn't do it in this simple example, nor even in a page that I created that had all the original asp:TextBox and asp:ListBox but filled with dummy data. Can anyone point me to any good ways of approaching this problem?

回答1:

You might have already tried this, but in the actual problem page, is it possible to remove as many server controls out of the updatepanel and just leave in offending textbox and then see what happens? I'm guessing you'll probably have to comment out alot of .cs/.vb code, which can be a pain.

Also try removing the updatepanel and see what happens.



回答2:

Another solution would be to make sure all HTML tags are closed inside the asp:UpdatePanel. In my case, I've got the open header tag placed in the Site.Master file (outside of UpdatePanel control) and the closing header tag inside the UpdatePanel control (on aspx page). Because of that, every time the UpdatePanel postbacks it recreates the closing header tag again causing the content to be duplicated. After I placed the closing tag into Site.Master file, everything worked beautifully.



回答3:

Some serious refactoring later, it now looks like (a very bloated version of) this.

<div><table><tbody><tr><td>
<label>Search</label><asp:TextBox ID="Search" runat="server"  />
<asp:LinkButton runat="server" OnClick="find_Click" >Find</asp:LinkButton>
</td></tr></tbody></table></div>
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" RenderMode="Block" UpdateMode="Conditional">
            <ContentTemplate>
                <div id="a"><table><tbody><tr><td>
                <label>Result</label><asp:TextBox ID="Result" runat="server" />
                </td></tr></tbody></table></div>       
            </ContentTemplate>
        </asp:UpdatePanel>