如何获取Dynamiclly创建的文本框控件的值。 [英] How to Get the values of Dynamiclly Created Textbox Control.
问题描述
嗨
我正在开发一个项目,它应该根据用户要求创建一个动态文本框控件(n no)。
我的要求:
在我的项目中,我有一个名为no.of分支的文本框如果用户输入3,那么它应该动态生成3个文本框,如果他给1它应该生成1个文本框。最后在按钮上单击动态创建的文本框中输入的文本应存储在数据库中。
项目状态:
我可以根据用户价值生成动态文本框。
我想要帮助/支持;
我无法获得动态文本框的价值。
它会如果有人帮助我摆脱这种摆脱,那就太明显了。
用于生成文本框的代码 - 工作正常:
Hi
I was developing a Project which should create a dynamic textbox control(n no's) as per user requirement.
My Requirement:
In my project i have a text box called no.of Branches in that if the user enters 3 then it should generate 3 textbox dynamically if he give 1 it should generate 1 textbox.Finally on button click the text the entered in dynamically created textbox should stored in database.
Project Status:
I am able to generate the Dynamic textbox as per the user value.
I want Help/Support For;
I'm unable to get the value of dynamic text boxes.
It would be appreciable if some one help me to come out of this rid.
Code i used for Generating Textbox - Working Fine:
protected void CreateDynamicTextBox(int bno)
{
//Uptxtchange.Update();
int addedBranches = bno;
for (int i = 0; i < addedBranches; i++)
{
Literal lt1 = new Literal();
lt1.Text = "<br /> Branches/Location:" + (i + 1) + "<br />" + " ";
pnlTextChange.Controls.Add(lt1);// "pnl1" is a id of Panel
TextBox MyTextBox = new TextBox();
//Assigning the textbox ID name
MyTextBox.ID = "tb" + i;
MyTextBox.Width = 265;
MyTextBox.Height = 15;
MyTextBox.Attributes["runat"] = "server";
MyTextBox.Text = String.Empty;
MyTextBox.BackColor = System.Drawing.Color.AliceBlue;
pnlTextChange.Controls.Add(MyTextBox);
Literal lt = new Literal();
lt.Text = " ";
pnlTextChange.Controls.Add(lt);
}
}
我用以获取值的代码: - [不工作 - 错误 - 始终获取nullpointer异常]
Code i used to get the values:- [Not Working -Error-Always getting nullpointer Exception]
// get values from your dynamically created elements
bno = Convert.ToInt32(TxtBranchesNo.Text);
string strValue = string.Empty;
int addedBranches = bno;
for (int i = 0; i < addedBranches; i++)
{
string boxName = "tb" + (i).ToString();
TextBox tb = pnlTextChange.FindControl(boxName) as TextBox;
strValue += tb.Text + "\n";
insert(strValue);
}
注意:在按钮上单击回发,以便动态texbox隐藏。
我在更新面板内部使用pnlTexchange。
等待一些可以帮我解决这个问题的人。
在此先收到预订
页面加载事件
Note: On button click postback was happening so the dynamic texboxes getting hide.
I'm using pnlTexchange inside updatepanel.
Waiting for the some who could give me a hand to come out of this issue.
Thanks in advance
Page load Event
protected void Page_Load(object sender, EventArgs e)
{
int bno;
TxtCompanyName.Text = Request.QueryString["CompanyName"];
Txtyourname.Text = Request.QueryString["YourName"];
TxtDesignation.Text = Request.QueryString["Designation"];
TxtMobileNo.Text = Request.QueryString["MobNo"];
TxtEmailidF.Text = Request.QueryString["Emailid"];
if (!IsPostBack)
{
bno = Convert.ToInt32(TxtBranchesNo.Text);
CreateDynamicTextBox(bno);
}
else
{
bno = 0;
}
}
我的aspx页面:
my aspx page:
<table frame="box">
<tr>
<td align="left">
<asp:Label ID="LblBranchesNo" runat="server" Text="Number of Branches / Location that you have : "></asp:Label>
</td>
<td>
<asp:UpdatePanel ID="Uptxtchange" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="PnlTxtchange" runat="server">
<asp:TextBox ID="TxtBranchesNo" runat="server" Width="265px" Height="25px"
ontextchanged="TxtBranchesNo_TextChanged" AutoPostBack="true">1</asp:TextBox>
</asp:Panel>
<asp:Panel ID="PnlDynamic" runat="server">
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TxtBranchesNo" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="LblSoftware" runat="server" Text="Are you using any software : "></asp:Label>
</td>
<td>
<asp:UpdatePanel ID="UpSoftware" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="PNLSoftware" runat="server">
<asp:RadioButtonList
ID="rbSoftware" runat="server" RepeatDirection="Vertical" RepeatColumns="2" AutoPostBack="true" onselectedindexchanged="rbSoftware_SelectedIndexChanged" >
<asp:ListItem Text = "Yes" Value = "Yes" ></asp:ListItem>
<asp:ListItem Text = "No" Value = "No" Selected = "True"></asp:ListItem>
</asp:RadioButtonList><br />
<asp:TextBox ID="TxtSoftware" runat="server" Width="265px" Height="25px" BackColor="ActiveBorder" Visible="false"></asp:TextBox>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TxtSoftware" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>.....
我的设计还可以?????
is my designing was ok?????
推荐答案
动态控件应该在OnInit中完成事件而不是Page_Load。这可能是你在回发期间找不到它们的原因。
Dynamic controls should be done in the OnInit event instead of Page_Load. That is likely why you can't find them during postback.
你的问题在于你为每个文本框分配了id的方式。而不是
Your problem is with the way you've assigned the id to each textbox. Instead of
MyTextBox.ID = "tb" + i;
尝试
MyTextBox.ID = "tb" + i.ToString();
嗨大家好,谢谢你帮助我:
看到我用过代码在下面的方式现在它的工作就像魅力!!!!
Hi Guys Thanks for Helping me:
See i used code in below way now it was working like charm!!!!
protected void Page_PreInit(object sender, EventArgs e)
{
Control myControl = GetPostBackControl(this.Page);
if ((myControl != null))
{
if ((myControl.ClientID.ToString() == "TxtBranchesNo"))
{
//myCount = int.Parse(TxtBranchesNo.Text);
myCount = Convert.ToInt32(Request["TxtBranchesNo"]);
}
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
int bno = Convert.ToInt32(TxtBranchesNo.Text);
if (Convert.ToInt32(Request["TxtBranchesNo"]) > 0)
{
dynamicTextBoxes = new TextBox[Convert.ToInt32(Request["TxtBranchesNo"])];
int i;
for (i = 0; i < Convert.ToInt32(Request["TxtBranchesNo"]); i += 1)
{
Literal lt1 = new Literal();
lt1.Text = "<br /> Branches:" + (i + 1);
myPlaceHolder.Controls.Add(lt1); ;// "pnl1" is a id of Panel
TextBox textBox = new TextBox();
textBox.ID = "myTextBox" + i.ToString();
textBox.Width = 200;
textBox.Height = 15;
textBox.Text = String.Empty;
textBox.BackColor = System.Drawing.Color.AliceBlue;
myPlaceHolder.Controls.Add(textBox);
dynamicTextBoxes[i] = textBox;
LiteralControl literalBreak = new LiteralControl(" ");
myPlaceHolder.Controls.Add(literalBreak);
}
}
}
protected void MyButton_Click(object sender, EventArgs e)
{
MyLabel.Text = "";
foreach (TextBox tb in dynamicTextBoxes)
{
//MyLabel.Text += tb.Text + ",";
MyLabel.Text = tb.Text ;
insert(MyLabel.Text);
}
}
public static Control GetPostBackControl(Page thePage)
{
Control myControl = null;
string ctrlName = thePage.Request.Params.Get("__EVENTTARGET");
if (((ctrlName != null) & (ctrlName != string.Empty)))
{
myControl = thePage.FindControl(ctrlName);
}
else
{
foreach (string Item in thePage.Request.Form)
{
Control c = thePage.FindControl(Item);
if (((c) is System.Web.UI.WebControls.TextBox))
{
myControl = c;
}
}
}
return myControl;
}
这篇关于如何获取Dynamiclly创建的文本框控件的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!