【问题标题】:TextBoxes in UpdatePanel are causing PageRequestManagerServerErrorExceptionUpdatePanel 中的文本框导致 PageRequestManagerServerErrorException
【发布时间】:2011-08-29 09:02:33
【问题描述】:

我正在尝试创建一个页面,该页面在数据绑定标签中包含一些信息,并带有编辑按钮。单击编辑按钮时,信息将替换为绑定到相同数据的文本框。然后可以修改数据,将其保存回数据库,并将文本框替换为更新的标签。

首先,为了简单起见,我只有一个UpdatePanel 和一个DataList 和两个按钮:EditButtonCancelButtonCancelButton 默认隐藏)。 DataListItemTemplate 有两个面板:ViewPanelEditPanelEditPanel 默认隐藏)。单击EditButton 时,我隐藏EditButtonDataListItems'ViewPanel,并显示CancelButtonDataListItems'EditPanel
不是问题。但是,一旦完成此操作,CancelButton 按钮将不起作用,并抛出 PageRequestManagerServerErrorException

通过一些摆弄,我发现这是因为EditPanel 上有数据绑定文本框。如果我不将数据绑定到文本框,一切都会完美运行。为什么这不起作用?

这是我的代码:

UpdatePanelTest.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UpdatePanelTest.aspx.cs" Inherits="WebLetterViewer.UpdatePanelTest" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
    <asp:SqlDataSource ID="AllLettersDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ORMSTestConnectionString %>" 
        SelectCommand="SELECT * FROM [Letters] WHERE ([id] = @id)">
        <SelectParameters>
            <asp:ControlParameter ControlID="HiddenLetterID" DefaultValue="1" Name="id" PropertyName="Value" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:HiddenField ID="HiddenLetterID" runat="server" Value="1" />
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
            <ContentTemplate>
                <asp:DataList ID="LettersDataList" runat="server" DataSourceID="AllLettersDataSource">
                    <ItemTemplate>
                        <asp:Panel ID="ViewPanel" runat="server">
                            <h2>Data1:</h2>
                            <asp:Label ID="data1Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data1") %>' Width="500px" />
                            <h2>data2:</h2>
                            <asp:Label ID="data2Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data2") %>' Width="500px" />
                            <h2>data3:</h2>
                            <asp:Label ID="data3Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data3") %>' Width="500px" />
                        </asp:Panel>
                        <asp:Panel ID="EditPanel" runat="server" Visible="False">
                            <h2>data1:</h2>
                            <asp:TextBox ID="data1TextBox" runat="server" Height="100px" Text='<%# Eval("data1", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox>
                            <h2>data2:</h2>
                            <asp:TextBox ID="data2TextBox" runat="server" Height="100px" Text='<%# Eval("data2", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox>
                            <h2>data3:</h2>
                            <asp:TextBox ID="data3TextBox" runat="server" Height="100px" Text='<%# Eval("data3", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox>
                        </asp:Panel>
                    </ItemTemplate>
                </asp:DataList>
                <asp:Button ID="EditButton" runat="server" onclick="EditButton_Click" Text="Edit" />
                <asp:Button ID="CancelButton" runat="server" onclick="CancelButton_Click" Text="Cancel" Visible="False" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

UpdatePanelTest.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebLetterViewer{
    public partial class UpdatePanelTest : System.Web.UI.Page{
        protected void Page_Load(object sender, EventArgs e){
        }

        protected void EditButton_Click(object sender, EventArgs e){
            foreach (DataListItem item in LettersDataList.Items){
                item.FindControl("ViewPanel").Visible = false;
                item.FindControl("EditPanel").Visible = true;
            }
            EditButton.Visible = false;
            CancelButton.Visible = true;
            UpdatePanel1.Update();
        }

        protected void CancelButton_Click(object sender, EventArgs e){
            foreach (DataListItem item in LettersDataList.Items){
                item.FindControl("ViewPanel").Visible = true;
                item.FindControl("EditPanel").Visible = false;
            }
            EditButton.Visible = true;
            CancelButton.Visible = false;
            UpdatePanel1.Update();
        }
    }
}

【问题讨论】:

  • 异常是否包含任何消息?也许是一些细节或 innerException?

标签: c# asp.net data-binding updatepanel


【解决方案1】:

将您的 EditPanel 放入 EditItemTemplate 并使用 Commands ,您并没有按照设计使用的方式使用此控件:

How to: Allow Users to Edit Items in DataList Web Server Controls

标记:

<asp:SqlDataSource ID="AllLettersDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORMSTestConnectionString %>"
        SelectCommand="SELECT * FROM [Letters] WHERE ([id] = @id)">
        <SelectParameters>
            <asp:ControlParameter ControlID="HiddenLetterID" DefaultValue="1" Name="id" PropertyName="Value"
                Type="Int32" />
        </SelectParameters>
        <!--change this -->
        UpdateCommand="UPDATE [Categories] SET [CategoryName] = @CategoryName, [Description]
        = @Description WHERE [CategoryID] = @original_CategoryID">
        <UpdateParameters>
            <asp:Parameter Name="CategoryName" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="original_CategoryID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:DataList ID="LettersDataList" runat="server" DataSourceID="AllLettersDataSource"
        OnEditCommand="LettersDataList_EditCommand" OnCancelCommand="LettersDataList_CancelCommand"
        OnUpdateCommand="LettersDataList_UpdateCommand">
        <ItemTemplate>
            <asp:Panel ID="ViewPanel" runat="server">
                <h2>
                    Data1:</h2>
                <asp:Label ID="data1Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px"
                    Text='<%# Eval("data1") %>' Width="500px" />
                <h2>
                    data2:</h2>
                <asp:Label ID="data2Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px"
                    Text='<%# Eval("data2") %>' Width="500px" />
                <h2>
                    data3:</h2>
                <asp:Label ID="data3Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px"
                    Text='<%# Eval("data3") %>' Width="500px" />
            </asp:Panel>
            <asp:Button ID="EditButton" runat="server" CommandName="edit" Text="Edit" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:Panel ID="EditPanel" runat="server">
                <h2>
                    data1:</h2>
                <asp:TextBox ID="data1TextBox" runat="server" Height="100px" Text='<%# Eval("data1", "{0}") %>'
                    TextMode="MultiLine" Width="500px"></asp:TextBox>
                <h2>
                    data2:</h2>
                <asp:TextBox ID="data2TextBox" runat="server" Height="100px" Text='<%# Eval("data2", "{0}") %>'
                    TextMode="MultiLine" Width="500px"></asp:TextBox>
                <h2>
                    data3:</h2>
                <asp:TextBox ID="data3TextBox" runat="server" Height="100px" Text='<%# Eval("data3", "{0}") %>'
                    TextMode="MultiLine" Width="500px"></asp:TextBox>
            </asp:Panel>
            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="update">
                    Save
            </asp:LinkButton>
            &nbsp;
            <asp:Button ID="CancelButton" runat="server" CommandName="cancel" Text="Cancel" Visible="False" />
        </EditItemTemplate>
    </asp:DataList>

代码隐藏:

    protected void LettersDataList_EditCommand(object source, DataListCommandEventArgs e)
    {
        LettersDataList.EditItemIndex = e.Item.ItemIndex;
        LettersDataList.DataBind();
    }

    protected void LettersDataList_CancelCommand(object source,
        DataListCommandEventArgs e)
    {
        LettersDataList.EditItemIndex = -1;
        LettersDataList.DataBind();
    }

    protected void LettersDataList_UpdateCommand(object source,
        DataListCommandEventArgs e)
    {
        //change this to your database needs

        //String categoryID =
        //     LettersDataList.DataKeys[e.Item.ItemIndex].ToString();
        //String categoryName =
        //     ((TextBox)e.Item.FindControl("textCategoryName")).Text;
        //String description =
        //     ((TextBox)e.Item.FindControl("textDescription")).Text;

        //SqlDataSource1.UpdateParameters["original_CategoryID"].DefaultValue
        //    = categoryID;
        //SqlDataSource1.UpdateParameters["categoryName"].DefaultValue
        //    = categoryName;
        //SqlDataSource1.UpdateParameters["Description"].DefaultValue
        //    = description;
        //SqlDataSource1.Update();

        LettersDataList.EditItemIndex = -1;
        LettersDataList.DataBind();
    }

【讨论】:

  • 谢谢你,我以为我做错了什么,你的回答很有帮助。我还发现我的文本框中有 HTML,并且验证阻止了表单提交。事后看来,这就是问题所在,所以技术上这不是我问题的答案,但正如我所说,它有很大帮助,也向我揭示了真正的问题。
  • 酷,很高兴它有帮助,我以前从未见过有人这样使用它,我学到了一些新东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 2012-05-08
相关资源
最近更新 更多