2010-07-12 51 views
1

我想使用GridView编辑更新数据库,更新CommandField。我有两个可编辑的字段,在编辑模式下显示为文本框。点击提交时,我试图将文本框的值放入变量中,但我无法访问它们。两列名称是“EOR”和“CategoryName”。我发现在其他论坛上的几个建议尝试类似:FindControl如何在GridView中工作?

protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    TextBox txtEor = (TextBox)myGridName.Rows[e.RowIndex].FindControl("EOR"); 

当我调试程序时,txtEor始终为空。我能想到的唯一的事情是我没有正确引用单元格。我将Headertext,AccessibleHeaderText,DataField和SortExpression设置为“EOR”,但它仍然是空的。

任何帮助将不胜感激!

ASP为GridView:

<asp:GridView ID="grdEOR" runat="server" BackColor="White" 
      BorderColor="#999999" OnPageIndexChanging="grdEor_PageIndexChanging" 
      BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" 
      AllowPaging="True" 
      PageSize="15" AutoGenerateColumns="False" onrowediting="grdEOR_RowEditing" 
         onrowcancelingedit="grdEOR_RowCancelingEdit" 
         onrowupdating="grdEOR_RowUpdating" onrowdeleting="grdEOR_RowDeleting" 
         ShowFooter="True"> 
      <PagerSettings Mode="NumericFirstLast" /> 
      <Columns> 
       <asp:BoundField DataField="EORCategoryID" HeaderText="EORCategoryID" 
        SortExpression="EORCategoryID" ReadOnly="True"> 
       </asp:BoundField> 
       <asp:BoundField DataField="EOR" HeaderText="EOR" SortExpression="EOR" 
        AccessibleHeaderText="EOR"/> 
       <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
        SortExpression="CategoryName" /> 
       <asp:CommandField ButtonType="Button" ShowDeleteButton="True" 
        ShowEditButton="True" /> 


      </Columns> 
      <FooterStyle BackColor="#CCCCCC" /> 
      <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> 
      <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> 
      <AlternatingRowStyle BackColor="#CCCCCC" BorderColor="Black" 
       BorderStyle="Solid" BorderWidth="5px" /> 
     </asp:GridView> 
+0

可以请你发布你正在试图导航相关的ASP? – luke 2010-07-12 19:46:55

+1

你把文本框放在EditItemTemplate中吗? – onof 2010-07-12 19:50:46

+0

我鼓励你使用ListView,它更容易使用。 – 2010-07-12 21:02:21

回答

1

我终于找到一个可行的办法:

 string newEor = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[1].Controls[0]).Text; 
     string newCategoryName = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 
+0

这很危险,因为它假定boundfield被渲染为文本框。您应该使用EditItemTemplate并在其中放置一个文本框。 – onof 2010-07-13 08:36:01

+0

你知道关于EditItemTemplate如何工作的很好的指南吗?我从来没有用过它。 – Starwfanatic 2010-07-13 12:28:44

+0

以下是一个示例:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.edititemtemplate.aspx – onof 2010-07-13 19:41:13