2013-02-24 72 views
0

我正在使用linq到實體我已經添加了gridview,但它不是編輯,當我調試它不訪問方法GridView5_RowUpdating()..在這裏它是代碼的網格視圖我不能在gridview編輯

<asp:GridView ID="GridView5" runat="server" AllowSorting="True" 
      AutoGenerateColumns="False" CellPadding="4" DataKeyNames="CustomerId" 
      DataSourceID="SqlDataSource3" ForeColor="#333333" GridLines="None" 
      onrowupdating="GridView5_RowUpdating"> 
      <AlternatingRowStyle BackColor="White" /> 
      <Columns> 
       <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
       <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" 
        InsertVisible="False" ReadOnly="True" SortExpression="CustomerId" /> 
       <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
        SortExpression="FirstName" /> 
       <asp:BoundField DataField="LastName" HeaderText="LastName" 
        SortExpression="LastName" /> 
       <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /> 
      </Columns> 
      <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> 
      <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> 
      <SortedAscendingCellStyle BackColor="#FDF5AC" /> 
      <SortedAscendingHeaderStyle BackColor="#4D0000" /> 
      <SortedDescendingCellStyle BackColor="#FCF6C0" /> 
      <SortedDescendingHeaderStyle BackColor="#820000" /> 
     </asp:GridView> 

我有必要的字段驗證當我試圖手動插入時,我刪除了必填字段驗證有人可以告訴我發生了什麼?

回答

0

首先,你需要實現在GridView中RowEditing事件進行編輯,MSDN定義:

單擊某一行的編輯按鈕時發生,但 前GridView控件進入編輯模式。

ASPX:

<asp:GridView 
     ID="gvCustomers" 
     runat="server" 
     OnRowEditing="gvCustomers_RowEditing"> 

後面的代碼:

protected void gvCustomers_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    gvCustomers.EditIndex = e.NewEditIndex; 
    //Re bind the grid view 
} 

現在,如果當你點擊你的網格編輯鏈路中,該事件不會被解僱查看意味着頁面上有一些驗證邏輯阻止回發。

擺脫這一問題的最好辦法是設置ValidationGroup屬性,用於驗證控件造成麻煩(不是GridView控件):

<div id="insertEmployee"> 
     <asp:TextBox ID="txtName" runat="server" ValidationGroup="Insert" /> 
     <asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtName" ErrorMessage="Name is required" ValidationGroup="Insert" /> 
     <asp:Button ID="btnAdd" runat="server" Text="Add" ValidationGroup="Insert" /> 
    </div>