动态加载文本框值的问题 [英] problem with dynamicall loaded textbox values
问题描述
我有一种情况需要从数据库到文本框显示数据。这些文本框必须动态创建并且可以编辑。我可以按如下方式创建文本框:
string [] myData = liD1.ToArray(); // liD1是一个包含数据库值的列表
public void showData()
{
for ( int i = 0 ; i < myData.Length; i ++)
{
if (txtMore.FindControl( txtD1StartTime + i)== null )
{
txtStart1 = < span class =code-keyword> new TextBox();
lblStart1 = new Label();
txtStart1.ID = txtD1S + i;
lblStart1.ID = lblD1S + i;
txtStart1.Text = myData [i] .ToString();
lblStart1.AssociatedControlID = txtStart1.ID;
lblStart1.Text = 数据 + i;
txtMore.Controls.Add(lblStart1);
txtMore.Controls.Add(txtStart1);
}
}
}
在我的ascx页面中,我正在加载
< ul class = txtMore id = txtMore runat = server >
< / ul >
showData()函数正在使用动态创建的文本框加载数据。这工作正常。现在如果我编辑文本框并尝试获取新数据,它就不会给我任何东西。实际上它甚至找不到那种控制。代码如下所示:
public void getData()
{
for ( int i = 0 ; i < myData.Length; i ++)
{
TextBox t = txtMore.FindControl( txtD1S + i) as TextBox;
if (t!= null )
{
string temp = t.Text;
}
}
即使我在getData()中动态加载了来自showData()的文本框,它无法找到任何控制。请帮助。
您需要为每个请求重新创建动态控件。
如何在ASP.NET中动态创建控件并从中检索值 [< a href =http://www.codeproject.com/Articles/502251/How-to-create-controls-dynamically-in-ASP-NET-andtarget =_ blanktitle =New Window> ^ ]
你应该使用 ASP.NET转发器控件 [ ^ ]
如需准备参考,请参阅以下代码:
Aspx页面:
< div >
< ul < span class =code-keyword>>
< asp:Repeater runat = 服务器 ID = rptTextList >
< ItemTemplate > ;
< li >
< asp:标签 文本 =' < % = Eval( 标题)%>' runat = 服务器 ID = lblText / < span class =code-keyword>>
< asp:TextBox runat = server ID = txtText 文本 =' <% = Eval( TextData)%>' / >
< / li >
< / ItemTemplate >
< / asp:Repeater >
< / ul >
< < span class =code-leadattribute> / div >
.cs页面:
foreach (RepeaterItem item in rptTextList.Items)
{
Label lblText =(Label)item.FindControl( lblText);
TextBox txtText =(TextBox)item.FindControl( txtText);
Response.Write(txtText.Text + < br />跨度>);
}
在页面渲染中更改ID ..您必须在页面指令级别使用。 的ClientIDMode = 静态
Hi,
I have a situation where I need to show data from database to textboxes. These textboxes has to be dynamically created and can be editable. I am able to create textboxes as follows:
string[] myData = liD1.ToArray(); //liD1 is a list that has values from database
public void showData()
{
for (int i = 0; i < myData.Length; i++)
{
if (txtMore.FindControl("txtD1StartTime" + i) == null)
{
txtStart1 = new TextBox();
lblStart1 = new Label();
txtStart1.ID = "txtD1S" + i;
lblStart1.ID = "lblD1S" + i;
txtStart1.Text = myData[i].ToString();
lblStart1.AssociatedControlID = txtStart1.ID;
lblStart1.Text = "Data" + i;
txtMore.Controls.Add(lblStart1);
txtMore.Controls.Add(txtStart1);
}
}
}
In my ascx page, I am loading those textboxes in
<ul class="txtMore" id="txtMore" runat="server">
</ul>
showData() function is loading data with dynamically created textboxes. This is working fine. Now If i edit textboxes and try to get new data, it is not giving me anything. Actually it couldn't even find that control. Code is as shown below:
public void getData()
{
for (int i = 0; i < myData.Length; i++)
{
TextBox t = txtMore.FindControl("txtD1S" + i) as TextBox;
if (t != null)
{
string temp = t.Text;
}
}
Even though I have dynamically loaded textboxes from showData(), in getData(),it is not able to find any control. Please help.
You will need to re-create the dynamic controls for every request.
How to create controls dynamically in ASP.NET and retrieve values from it[^]
Hi,
You should have to use The ASP.NET Repeater Control[^]
For your ready reference see below code:
Aspx page:
<div> <ul> <asp:Repeater runat="server" ID="rptTextList" > <ItemTemplate> <li> <asp:Label Text='<%= Eval("Title") %>' runat="server" ID="lblText" /> <asp:TextBox runat="server" ID="txtText" Text='<%= Eval("TextData") %>' /> </li> </ItemTemplate> </asp:Repeater> </ul> </div>
.cs page:
foreach (RepeaterItem item in rptTextList.Items) { Label lblText = (Label)item.FindControl("lblText"); TextBox txtText = (TextBox)item.FindControl("txtText"); Response.Write(txtText.Text + "<br/>"); }
IDs are changed in page rendering.. you have to use at page directive level. ClientIDMode="Static"
这篇关于动态加载文本框值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!