2012-01-29 9 views
0

基本上我想檢索一個「TextBox1」,我將它添加到gridview的新列(TemplateField)之一中,其餘部分該列是來自sqldatasource控件的數據加載如何在gridview中檢索「TextBox1」,並在aspx加載時指示它的值

我想要指出我計算的「TextBox1」基本值...所以結果是當default.aspx加載(意味着頁面加載)「TextBox1 「應該顯示計算值...現在是我現在是怎麼從gridview檢索」TextBox1「,以便我指出一個值

因爲我不需要使用下面的代碼作爲我的gridview數據使用sqldata中的檢索源設計的Default.aspx

DataTable dt = new DataTable(); 
dt.Columns.Add("Name"); 
dt.Columns.Add("Age"); 

DataRow dr = dt.NewRow(); 
dr["Name"] = "Chris Harris"; 
dr["Age"] = "40"; 
dt.Rows.Add(dr); 

dr = dt.NewRow(); 
dr["Name"] = "Sean Williams"; 
dr["Age"] = "39"; 
dt.Rows.Add(dr); 

dr = dt.NewRow(); 
dr["Name"] = "Paul Newcombe"; 
dr["Age"] = "38"; 
dt.Rows.Add(dr); 

GridView1.DataSource = dt; 
GridView1.DataBind(); 

SO應該怎麼做來檢索gridview的說,「TextBox1的」爲了讓我指示值

<ItemTemplate> 
       <table style="width: 73%; height: 31px;"> 
        <tr> 
         <td class="style1"> 
          <asp:Label ID="Label2" runat="server" Text="Calculation:"></asp:Label> 
         </td> 
         <td > 
          <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
         </td> 
        </tr> 

       </table> 
      </ItemTemplate> 

回答

0

如果一個行由行的情況下,你可能會想這樣做是在GridView中RowDataBound事件:

public void gridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 

    if (e.Row.RowType == DataControlRowType.DataRow) { 
     Textbox txt = (TextBox)e.Row.FindControl("Textbox1"); 
     //Do your processing here... 

    } 
} 
+0

感謝它的工作!!!! – actKing 2012-01-29 07:07:12

+0

真棒新聞! :) – Evan 2012-01-29 07:08:08

0

你需要找到在GridView內部控制RowDatabound事件中的容器。 Gridview class and methods

TextBox t = GridView1.FindControl("TextBox1") as TextBox; 
      if (t != null) 
      { 
       t.Text = "Hello World!"; 
      } 
相關問題