GridView编辑按钮导致异常

发布于 2024-11-16 20:13:25 字数 4056 浏览 6 评论 0原文

我有一个用于 deleteedit 按钮的命令模板。

当我单击编辑按钮并为每个字段填写正确数据类型的数据时,这将工作得很好。

但是,当我在字段中填写错误数据类型的数据时,网页浏览器上会触发异常。我该如何纠正这个问题,你们背后的任何代码都可以建议,以及将其放在哪里?

<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" 
                    CellPadding="4" DataKeyNames="ProductCode" DataSourceID="SqlProductmaster" 
                    ForeColor="#333333" GridLines="None">
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <Columns>
                        <asp:BoundField DataField="ProductCode" HeaderText="ProductCode" 
                            ReadOnly="True" SortExpression="ProductCode" />
                        <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                            SortExpression="ProductName" />
                        <asp:BoundField DataField="Category" HeaderText="Category" 
                            SortExpression="Category" />
                        <asp:BoundField DataField="SellingPrice" HeaderText="SellingPrice" 
                            SortExpression="SellingPrice" />
                        <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
                            SortExpression="Quantity" />
                        <asp:BoundField DataField="BrandName" HeaderText="BrandName" 
                            SortExpression="BrandName" />
                        <asp:BoundField DataField="ReOrderQty" HeaderText="ReOrderQty" 
                            SortExpression="ReOrderQty" />
                        <asp:BoundField DataField="ReOrderLevel" HeaderText="ReOrderLevel" 
                            SortExpression="ReOrderLevel" />
                        <asp:BoundField DataField="Ordered" HeaderText="Ordered" 
                            SortExpression="Ordered" />
                        <asp:BoundField DataField="Allocated" HeaderText="Allocated" 
                            SortExpression="Allocated" />
                        <asp:BoundField DataField="FreeQty" HeaderText="FreeQty" 
                            SortExpression="FreeQty" />
                        <asp:TemplateField ShowHeader="False">
                            <EditItemTemplate>
                                <asp:Button ID="Update" runat="server" CausesValidation="True" 
                                    CommandName="Update" Text="Update" />
                                &nbsp;<asp:Button ID="Cancel" runat="server" CausesValidation="False" 
                                    CommandName="Cancel" Text="Cancel" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Button ID="edit" runat="server" CausesValidation="False" 
                                    CommandName="Edit" Text="Edit" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:Button ID="Delete" runat="server" CausesValidation="False" 
                                    CommandName="Delete" Text="Delete" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#990000" Font-Bold="false" ForeColor="White" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="False" ForeColor="Navy" />
                    <HeaderStyle BackColor="#990000" Font-Bold="False" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView> 

I have a command template for delete and edit button.

When I click the edit button and fill in data of correct datatype for each field, this will work just fine.

However, when I fill in data of wrong datatype into the fields, an exception will trigger on web page browser. How can I to rectify this, any code behind you guys can suggest, and where to put it?

<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" 
                    CellPadding="4" DataKeyNames="ProductCode" DataSourceID="SqlProductmaster" 
                    ForeColor="#333333" GridLines="None">
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <Columns>
                        <asp:BoundField DataField="ProductCode" HeaderText="ProductCode" 
                            ReadOnly="True" SortExpression="ProductCode" />
                        <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                            SortExpression="ProductName" />
                        <asp:BoundField DataField="Category" HeaderText="Category" 
                            SortExpression="Category" />
                        <asp:BoundField DataField="SellingPrice" HeaderText="SellingPrice" 
                            SortExpression="SellingPrice" />
                        <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
                            SortExpression="Quantity" />
                        <asp:BoundField DataField="BrandName" HeaderText="BrandName" 
                            SortExpression="BrandName" />
                        <asp:BoundField DataField="ReOrderQty" HeaderText="ReOrderQty" 
                            SortExpression="ReOrderQty" />
                        <asp:BoundField DataField="ReOrderLevel" HeaderText="ReOrderLevel" 
                            SortExpression="ReOrderLevel" />
                        <asp:BoundField DataField="Ordered" HeaderText="Ordered" 
                            SortExpression="Ordered" />
                        <asp:BoundField DataField="Allocated" HeaderText="Allocated" 
                            SortExpression="Allocated" />
                        <asp:BoundField DataField="FreeQty" HeaderText="FreeQty" 
                            SortExpression="FreeQty" />
                        <asp:TemplateField ShowHeader="False">
                            <EditItemTemplate>
                                <asp:Button ID="Update" runat="server" CausesValidation="True" 
                                    CommandName="Update" Text="Update" />
                                 <asp:Button ID="Cancel" runat="server" CausesValidation="False" 
                                    CommandName="Cancel" Text="Cancel" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Button ID="edit" runat="server" CausesValidation="False" 
                                    CommandName="Edit" Text="Edit" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:Button ID="Delete" runat="server" CausesValidation="False" 
                                    CommandName="Delete" Text="Delete" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#990000" Font-Bold="false" ForeColor="White" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="False" ForeColor="Navy" />
                    <HeaderStyle BackColor="#990000" Font-Bold="False" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView> 

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

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

发布评论

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

评论(2

霓裳挽歌倾城醉 2024-11-23 20:13:26

您可以对该特定控件进行验证,并且永远不会生成异常。

you can put validation for that particular control and never exception generated.

尘曦 2024-11-23 20:13:26

您可以覆盖允许您编辑 BoundFields 的函数,而不是使用模板字段。为此,请尝试如下操作:

<asp:GridView runat="server" ID="gvCurrentLocations" ... OnRowEditing="rowEditing" OnRowCancelingEdit="rowCancel" OnRowUpdating="rowUpdating">
    <Columns>
        <asp:BoundField DataField="locationname" HeaderText="Location Name" />
        <asp:TemplateField HeaderText="Action">
            <ItemTemplate>
                <asp:CheckBox ID="cbToRemove" runat="server" CssClass="remove" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="boundpdas" HeaderText="Assigned To" ReadOnly="true" />
        <asp:CommandField ShowEditButton="true" UpdateText="Save"  />
    </Columns>
    <EmptyDataTemplate>
        There are currently no locations.
    </EmptyDataTemplate>
</asp:GridView>

注意第二个绑定字段是只读的,这将阻止它可编辑。第四列中的命令字段显示一个按钮“编辑”,该按钮将绑定字段更改为文本框。

在后端代码中,您可以通过访问 GridViewUpdateEventArgs 参数的“NewValues”字典来获取新值。

Instead of using template fields, you can override functions which allow you to edit BoundFields. To do this, try something like this:

<asp:GridView runat="server" ID="gvCurrentLocations" ... OnRowEditing="rowEditing" OnRowCancelingEdit="rowCancel" OnRowUpdating="rowUpdating">
    <Columns>
        <asp:BoundField DataField="locationname" HeaderText="Location Name" />
        <asp:TemplateField HeaderText="Action">
            <ItemTemplate>
                <asp:CheckBox ID="cbToRemove" runat="server" CssClass="remove" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="boundpdas" HeaderText="Assigned To" ReadOnly="true" />
        <asp:CommandField ShowEditButton="true" UpdateText="Save"  />
    </Columns>
    <EmptyDataTemplate>
        There are currently no locations.
    </EmptyDataTemplate>
</asp:GridView>

Notice how the 2nd bound field is readonly, this will prevent it being editable. The command field in the 4th column exposes a button 'Edit' which changes the bound field to a text box.

In your backend code, you can get the new values by accessing the 'NewValues' dictionary of the GridViewUpdateEventArgs parameter.

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