Ajax 回调 UpdatePanel.Update() 仍在重新加载整个页面

发布于 2024-07-08 14:56:57 字数 1233 浏览 7 评论 0原文

我在更新面板中有代码,即使单击按钮,我也将数据插入数据库并简单地调用 Updatepanel.Update() ,整个页面都会重新加载:

Gifts.ASPX

<table style="width:100%;">
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Gift"></asp:Label>
                </td>
                <td>
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
                    <asp:TextBox ID="txtNewGift" runat="server"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>
                </td>
            </tr>
            <tr>

Gifts.aspx.CS

protected void cmdAddGift_Click(object sender, EventArgs e)
{
    OleDbConnection objConn = new OleDbConnection(DataSource);

    Random r = new Random();
    int giftID = r.Next(1200, 14000);

    OleDbCommand objCommand = new OleDbCommand("Insert into Gifts (GiftID, Description) values (" + giftID + ",'" + txtNewGift.Text + "')", objConn);
    ExecuteCommand(objCommand);

    PopulateGifts(objConn);

    txtNewGift.Text = "";
    UpdatePanel3.Update();
}

任何想法为什么整个页面会重新加载而不仅仅是文本框得到更新?

I have code in an Update Panel and even though on a button click i am inserting data into a db and simply calling Updatepanel.Update() the whole page is reloaded:

Gifts.ASPX

<table style="width:100%;">
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Gift"></asp:Label>
                </td>
                <td>
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
                    <asp:TextBox ID="txtNewGift" runat="server"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>
                </td>
            </tr>
            <tr>

Gifts.aspx.CS

protected void cmdAddGift_Click(object sender, EventArgs e)
{
    OleDbConnection objConn = new OleDbConnection(DataSource);

    Random r = new Random();
    int giftID = r.Next(1200, 14000);

    OleDbCommand objCommand = new OleDbCommand("Insert into Gifts (GiftID, Description) values (" + giftID + ",'" + txtNewGift.Text + "')", objConn);
    ExecuteCommand(objCommand);

    PopulateGifts(objConn);

    txtNewGift.Text = "";
    UpdatePanel3.Update();
}

Any ideas why this whole page would reload instead of just the textbox getting update?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

意犹 2024-07-15 14:56:57

上例中的按钮在哪里? UpdatePanel 内部或外部。 如果它在外部,您需要将其添加到 UpdatePanel 的触发器集合中。

此外,如果您要更改 UpdatePanel 的内容而不是导致(部分)回发的内容,则只需调用 UpdatePanel.Update() 即可。

作为旁注(和个人改革),建议在数据库连接周围放置一个 using 语句。

使用下面的标记,将发生以下情况:

  • btnInnerPart 位于更新面板内,因此它将自动导致部分回发
  • btnInnerFull 将导致完整回发,因为它在触发器集合中具有 PostBackTrigger
  • btnOuterPart 将导致部分回发,因为它有触发器集合
  • btnOuterFull 中的 AsyncPostBackTrigger 将导致完整回发,因为它位于 UpdatePanel

标记之外:

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <!-- Content -->
        <asp:Button runat="server" ID="btnInnerPart" Text="Inner Part" />
        <asp:Button runat="server" ID="btnInnerFull" Text="Inner Full" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnOuterPart" />
        <asp:PostBackTrigger ControlID="btnInnerFull" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button runat="server" ID="btnOuterFull" Text="Outer Full" />
<asp:Button runat="server" ID="btnOuterPart" Text="Outer Part" />

Where is the button in the above example? Inside or outside the UpdatePanel. If it is outside you will need to add it to the triggers collection of the UpdatePanel.

Also you only need to call UpdatePanel.Update() if you are changing the content of an UpdatePanel other than the one that caused the (Partial) postback.

As a side note (and personal crusade), it is recommended that a using statement is put around your DB connection.

With the markup below, the following will happen:

  • btnInnerPart is inside the update panel, so it will automatically cause a partial postback
  • btnInnerFull will cause a full postback as it has a PostBackTrigger in the trigger collection
  • btnOuterPart will cause a partial postback as it has an AsyncPostBackTrigger in the trigger collection
  • btnOuterFull will cause a full postback as it is outside the UpdatePanel

Markup:

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <!-- Content -->
        <asp:Button runat="server" ID="btnInnerPart" Text="Inner Part" />
        <asp:Button runat="server" ID="btnInnerFull" Text="Inner Full" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnOuterPart" />
        <asp:PostBackTrigger ControlID="btnInnerFull" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button runat="server" ID="btnOuterFull" Text="Outer Full" />
<asp:Button runat="server" ID="btnOuterPart" Text="Outer Part" />
梦幻之岛 2024-07-15 14:56:57

Gifts.ASPX 上的按钮在哪里? 如果您将按钮放在 UpdatePanel 内或使用触发器,则不需要调用 UpdatePanel3.Update(); 从后面的代码来看。

Where is the button on Gifts.ASPX? If you put the button inside the UpdatePanel or use triggers you don't need to call UpdatePanel3.Update(); from the code behind.

苏佲洛 2024-07-15 14:56:57

另外,您的页面上需要有一个 ScriptManager 对象。 你是否有一个?

Also, You need to have a ScriptManager object on your page. Do you have one?

屋檐 2024-07-15 14:56:57

请检查更新面板的标签...您必须指定更新面板的触发控件,更新面板将在其上进行更新

please check tag of update panel...you have to specify the trigger controls for update panel on on which the update panel will get update

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文