2011-06-24 85 views
1

我有一個用於deleteedit按鈕的命令模板。GridView編輯按鈕導致異常

當我單擊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" /> 
           &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> 
+0

請發送例外 –

+0

什麼是例外名稱?也許你需要寫一個事件。 –

+0

@Marco抱歉Marco,現在異常消失了,但是出現了其他問題。似乎更新按鈕不會觸發任何東西,當我click.cancel和刪除工作正常。奇怪的? –

回答

0

您可以對該特定控件進行驗證,並且不會生成異常。

0

除了使用模板字段,您還可以覆蓋允許您編輯BoundField的函數。要做到這一點,請嘗試如下所示:

<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'字典來獲取新值。