开发者

GridView edit button cause exception

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" />
                                &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> 


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜