2011-01-20 53 views
0
<asp:GridView ID="GridView1" runat="server" > 

<asp:TemplateField HeaderText="Token" SortExpression="Token" HeaderStyle-Width="100px"> 
        <ItemTemplate>      

        </ItemTemplate> 
       </asp:TemplateField> 

</asp:GridView> 

更新:我怎么能读一个动态创建文本框(gridview的OnRowUpdating)

后,我查看页面thsi的源代码是我所看到的,我已经创建的动态文本框的ID。

ctl00_ContentPlaceHolder1_tabControl_tabUsers_MyControl1_gv_ctl02__token0_3 

OnRowUpdating:

TextBox _token = gvOrg.Rows[e.RowIndex].Cells[7].FindControl("_token " + e.RowIndex + "_" + rowId) as TextBox; 

更新结束:

我加入了一些文本的动态在OnRowDataBound和磨片我尝试所获得的价值,然后我得到空

这里是我的代码:

protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e) 
     {  
      for (int rowId = 0; rowId < 5; rowId++) 
      { 
       TextBox _token = gvOrg.Rows[e.RowIndex].Cells[7].FindControl("_token" + rowId) as TextBox; 
      }  
     } 

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit)) 
      { 

       if (e.Row.RowType == DataControlRowType.DataRow) 
       {            
        for (int rowId = 0; rowId < 5; rowId++) 
        {  
         TextBox txtBox = new TextBox(); 
         txtBox.ID = "_token" + rowId; 
         txtBox.Text = "token" + rowId; 
         e.Row.Cells[7].Controls.Add(txtBox); 
        } 
} 

回答

0

这里是我如何解决这个问题:而不是创建rowdatabound我创建RowCreated,希望这会帮助其他人。

protected void gridviwe1_RowCreated(object sender, GridViewRowEventArgs e) 
     { 

       if (e.Row.RowType == DataControlRowType.DataRow) 
       { 
        for (int rowId = 0; rowId < 5; rowId++) 
        { 
         TextBox txtBox = new TextBox(); 
         txtBox.ID = "_registration" + e.Row.RowIndex + "_" + rowId; 
         txtBox.Text = "_registration" + e.Row.RowIndex + "_" + rowId; 
         e.Row.Cells[7].Controls.Add(txtBox); 
        } 
       } 
      } 
0

您正在为每行创建文本框 - 其中5个......并且在每一行中,这些文本框中的每一个都与其他行具有相同的id。例如,您需要在行的索引处创建文本框的名称。您无法使用相同的ID在页面上进行控制,否则无法正确找到它。

这是一种方法。

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit)) 
      { 

       if (e.Row.RowType == DataControlRowType.DataRow) 
       {            
        for (int rowId = 0; rowId < 5; rowId++) 
        {  
         TextBox txtBox = new TextBox(); 
         txtBox.ID = "_token" + e.Row.RowIndex + "_" + rowId; 
         txtBox.Text = "token" + rowId; 
         e.Row.Cells[7].Controls.Add(txtBox); 
        } 
     } 

我无法测试这是完整的解决方案,但它是一个开始的地方。

+0

好赶上,我没有更新我的代码,并更新我的`OnRowUpdating`,但仍然变空 - 我更新了我的问题。 – 2011-01-20 20:23:45

+0

为我发布你的aspx代码,我会为你修复它。 – TheGeekYouNeed 2011-01-20 23:37:06