UpdatePanel中的TextBox会导致PageRequestManagerServerErrorException

时间:2011-08-26 06:29:07

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

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

首先,为了简单起见,我只有UpdatePanel DataList和两个按钮:EditButtonCancelButton({{1}默认情况下隐藏)。 CancelButton的{​​{1}}有两个小组:DataListItemTemplate(默认情况下隐藏ViewPanel)。点击EditPanel后,我会隐藏EditPanelEditButton的{​​{1}}'EditButton,并显示DataListItems } ViewPanel'CancelButton 不是问题。但是,完成此操作后,DataList按钮将无效,抛出Items

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

这是我的代码:

UpdatePanelTest.aspx

CancelButton

UpdatePanelTest.aspx.cs

PageRequestManagerServerErrorException

1 个答案:

答案 0 :(得分: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();
    }