0
 <asp:GridView ID="GridView1" runat="server"  >

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

                    </ItemTemplate> 
                </asp:TemplateField>

</asp:GridView> 

更新:

在我查看页面 thsi 的源代码后,我看到了我创建的动态文本框的 id。

ctl00_ContentPlaceHolder1_tabControl_tabUsers_MyControl1_gv_ctl02__token0_3

OnRow 更新:

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

更新结束:

我在 OnRowDataBound 中添加了一些动态文本框,当我尝试获取值时,我得到了 null

这是我的代码:

 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);  
                    } 
}
4

2 回答 2

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);
                    }
                }
            } 
于 2011-01-23T03:16:40.163 回答
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);  
                    } 
        }

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

于 2011-01-20T19:39:25.930 回答