Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
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
as follows:
C#
<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:

C#
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.
Posted

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[^]
 
Share this answer
 
Comments
Dhyanga 9-Dec-14 14:29pm    
I couldn't understand where the SaveViewState is called in that code.
Richard Deeming 9-Dec-14 15:44pm    
The SaveViewState and LoadViewState methods are automatically called as part of the ASP.NET page lifecycle.
Dhyanga 9-Dec-14 16:25pm    
so Do i need to write those code by myself or do they automatically generated?
Richard Deeming 10-Dec-14 7:08am    
You will need to write the code which recreates the controls yourself.

The code which calls SaveViewState and LoadViewState is part of the framework.
Hi,

You should have to use The ASP.NET Repeater Control[^]

For your ready reference see below code:

Aspx page:
HTML
<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:

C#
foreach (RepeaterItem item in rptTextList.Items)
{
    Label lblText = (Label)item.FindControl("lblText");
    TextBox txtText = (TextBox)item.FindControl("txtText");

    Response.Write(txtText.Text + "<br/>");
}
 
Share this answer
 
Comments
Dhyanga 9-Dec-14 14:26pm    
if I have repeater with textboxes, how do I insert my string array data to those textboxes ?
Dusara Maulik 10-Dec-14 2:19am    
Kindly review below link which will be helpful to you to bind string array to repeater.

http://www.aspnettutorials.com/tutorials/controls/bind-arrlist-rep-asp4-cs
Dhyanga 10-Dec-14 10:32am    
Thank you!!
IDs are changed in page rendering.. you have to use at page directive level. ClientIDMode="Static"
 
Share this answer
 
Comments
Dhyanga 9-Dec-14 12:31pm    
I added the ClientIDMMode but still same issue. :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900