2010-07-12 40 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

相關問題