如何创建独立的Update Panel?

3
据我了解,UpdatePanel应该是分别进行失效处理的,即触发一个UpdatePanel不应该影响其他面板的控件。对于不在任何UpdatePanel中的控件确实是这样工作的,但是任何UpdatePanel的触发都会影响其中的控件,包括那些在UpdatePanel中的控件。
    <form id="form1" runat="server">
<asp:ScriptManager ID="SM1" runat="server"/>
<div>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <asp:UpdatePanel ID="update1" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="update2" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
</form>

代码后台:

        protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "Clicked 1";
        TextBox2.Text = "Shouldn't appear";
        TextBox3.Text = "Neither should this";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox2.Text = "Clicked 2";
        TextBox1.Text = "Shouldn't appear";
        TextBox3.Text = "Neither should this";
    }

“不应该出现”没有出现,但是“不应该出现”出现了 :(。 有人能帮我理解是什么原因导致了这种行为吗?

1个回答

3

现在我看到了,我忘记给UpdatePanels添加UpdateMode="Conditional"属性。

正确的代码:

    <form id="form1" runat="server">
<asp:ScriptManager ID="SM1" runat="server"/>
<div>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <asp:UpdatePanel ID="update1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="update2" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
</form>

是的。实际上,这本应该是默认设置,但可惜并非如此。 - RichardOD
毫无疑问,毕竟实现这个功能正是使用UpdatePanel的意义所在... - Aaalf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接