[英]Get values of Dynamically Created Controls inside Asp.Net Gridview C#
I am using Asp.Net Gridview and adding a text box dynamically in third column. 我正在使用Asp.Net Gridview并在第三栏中动态添加文本框。 The third column has a PlaceHolder and i am adding Textbox inside that placeHolder dynamically. 第三列有一个PlaceHolder,我正在动态地在该placeHolder中添加文本框。 This part is working fine. 这部分工作正常。 Now if i entered any text inside that textBox, how can i be able to get the value entered by the user.? 现在,如果我在该文本框中输入了任何文本,那么我如何能够获得用户输入的值?
Below is my code : 下面是我的代码:
protected void ddlChangeSubType_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlChangeSubType = (DropDownList)sender;
GridViewRow currentRow = (GridViewRow)ddlChangeSubType.NamingContainer;
DropDownList ddlChangeType = currentRow.FindControl("ddlChangeType") as DropDownList;
//TextBox txt = currentRow.FindControl("txt") as TextBox;
PlaceHolder placehldr = currentRow.FindControl("placehldrDynamicCnrtl") as PlaceHolder;
object objControl;
rowIndex = currentRow.RowIndex;
if (Session["DynamicControls"] != null)
{
for (int y = 0; y < CRFormGridView.Rows.Count; y++)
{
if (((Dictionary<int, object>)Session["DynamicControls"]).TryGetValue(y, out objControl))
{
objControlsDict.Add(y, objControl);
}
}
}
if (ddlChangeSubType != null && currentRow != null && ddlChangeSubType != null)
{
switch (ddlChangeType.SelectedItem.Text.ToUpper())
{
case "UPDATE OFFER":
TextBox txtBox = new TextBox();
txtBox.Text = "Text Box Added";
txtBox.ID = "txt";
txtBox.ClientIDMode = ClientIDMode.Static;
txtBox.EnableViewState = true;
placehldr.Controls.Add(txtBox);
if (objControlsDict.ContainsKey(rowIndex))
objControlsDict.Remove(rowIndex);
objControlsDict.Add(rowIndex, txtBox);
break;
case "ADD COMPONENT":
Label lbl = new Label();
lbl.Text = "Label Added";
lbl.ID = "lbl";
lbl.ClientIDMode = ClientIDMode.Static;
lbl.EnableViewState = true;
placehldr.Controls.Add(lbl);
if (objControlsDict.ContainsKey(rowIndex))
objControlsDict.Remove(rowIndex);
objControlsDict.Add(rowIndex, lbl);
break;
case "UPDATE REQUEST":
break;
default:
break;
}
Session.Add("DynamicControls", objControlsDict);
}
}
protected void placehldrDynamicCnrtl_PreRender(object sender, EventArgs e)
{
try
{
if (Page.IsPostBack)
{
PlaceHolder placeHldr = (PlaceHolder)sender;
GridViewRow currentRow = (GridViewRow)placeHldr.NamingContainer;
objControlsDict = (Dictionary<int, object>)Session["DynamicControls"];
if (objControlsDict != null)
{
if (objControlsDict.ContainsKey(count) && objControlsDict[count] is TextBox)
{
TextBox txtBox = (TextBox)objControlsDict[count];
txtBox.Text = "Text Box Added";
txtBox.ID = "txt";
txtBox.ClientIDMode = ClientIDMode.Static;
txtBox.EnableViewState = true;
((PlaceHolder)this.CRFormGridView.Rows[count].Cells[3].FindControl(
"placehldrDynamicCnrtl")).Controls.Add(txtBox);
}
if (objControlsDict.ContainsKey(count) && objControlsDict[count] is Label)
{
Label lbl = (Label)objControlsDict[count];
lbl.Text = "Label Added";
lbl.ID = "lbl";
lbl.ClientIDMode = ClientIDMode.Static;
lbl.EnableViewState = true;
((PlaceHolder)this.CRFormGridView.Rows[count].Cells[3].FindControl(
"placehldrDynamicCnrtl")).Controls.Add(lbl);
}
count++;
}
}
}
catch (Exception es)
{
throw;
}
}
You can access your Textbox by `FindControl("ControlID"), for example: 您可以通过`FindControl(“ ControlID”)访问文本框,例如:
TextBox txtbox = (TextBox)FindControl("txt");
and in order to get it's value, you'll write: 为了获得它的价值,您将编写:
String txt_value = txtbox.Text;
You can access any property of your textbox after making the TextBox object and finding it by it's ID with the help if FindControl()
在创建TextBox对象并通过FindControl()
的帮助通过其ID查找它之后,可以访问文本框的任何属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.