473,396 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

building dynamic list of web from controls

Is there a way to generate a list of say textbox controls dynamically at run
time, based on say a value coming out of a database which could vary each
time the code is run. In traditional asp you could just have run a loop to do
something like that. I am aware in you can use something like
Form1.Controls.Add(TextBox1) to add textboxes dynamically at run time but you
still need to declare each individual textbox control as far as I can tell..
This is not practial as I don't know how many I will need before I run the
page.
--
Scott
Nov 19 '05 #1
12 1453
dim x as integer = 10 'pull this from db

for i = 1 to 10
dim txt as new TextBox()
txt.Id = i.ToString()
Form1.Controls.Add(txt)
end

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"scottrm" <sc*****@newsgroup.nospam> wrote in message
news:74**********************************@microsof t.com...
Is there a way to generate a list of say textbox controls dynamically at run time, based on say a value coming out of a database which could vary each
time the code is run. In traditional asp you could just have run a loop to do something like that. I am aware in you can use something like
Form1.Controls.Add(TextBox1) to add textboxes dynamically at run time but you still need to declare each individual textbox control as far as I can tell.. This is not practial as I don't know how many I will need before I run the
page.
--
Scott

Nov 19 '05 #2
Perhaps you could add the TextBox controls to a higher level parent
control, like a Panel or even a Repeater. You wouldn't have to declare
each TextBox control in the code behind as a class member variable -
you could loop through the controls in the container and act upon
them.

Making sense?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 18 Apr 2005 07:11:13 -0700, "scottrm"
<sc*****@newsgroup.nospam> wrote:
Is there a way to generate a list of say textbox controls dynamically at run
time, based on say a value coming out of a database which could vary each
time the code is run. In traditional asp you could just have run a loop to do
something like that. I am aware in you can use something like
Form1.Controls.Add(TextBox1) to add textboxes dynamically at run time but you
still need to declare each individual textbox control as far as I can tell..
This is not practial as I don't know how many I will need before I run the
page.


Nov 19 '05 #3
Yep, it's fairly easy. The big problem is that across postbacks it's up to
you to recreate the controls. I just posted a sample earlier:

http://groups-beta.google.com/group/...717a97bd32c450

-Brock
DevelopMentor
http://staff.develop.com/ballen
Is there a way to generate a list of say textbox controls dynamically
at run time, based on say a value coming out of a database which could
vary each time the code is run. In traditional asp you could just have
run a loop to do something like that. I am aware in you can use
something like Form1.Controls.Add(TextBox1) to add textboxes
dynamically at run time but you still need to declare each individual
textbox control as far as I can tell.. This is not practial as I don't
know how many I will need before I run the page.


Nov 19 '05 #4
Thanks for all your inputs,

Hi Scott,

If the number of textboxes are not constant and depend on the datasoure's
record counts, I recommend that you consider using a template databound
control (specifying the textbox in the item template), such as
Repeater/DataList to display them. Also, it's not very complex to build a
custom template databound control so as to meet our particular requirement.

If there is any further quesitons, please feel free to post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #5
Hi

Thanks for your reply. Using a datalist with a textbox in the item template
is fine but there a couple of other things I need to do which I can't seem to
find much information on

1. When I have a list of textboxes based on values from the database I need
to populate the content of each with data, when I try something like the code
below I get an error.

<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<asp:TextBox id="TextBox1" runat="server"><%#
Container.DataItem("Name")%></asp:TextBox>
</ItemTemplate>
</asp:DataList>

2. When the user fills in all the text boxes and hits submit I need to
retrieve all those values to store in a database.
Steven Cheng[MSFT]" wrote:
Thanks for all your inputs,

Hi Scott,

If the number of textboxes are not constant and depend on the datasoure's
record counts, I recommend that you consider using a template databound
control (specifying the textbox in the item template), such as
Repeater/DataList to display them. Also, it's not very complex to build a
custom template databound control so as to meet our particular requirement.

If there is any further quesitons, please feel free to post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #6
Hi Scott,

Glad to hear from you. As for the further problems you mentioned, here are
some of my suggestions:

1. The error you met when binding text with textbox like:
==================
<asp:TextBox id="TextBox1" runat="server"><%#
Container.DataItem("Name")%></asp:TextBox>
=====================

is because the TextBox can't didn't support any content in it's InnerTag.
We should set the binding expression for it's Text property. For example:

<asp:TextBox id="TextBox1" runat="server"
Text="<%# Container.DataItem('Name')%>"></asp:TextBox>
2. As for how to retrieve all the input values in the textboxes in the
DAtaList control. We can use the Items collection to loopthrough all the
Items in DataList and find the TextBox in each itemtemplate and retrieve
the text value(we need to specify an ID for TextBox so as to reference it).
For example:
foreach(DataListItem dli in dlMain.Items)
{
TextBox txt = dli.FindControl("txtValue") as TextBox;
if(txt != null)
{
//do something
}
}

If there are anything else unclear, please feel free to post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #7
Thanks for the reply, that answers my question 1. ok but I do have a bit of a
problem with point 2. as I am unclear where to get the id's for the text
boxes since they are generated dynamically. I tried having a look at the html
produced and getting it from there eg. "DataList1__ctl157_TextBox1" but that
did not seem to work

"Steven Cheng[MSFT]" wrote:
Hi Scott,

Glad to hear from you. As for the further problems you mentioned, here are
some of my suggestions:

1. The error you met when binding text with textbox like:
==================
<asp:TextBox id="TextBox1" runat="server"><%#
Container.DataItem("Name")%></asp:TextBox>
=====================

is because the TextBox can't didn't support any content in it's InnerTag.
We should set the binding expression for it's Text property. For example:

<asp:TextBox id="TextBox1" runat="server"
Text="<%# Container.DataItem('Name')%>"></asp:TextBox>
2. As for how to retrieve all the input values in the textboxes in the
DAtaList control. We can use the Items collection to loopthrough all the
Items in DataList and find the TextBox in each itemtemplate and retrieve
the text value(we need to specify an ID for TextBox so as to reference it).
For example:
foreach(DataListItem dli in dlMain.Items)
{
TextBox txt = dli.FindControl("txtValue") as TextBox;
if(txt != null)
{
//do something
}
}

If there are anything else unclear, please feel free to post here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #8
Thanks for your response Scott,

As for FindControl, when we call FindConrol on a webControl which is a
NamingContainer, we don't need to specify the underlying encoded id of the
sub controls in it. We can directly use the ID we specify in aspx
template. For the example I mentioned in the previous message:

when we have the following template
===================
<ItemTemplate>
<asp:TextBox id="txtValue" runat="server"
Text="<%# Container.DataItem('Name')%>"></asp:TextBox>

</ItemTemplate>

we can just use the below code to retrieve the TextBox's reference:

foreach(DataListItem dli in dlMain.Items)
{
TextBox txt = dli.FindControl("txtValue") as TextBox;
if(txt != null)
{
//do something
}
}
===================

the encoded ID such as DataList1__ctl157_TextBox1 is the internal
implementation of ASP.NET, we should not care about that in a developer's
general perspective.

Hope Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #9
HI

Thanks for your help, much appreciated. I do have one final question. Is
there any way to identify specific text boxes with values based on values
taken from the database so I can accurately know which text boxes I am
retrieving data from.

"Steven Cheng[MSFT]" wrote:
Thanks for your response Scott,

As for FindControl, when we call FindConrol on a webControl which is a
NamingContainer, we don't need to specify the underlying encoded id of the
sub controls in it. We can directly use the ID we specify in aspx
template. For the example I mentioned in the previous message:

when we have the following template
===================
<ItemTemplate>
<asp:TextBox id="txtValue" runat="server"
Text="<%# Container.DataItem('Name')%>"></asp:TextBox>

</ItemTemplate>

we can just use the below code to retrieve the TextBox's reference:

foreach(DataListItem dli in dlMain.Items)
{
TextBox txt = dli.FindControl("txtValue") as TextBox;
if(txt != null)
{
//do something
}
}
===================

the encoded ID such as DataList1__ctl157_TextBox1 is the internal
implementation of ASP.NET, we should not care about that in a developer's
general perspective.

Hope Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #10
Thanks for your reply Scott,

I'm glad that my suggestion was of assistance. As for the identify the
Textboxes so as to conveniently reference them through the database column
name in db, I think we can make use of the DataGrid/DataList's ItemCreated
event, in that event, we can retrieve the control instance and manually
assign ID to it. For example:

private void dgPager_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
TextBox txt = e.Item.FindControl("txtOld") as TextBox;

if(txt != null)
{
txt.ID = "txtNew";
}
}
}

In addition, as my own opinion, I don't recommend this means since
manually assign ID will require additional processing time which may reduce
the performance. And I think assign the ID at design time in page template
is the prefer means.

Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #11
HI

Thanks for this although I am not sure I have explained properly, what I
want to do is say build a list of 10 checkboxes based on a value from the
database and assign each checkbox with an id based on a database value. I
then need to retrieve the contents of the texboxes based on the id from the
database. I am not sure how I would use your code to do this as
e.Item.FindControl("txtOld") used only one id "txtOld" not multiple ones for
each text box like I want. Also you say that I could assign the ID at design
time in the page template but I can only assign a single ID, not identify the
text boxes individually.

"Steven Cheng[MSFT]" wrote:
Thanks for your reply Scott,

I'm glad that my suggestion was of assistance. As for the identify the
Textboxes so as to conveniently reference them through the database column
name in db, I think we can make use of the DataGrid/DataList's ItemCreated
event, in that event, we can retrieve the control instance and manually
assign ID to it. For example:

private void dgPager_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
TextBox txt = e.Item.FindControl("txtOld") as TextBox;

if(txt != null)
{
txt.ID = "txtNew";
}
}
}

In addition, as my own opinion, I don't recommend this means since
manually assign ID will require additional processing time which may reduce
the performance. And I think assign the ID at design time in page template
is the prefer means.

Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #12
Hi Scott,

Thanks for your further description. I think I did misunderstand your
question in the last response. So let me restate my understanding:

Since you bind the checkbox (or other entry field) control with datas
retrieved from database records, you want to also identify each checkbox(in
the datalist items) so as to mapping the checkbox's value with the record
in database, yes? If there're still anything I misunderstood, please feel
free to let me know.

If this is your actual requirement, I think we have the following options:

1. use the webControl.Attributes collection and store the primary key into
a certain control's attribute. For example:
=========================
private void dlFISVIEW_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
DataRowView drv = e.Item.DataItem as DataRowView;
CheckBox chk = e.Item.FindControl("chkSelected") as CheckBox;

if(chk != null && drv != null)
{
chk.Attributes["id"] = drv["id"];
}
}
=======================

Then, when we retrieve the CheckBox later in postback event, we can check
its Attirubtes["id"] 's value to identify it.

2. If you don't want to use the Attributes collection. We can also consider
adding an additional HIDDEN control into the template to store the identity
of the data record, for example:

<itemTemplate>
<asp:Label id="key" runat="server" Visible="false" ..../>
<asp:CheckBox..... >
</itemtemplate>
And in ItemDataBound, we can store the primary key or other identity into
the label control's text and retrieve it later.

How do you think of this? If you have any other questions, please feel free
to post here. Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



Nov 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
4
by: Brian Shannon | last post by:
I have 3 combo boxes and two date text boxes on a .aspx page. The user can fill in any of the 5 controls or none to filter a datagrid. I was hoping someone could explain how to efficiently build...
5
by: mytestemailaccount | last post by:
Hi, Hope you can help. I am relatively new to all this but would appreciate the groups help. The scenario: I am c# and asp.net to create a web application. The web page contains a user...
1
by: npverni | last post by:
I have a fairly complex form that needs to load and maintain the state of several different dynamic user controls. Here is the workflow: 1) A series of editable user controls (each containing...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.