building dynamic list of web from controls

G

Guest

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

Scott Allen

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?
 
S

Steven Cheng[MSFT]

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

Guest

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

Steven Cheng[MSFT]

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

Guest

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
 
S

Steven Cheng[MSFT]

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

Guest

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

Steven Cheng[MSFT]

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

Guest

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

Steven Cheng[MSFT]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top