2014-10-18 73 views
0

我將數組綁定到GridView。以下是我的模板字段,當點擊更新時它不會觸發RowUpdating。RowUpdating在EditItemTemplate中未觸發

<asp:TemplateField HeaderText="Role"> 
       <EditItemTemplate> 
        <asp:TextBox runat="server" Text='<%# Container.DataItem.ToString() %>' ID="txtEditRole"></asp:TextBox> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <%# Container.DataItem.ToString() %> 
       </ItemTemplate> 
      </asp:TemplateField> 

發生這種情況後,使領域TempleteField。早些時候,該領域如下所示。

<asp:BoundField DataField="!" HeaderText="Role" /> 
+0

你在gridview中使用'OnRowUpdating'事件http://www.webblogsforyou.com/gridview-inline-add-insert-edit-update-delete-data-in-asp-net-c-vb/像這樣? – 2014-10-19 04:45:37

+0

是的,我有OnRowUpdating事件 – user966398 2014-10-20 01:37:27

回答

0

確保在<%#Eval("Role") %>指定OnRowUpdating="gv_RowUpdating"事件,改變字段名,看下面的例子:

.aspx頁面中

<asp:GridView ID="gv" runat="server" DataKeyNames="Id" AutoGenerateColumns="false" OnRowEditing="gv_RowEditing"  
OnRowUpdating="gv_RowUpdating" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowCommand="gv_RowCommand" OnRowDeleting="gv_RowDeleting"> 
<Columns> 
    <asp:TemplateField> 
    <EditItemTemplate> 
     <asp:LinkButton ID="lbtnUpdate" runat="server" CommandName="Update" Text="Update" /> 
     <asp:LinkButton ID="lbtnCancel" runat="server" CommandName="Cancel" Text="Cancel" /> 
    </EditItemTemplate> 
    <ItemTemplate> 
     <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" Text="Edit" /> 
     <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" CausesValidation="false" /> 
    </ItemTemplate>  
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Role"> 
    <EditItemTemplate> 
     <asp:TextBox ID="txtEditRole" runat="server" Text='<%#Eval("Role") %>' /> 
    </EditItemTemplate> 
    <ItemTemplate> 
     <asp:Label ID="lblRole" runat="server" Text='<%#Eval("Role") %>' /> 
    </ItemTemplate>  
    </asp:TemplateField> 
</Columns> 
</asp:GridView> 

.aspx.cs

protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    //your code here.. 
} 

要檢查完整的文章,結帳insert, update, delete gridview data example in asp.net

+0

這不起作用,因爲我設置數據源如下。 gridViewRoles.DataSource = Roles.GetAllRoles();所以它給出了一個錯誤'System.String'不包含名稱爲'Role'的屬性。 – user966398 2014-10-20 21:07:39

+0

這是因爲從「Roles.GetAllRoles();」返回的結果集中找不到「角色」字段。確保結果集中返回「角色」字段並將其作爲列表傳遞。 – 2014-10-21 04:07:00