C# - 无法在gridview中读取动态创建的文本框 [英] C# - cannot read dynamically created textbox in gridview
问题描述
嗨
我在gridview中以编程方式创建文本框。但是我无法从文本框中获取值。
我有一个文本框,我可以在其中获取要创建的行数并按下Go 按钮,它用动态创建的文本框填充gridview。
我需要读取gridview文本框中的值,这就是我被卡住的地方。
我的网格视图:
< asp:GridView ID =gvPlanrunat =server ShowFooter =TrueAutoGenerateColumns =true>
< Columns>
< / Columns>
< / asp:GridView>
代码页:
protected void btnGo_Click(object sender,EventArgs e)
{
Insert_Grid();
}
private void Insert_Grid()
{
CreateGridView();
int numbers = int.Parse(txtRows.Text.Trim());
int cellCount = gvPlan.Rows [0] .Cells.Count;
int rowsCount = gvPlan.Rows.Count;
foreach(gvPlan.Rows中的GridViewRow行)
{
//文本框 - ID
TextBox txtID = new TextBox();
txtID.ID =tID+(Convert.ToInt32(row.RowIndex + 1))。ToString();
txtID.Attributes.Add(runat,server);
row.Cells [1] .Controls.Add(txtID);
//文本框 - 名称
TextBox txtName = new TextBox();
txtName.ID =tName+(Convert.ToInt32(row.RowIndex + 1))。ToString();
txtName.Attributes.Add(runat,server);
txtName.Attributes.Add(ReadOnly,true);
row.Cells [2] .Controls.Add(txtName);
}
}
private void CreateGridView()
{
int numbers = int.Parse(this.txtRows.Text.Trim());
DataTable dt = new DataTable();
dt.Columns.Add(ID,typeof(string));
dt.Columns.Add(Name,typeof(string));
for(int i = 0; i< numbers; i ++)
{
dt.Rows.Add(,);
}
ViewState [CurrentTable] = dt;
gvPlan.DataSource = dt;
gvPlan.DataBind();
}
protected void btnSave_Click(object sender,EventArgs e)
{
int rowIndex = 0;
if(ViewState [CurrentTable]!= null)
{
DataTable dtCurrentTable =(DataTable)ViewState [CurrentTable];
if(dtCurrentTable.Rows.Count> 0)
{
for(int i = 1; i< = dtCurrentTable.Rows.Count; i ++)
{
//提取TextBox值
TextBox gtxtID =(TextBox)gvPlan.Rows [rowIndex] .Cells [1] .FindControl(tID+(rowIndex + 1));
TextBox gtxtName =(TextBox)gvPlan.Rows [rowIndex] .Cells [2]。.FindControl(tName+(rowIndex + 1));
gtxtName.Text = gtxtID.Text;
rowIndex ++;
}
}
}
问题出现在btnSave_Click程序中,我似乎没有从gtxtID获取值。它找不到文本框控件,对象为空
非常感谢您的帮助。
问候
Ilyas
我的尝试:
按代码尝试调试代码但是徒劳无法
这里的答案可能会有所帮助:如何在gridview中读取动态创建的文本框 [ ^ ]
Hi
I 'm programatically creating text boxes in gridview. But I'm unable to get the value from the text box.
I have a text box where i get the number of rows to be created and on pressing "Go" button, it populates gridview with dynamically created text boxes.
I need to read the values from gridview text boxes and this is where i got stuck.
My Gridview:
<asp:GridView ID="gvPlan" runat="server" ShowFooter="True" AutoGenerateColumns="true"> <Columns> </Columns> </asp:GridView>
Code Page:
protected void btnGo_Click(object sender, EventArgs e) { Insert_Grid(); } private void Insert_Grid() { CreateGridView(); int numbers = int.Parse(txtRows.Text.Trim()); int cellCount = gvPlan.Rows[0].Cells.Count; int rowsCount = gvPlan.Rows.Count; foreach (GridViewRow row in gvPlan.Rows) { // text box - ID TextBox txtID = new TextBox(); txtID.ID = "tID" + (Convert.ToInt32(row.RowIndex + 1)).ToString(); txtID.Attributes.Add("runat", "server"); row.Cells[1].Controls.Add(txtID); // text box - NAME TextBox txtName = new TextBox(); txtName.ID = "tName" + (Convert.ToInt32(row.RowIndex + 1)).ToString(); txtName.Attributes.Add("runat", "server"); txtName.Attributes.Add("ReadOnly", "true"); row.Cells[2].Controls.Add(txtName); } } private void CreateGridView() { int numbers = int.Parse(this.txtRows.Text.Trim()); DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(string)); dt.Columns.Add("Name", typeof(string)); for (int i = 0; i < numbers; i++) { dt.Rows.Add("", ""); } ViewState["CurrentTable"] = dt; gvPlan.DataSource = dt; gvPlan.DataBind(); } protected void btnSave_Click(object sender, EventArgs e) { int rowIndex = 0; if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; if (dtCurrentTable.Rows.Count > 0) { for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) { //extract the TextBox values TextBox gtxtID = (TextBox)gvPlan.Rows[rowIndex].Cells[1].FindControl("tID" + (rowIndex+1)); TextBox gtxtName = (TextBox)gvPlan.Rows[rowIndex].Cells[2].FindControl("tName" + (rowIndex + 1)); gtxtName.Text = gtxtID.Text; rowIndex++; } } }
The issue is in "btnSave_Click" procedure where i dont seem to get the value from gtxtID. It cannot find the text box control and the object is null
Your help is much appreciated.
Regards
Ilyas
What I have tried:
Tried debugging code by code but in vain
Maybe the answers here will be of help: How can i read a dynamically created textbox in gridview[^]
这篇关于C# - 无法在gridview中读取动态创建的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!