Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > building dynamic list of web from controls

Reply
Thread Tools

building dynamic list of web from controls

 
 
=?Utf-8?B?c2NvdHRybQ==?=
Guest
Posts: n/a
 
      04-18-2005
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
 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      04-18-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Scott Allen
Guest
Posts: n/a
 
      04-18-2005
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"
<(E-Mail Removed)> 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.


 
Reply With Quote
 
Brock Allen
Guest
Posts: n/a
 
      04-18-2005
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.
>




 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-19-2005
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.)

 
Reply With Quote
 
=?Utf-8?B?c2NvdHRybQ==?=
Guest
Posts: n/a
 
      04-22-2005
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.

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

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.)
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-25-2005
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.)



 
Reply With Quote
 
=?Utf-8?B?c2NvdHRybQ==?=
Guest
Posts: n/a
 
      04-25-2005
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.)
>
>
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-26-2005
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.)

 
Reply With Quote
 
=?Utf-8?B?c2NvdHRybQ==?=
Guest
Posts: n/a
 
      04-27-2005
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.)
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Building Dynamic Controls Robert Bevington ASP .Net Building Controls 0 07-11-2007 07:12 PM
Dynamic Controls created by Dynamic Controls Ronald ASP .Net 2 01-17-2006 12:44 AM
building dynamic controls in asp.net...test hierarchy...please hel Irfan Akram ASP .Net Web Services 1 12-13-2004 08:44 PM
Dynamic Controls in asp.net...Building a tree hierarchy..please he =?Utf-8?B?SXJmYW4gQWtyYW0=?= ASP .Net 0 12-13-2004 07:39 PM
Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls Denny Smolinski via .NET 247 ASP .Net Web Controls 1 05-04-2004 09:15 PM



Advertisments