GridView编辑按钮导致异常
我有一个用于 delete
和 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>
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以对该特定控件进行验证,并且永远不会生成异常。
you can put validation for that particular control and never exception generated.
您可以覆盖允许您编辑 BoundFields 的函数,而不是使用模板字段。为此,请尝试如下操作:
注意第二个绑定字段是只读的,这将阻止它可编辑。第四列中的命令字段显示一个按钮“编辑”,该按钮将绑定字段更改为文本框。
在后端代码中,您可以通过访问 GridViewUpdateEventArgs 参数的“NewValues”字典来获取新值。
Instead of using template fields, you can override functions which allow you to edit BoundFields. To do this, try something like this:
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.