GridView编辑按钮导致异常
本文关键字:异常 按钮 编辑 编辑按 GridView | 更新日期: 2023-09-27 18:00:28
我有一个delete
和edit
按钮的命令模板。
当我单击edit
按钮并为每个字段填写正确数据类型的数据时,这将非常有效。
然而,当我在字段中填写错误数据类型的数据时,网页浏览器上将触发异常。我该如何纠正这一点,你们背后的任何代码都可以建议,把它放在哪里?
<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>
您可以对该特定控件进行验证,而不会生成异常。
您可以覆盖允许编辑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>
请注意第二个绑定字段是只读的,这将使其无法编辑。第4列中的命令字段显示一个按钮"编辑",该按钮将绑定字段更改为文本框。
在后端代码中,您可以通过访问GridViewUpdateEventArgs参数的"NewValues"字典来获取新值。