C# 从使用C动态创建的文本框中检索值#
在流中编码时,我动态创建了几个C# 从使用C动态创建的文本框中检索值#,c#,javascript,asp.net,c#-4.0,textbox,C#,Javascript,Asp.net,C# 4.0,Textbox,在流中编码时,我动态创建了几个textbox,我为每个文本框提供了唯一的id,并使用C#对所有文本框硬编码了一些值。 现在,单击按钮,我试图从文本框中检索值,我使用了下面的代码,但它引发了一个异常,因为对象引用未设置为对象的实例 请看下面的代码,我已经尝试了这两种方法,但仍然没有成功。请帮助我。 谢谢 谢谢 Am trying to use view state as per you told that i shlould recreate the controls ones again but
textbox
,我为每个文本框提供了唯一的id,并使用C#对所有文本框硬编码了一些值。现在,单击按钮,我试图从文本框中检索值,我使用了下面的代码,但它引发了一个异常,因为
对象引用未设置为对象的实例
请看下面的代码,我已经尝试了这两种方法,但仍然没有成功。
请帮助我。
谢谢
谢谢
Am trying to use view state as per you told that i shlould recreate the controls ones again but am getting exception as Invalid Arguments. please go have a look.
protected void btnSet_onClick(object sender, EventArgs e)
{
Table tblMainY = new Table();
TableRow tblRow = new TableRow();
tblMainY.Controls.Add(tblRow);
TableCell tblCel = new TableCell();
TextBox txtdyn = new TextBox();
txtdyn.Text = "1";
txtdyn.ID = "txtY01";
txtdyn.Width = 50;
tblCel.Controls.Add(txtdyn);
tblRow.Controls.Add(tblCel);
splY.Controls.Add(tblMainY);
ViewState["temptbl"] = tblMainY
}
protected void btnPltGrap_onclick(object sender, EventArgs e)
{
splY.Controls.Add(ViewState["Temptbl"]);
}
Please help me out
必须在init
上重新创建控件才能获取其值
以下是一些链接
编辑1
我过去也有过同样的问题
我所做的是给动态添加的控件一个ID,并确保它在回发时也保留该ID
一旦回发控件具有与以前相同的ID,Microsoft就会施展魔法,用回发前的值重新填充控件
把这段代码读一遍
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
this.NumberOfControls = 0; //very first time when page is loaded, value will be 0
else
this.createControls(); //if it is postback it will recreate the controls according to number of control has been created
}
//this is the base of this, it will hold the number of controls has been created, called properties
protected int NumberOfControls
{
get { return (int)ViewState["NumControls"]; }
set { ViewState["NumControls"] = value; }
}
//it will create the controls
protected void createControls()
{
int count = this.NumberOfControls;
for (int i = 0; i < count; i++) //loop for the total number of control.
{
TextBox tx = new TextBox(); //creating new control
tx.ID = "ControlID_" + i.ToString(); //in your solution you are giving static id, don't do that, assign id number dynamically, it will help you further, if you want to manipulate the controls for some other use
//Add the Controls to the container of your choice
form1.Controls.Add(tx);
}
}
//add new control
protected void addSomeControl()
{
TextBox tx = new TextBox();
tx.ID = "ControlID_" + NumberOfControls.ToString();
form1.Controls.Add(tx);
this.NumberOfControls++; //increment the number of control
}
protected void AddBtn_Click(object sender, EventArgs e)
{
addSomeControl();
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!Page.IsPostBack)
this.NumberOfControls=0;//第一次加载页面时,值将为0
其他的
this.createControls();//如果它是回发的,它将根据已创建控件的数量重新创建控件
}
//这是它的基础,它将保存已创建的控件的数量,称为属性
受保护的整数控制
{
获取{return(int)ViewState[“NumControls”];}
设置{ViewState[“NumControls”]=value;}
}
//它将创建控件
受保护的void createControls()
{
int count=this.NumberOfControls;
for(int i=0;i
以下链接中的一些示例代码作为我的博客
它解释了如何使用panel
control在dynamicali中放置和获取带有值和验证的textboxe
让我们看看这个网址。你可以得到很好的解决方案
用于在中获取文本框值的简单行
TextBox objTextBox = (TextBox)PlaceHolder.FindControl("CorrecttextBoxName");
string value=objTextBox .Text;
Default.aspx
在aspx文件中获取占位符标记
Default.aspx.cs
//添加/创建动态文本框
TextBox txt = new TextBox();
txt.ID = "New_txt";
txt.TextMode = TextBoxMode.MultiLine;
txt.Text = dt.Rows[0]["message"].ToString();
txt.Width = 802;
txt.Height = 450;
txt.ReadOnly = true;
PlaceHolder1.Controls.Add(txt);
从文本框中检索值
字符串str=txt.Text 首先确保这不是回发问题,在检索textbox的值之前,请确保在回发过程中您已创建了textbox,并且它们存在。谢谢。我正在尝试使用视图状态。但它将异常作为无效参数抛出。请参考下面的代码。由于文字限制,我分成两部分。请仔细阅读。我如何向你们展示我的代码,大约有15行,但是这个文本限制了我不能发布的限制。你能告诉我吗,因为我对这个很陌生。。所以谢谢,在你发布的问题下方有一个“编辑”链接,点击该链接,你可以编辑你的问题,只需在原始问题下方添加代码。非常感谢你的支持,我得到了它:)谢谢。为了只重新创建,我将其放入视图状态,然后在另一个按钮中单击“我只使用该视图状态”,并将其绑定到我的面板中,如splY.Controls.Add(视图状态[“testbl”]);但这告诉我我不能那样做。它将异常作为无效参数给出。@Rakesh您需要将其转换回textbox
,然后添加它。它应该会起作用。请参阅我的编辑。谢谢,但引发类似类型“System.Web.UI.WebControls.Table‘in Assembly’System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的异常不起作用。您在动态创建的表中保存的内容将创建一个动态文本框。因此,我将表本身保持为viewstate。ViewState[“TENTBL”]=tblMainY
TextBox objTextBox = (TextBox)PlaceHolder.FindControl("CorrecttextBoxName");
string value=objTextBox .Text;
TextBox txt = new TextBox();
txt.ID = "New_txt";
txt.TextMode = TextBoxMode.MultiLine;
txt.Text = dt.Rows[0]["message"].ToString();
txt.Width = 802;
txt.Height = 450;
txt.ReadOnly = true;
PlaceHolder1.Controls.Add(txt);