创建动态Textboxex并获取值 [英] Create Dynamic Textboxex and Get values

查看:77
本文介绍了创建动态Textboxex并获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

当用户单击添加更多链接"按钮时,我想创建动态文本框.
为此,我正在使用此代码.我不得不提到我正在使用母版页.


受保护的void lnkAddMore_Click(对象发送者,EventArgs e)
{
如果(Request.Cookies ["value"]!= null)
{
i = Convert.ToInt32(Request.Cookies ["value"].Value)+ 1;
}
for(int k = 1; k< = i; k ++)
{
LiteralControl文字=新的LiteralControl();
literal.Text =< br/>< br/>";
标签newLabel = new Label();
newLabel.Text =选择" +" + k.ToString();
newLabel.ID ="lblChoice_" + k.ToString();
newLabel.Attributes.Add("runat","Server");
this.panelLabel.Controls.Add(newLabel);
this.panelLabel.Controls.Add(literal);

LiteralControlliteral1 =新的LiteralControl();
literal1.Text =< br/>< br/>";
TextBox nexText =新的TextBox();
nexText.ID ="txtChoice_" + k.ToString();
nexText.Attributes.Add("TextMode","MultiLine");
nexText.Attributes.Add("runat","Server");
panelTextbox.Controls.Add(nexText);
this.panelTextbox.Controls.Add(literal1);

Response.Cookies ["value"].Value = i.ToString();
Session ["Panel"] = panelTextbox;
}
}

受保护的void Page_Load(对象发送者,EventArgs e)
{
如果(!IsPostBack)
{
如果(Session ["Panel"]!= null)
{
ContentPlaceHolder content =新的ContentPlaceHolder();
content.Controls.Add(Session ["Panel"]作为面板);
}
}
}

现在,在单击提交"按钮后如何检索这些文本框的数据时遇到了麻烦,这样我就可以将那里的文本框的值存储到数据库中了.

btnSave的click事件将编写什么代码
受保护的无效btnSave_Click(对象发送者,EventArgs e)
{
如果(Session ["Panel"]!= null)
{
ContentPlaceHolder content_new =新的ContentPlaceHolder();
for(int i = 1; i< = count; i ++)
{
strControlName ="txtChoice_" + i.ToString();

TextBox objTextBox =(TextBox)content_new.FindControl(strControlName);

strTextBoxValues [i] = objTextBox.Text;
字符串str3 = strTextBoxValues [2];
}
}
}


此代码显示objTextBox错误
错误是NullReferenceException

以及如何编写存储过程以保存上述代码的数据.

主要问题是处理参数声明

如何声明用于传递值的动态参数,以便为动态文本框保存值



在此先感谢

Deepak Pandey

Hello,

I want to create dynamic text box when user click on Add more link button.
For this I am using this code. And I have to mention that I am using master page.


protected void lnkAddMore_Click(object sender, EventArgs e)
{
if (Request.Cookies["value"] != null)
{
i = Convert.ToInt32(Request.Cookies["value"].Value) + 1 ;
}
for (int k = 1; k <= i; k++)
{
LiteralControl literal = new LiteralControl();
literal.Text = "<br /><br />";
Label newLabel = new Label();
newLabel.Text = "Choice" + " " + k.ToString();
newLabel.ID = "lblChoice_" + k.ToString();
newLabel.Attributes.Add("runat", "Server");
this.panelLabel.Controls.Add(newLabel);
this.panelLabel.Controls.Add(literal);

LiteralControl literal1 = new LiteralControl();
literal1.Text = "<br /><br />";
TextBox nexText = new TextBox();
nexText.ID = "txtChoice_" + k.ToString();
nexText.Attributes.Add("TextMode", "MultiLine");
nexText.Attributes.Add("runat", "Server");
panelTextbox.Controls.Add(nexText);
this.panelTextbox.Controls.Add(literal1);

Response.Cookies["value"].Value = i.ToString();
Session["Panel"] = panelTextbox;
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Panel"] != null)
{
ContentPlaceHolder content=new ContentPlaceHolder();
content.Controls.Add(Session["Panel"] as Panel);
}
}
}

Now I am facing trouble how to retrieve the data of the these text boxes after the clicking on the submit button so that I can store the values of there text boxes to database.

What will be code written for the click event of btnSave
protected void btnSave_Click(object sender, EventArgs e)
{
if (Session["Panel"] != null)
{
ContentPlaceHolder content_new = new ContentPlaceHolder();
for (int i = 1; i <= count; i++)
{
strControlName = "txtChoice_" + i.ToString();

TextBox objTextBox = (TextBox)content_new.FindControl(strControlName);

strTextBoxValues[i] = objTextBox.Text;
string str3 = strTextBoxValues[2];
}
}
}


This code is showing error for objTextBox
The error is NullReferenceException

And How to write stored procedure for saving data of above code.

The main problem is handling the parameter declaration

how to declare dynamic parameter for passing values so that value is saved for dynamic textbox



Thanks in advance

Deepak Pandey

推荐答案

使用以下命令:

Use following:

protected void btnSave_Click(object sender, EventArgs e)
           {
               foreach( Control c in panelTextbox.Controls )
               {
                    if(c is TextBox)
                    {
                       // Write code for saving data in Database.
                    }
               }
           }


回发后,您需要使用Page.FindControl方法来找到它们.
Page.FindControl方法(字符串) [
After your postback, you need to use the Page.FindControl Method to find them.
Page.FindControl Method (String)[^]

Note: The Control.FindControl Method would narrow it down if you want to use panelTextbox as a base. Probably a little more efficient.


添加动态控件时,您必须在回发中再次添加它们,然后从它们中取出数据.如果您打算从其中任何一个中获取数据,您将需要知道已添加了多少个文本框或控件,然后再次将其全部添加.同样,每回发一次,您都必须添加动态添加的控件,然后才能在回发中引用它们.

尝试此链接以获取帮助
http://support.microsoft.com/kb/317515 [
When adding dynamic controls you have to add them again on the postback and then retireve the data from them. You will need to know how many text boxes or controls you have added and add them all again if you plan to get data out of any of them. Again every post back you must add the controls that you dynamically added before referencig them on your postback.

Try this link for some help
http://support.microsoft.com/kb/317515[^]


这篇关于创建动态Textboxex并获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆