473,399 Members | 3,888 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,399 software developers and data experts.

Dynamically build Repeater Items - How??

Hello,

I would greatly appreciate if someone can show me how to dynamically
build a Repeater
with unknown number of columns at design time. I have looked various
threads in this
newsgroup, websites, MSDN and was not able to find something that would
help me understand and code. I might not be searching for the right
words or phrases. So if you know how to do this or know of links or
websites that have information about this, please post them here.

Thank you in advance !

-Sydney

Mar 30 '06 #1
4 5035
You can design your templates programmatically.

This demo creates templates programmatically in asp.net 2.0
http://www.webswapp.com/codesamples/...e/default.aspx

This does the same concepts in asp.net 1.1
http://www.societopia.net/Samples/Da...edColumns.aspx

And this is and MSDN reference to do it in a repeater in asp.net.1.
http://msdn.microsoft.com/library/de...ynamically.asp
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"sy********@gmail.com" wrote:
Hello,

I would greatly appreciate if someone can show me how to dynamically
build a Repeater
with unknown number of columns at design time. I have looked various
threads in this
newsgroup, websites, MSDN and was not able to find something that would
help me understand and code. I might not be searching for the right
words or phrases. So if you know how to do this or know of links or
websites that have information about this, please post them here.

Thank you in advance !

-Sydney

Mar 30 '06 #2
Phillip,

Thanks for the response. I have made some progress after examing the
article under this link you provided:

http://msdn.microsoft.com/library/de...ynamically.asp

then click on "Creating Templates Programmatically in the DataGrid
Control". I decided to use the DataGrid control.

and here's an excerpt from this article:

' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim tc1 As New TemplateColumn()
tc1.HeaderTemplate = New _
DataGridTemplate(ListItemType.Header, "Column1")
tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, _
"Column1")
tc1.EditItemTemplate = New _
DataGridTemplate(ListItemType.EditItem, "Column1")
tc1.FooterTemplate = New _
DataGridTemplate(ListItemType.Footer, "Column1")
DataGrid1.Columns.Add(tc1)

Dim tc2 As New TemplateColumn()
tc2.HeaderTemplate = New _
DataGridTemplate(ListItemType.Header, "Column2")
tc2.ItemTemplate = New _
DataGridTemplate(ListItemType.Item, "Column2")
tc2.EditItemTemplate = New _
DataGridTemplate(ListItemType.EditItem, "Column2")
tc2.FooterTemplate = New _
DataGridTemplate(ListItemType.Footer, "Column2")
DataGrid1.Columns.Add(tc2)
SqlDataAdapter1.Fill(DsCategories1)
DataGrid1.DataBind()
End Sub

' Visual Basic
Private Class DataGridTemplate
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String

Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub

Sub InstantiateIn(ByVal container As Control) _
Implements ITemplate.InstantiateIn
Dim lc As New Literal()
Select Case templateType
Case ListItemType.Header
lc.Text = "<B>" & columnName & "</B>"
container.Controls.Add(lc)
Case ListItemType.Item
lc.Text = "Item " & columnName
container.Controls.Add(lc)
Case ListItemType.EditItem
Dim tb As New TextBox()
tb.Text = ""
container.Controls.Add(tb)
Case ListItemType.Footer
lc.Text = "<I>Footer</I>"
container.Controls.Add(lc)
End Select
End Sub
End Class
My question is: why it produced four additional columns instead of two
since the code asked to dynamically allocate two
more columns in addition to the data columns coming back from the
database? This is what it looks like without my data
columns showing.

Column1 Column2 Column1 Column2
Item Column1 Item Column2 Item Column1 Item Column2

-Sydney
Phillip Williams wrote:
You can design your templates programmatically.

This demo creates templates programmatically in asp.net 2.0
http://www.webswapp.com/codesamples/...e/default.aspx

This does the same concepts in asp.net 1.1
http://www.societopia.net/Samples/Da...edColumns.aspx

And this is and MSDN reference to do it in a repeater in asp.net.1.
http://msdn.microsoft.com/library/de...ynamically.asp
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"sy********@gmail.com" wrote:
Hello,

I would greatly appreciate if someone can show me how to dynamically
build a Repeater
with unknown number of columns at design time. I have looked various
threads in this
newsgroup, websites, MSDN and was not able to find something that would
help me understand and code. I might not be searching for the right
words or phrases. So if you know how to do this or know of links or
websites that have information about this, please post them here.

Thank you in advance !

-Sydney


Mar 31 '06 #3
Sydney,

Set the AutoGenerateColumns = false
http://msdn2.microsoft.com/en-us/lib...tecolumns.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"sy********@gmail.com" wrote:
Phillip,

Thanks for the response. I have made some progress after examing the
article under this link you provided:

http://msdn.microsoft.com/library/de...ynamically.asp

then click on "Creating Templates Programmatically in the DataGrid
Control". I decided to use the DataGrid control.

and here's an excerpt from this article:

' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim tc1 As New TemplateColumn()
tc1.HeaderTemplate = New _
DataGridTemplate(ListItemType.Header, "Column1")
tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, _
"Column1")
tc1.EditItemTemplate = New _
DataGridTemplate(ListItemType.EditItem, "Column1")
tc1.FooterTemplate = New _
DataGridTemplate(ListItemType.Footer, "Column1")
DataGrid1.Columns.Add(tc1)

Dim tc2 As New TemplateColumn()
tc2.HeaderTemplate = New _
DataGridTemplate(ListItemType.Header, "Column2")
tc2.ItemTemplate = New _
DataGridTemplate(ListItemType.Item, "Column2")
tc2.EditItemTemplate = New _
DataGridTemplate(ListItemType.EditItem, "Column2")
tc2.FooterTemplate = New _
DataGridTemplate(ListItemType.Footer, "Column2")
DataGrid1.Columns.Add(tc2)
SqlDataAdapter1.Fill(DsCategories1)
DataGrid1.DataBind()
End Sub

' Visual Basic
Private Class DataGridTemplate
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String

Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub

Sub InstantiateIn(ByVal container As Control) _
Implements ITemplate.InstantiateIn
Dim lc As New Literal()
Select Case templateType
Case ListItemType.Header
lc.Text = "<B>" & columnName & "</B>"
container.Controls.Add(lc)
Case ListItemType.Item
lc.Text = "Item " & columnName
container.Controls.Add(lc)
Case ListItemType.EditItem
Dim tb As New TextBox()
tb.Text = ""
container.Controls.Add(tb)
Case ListItemType.Footer
lc.Text = "<I>Footer</I>"
container.Controls.Add(lc)
End Select
End Sub
End Class
My question is: why it produced four additional columns instead of two
since the code asked to dynamically allocate two
more columns in addition to the data columns coming back from the
database? This is what it looks like without my data
columns showing.

Column1 Column2 Column1 Column2
Item Column1 Item Column2 Item Column1 Item Column2

-Sydney
Phillip Williams wrote:
You can design your templates programmatically.

This demo creates templates programmatically in asp.net 2.0
http://www.webswapp.com/codesamples/...e/default.aspx

This does the same concepts in asp.net 1.1
http://www.societopia.net/Samples/Da...edColumns.aspx

And this is and MSDN reference to do it in a repeater in asp.net.1.
http://msdn.microsoft.com/library/de...ynamically.asp
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"sy********@gmail.com" wrote:
Hello,

I would greatly appreciate if someone can show me how to dynamically
build a Repeater
with unknown number of columns at design time. I have looked various
threads in this
newsgroup, websites, MSDN and was not able to find something that would
help me understand and code. I might not be searching for the right
words or phrases. So if you know how to do this or know of links or
websites that have information about this, please post them here.

Thank you in advance !

-Sydney


Apr 1 '06 #4
Phillip,

Basically the code is doing the following (see excerpt below):

1. Add some datagrid columns at runtime.
2. Bind dataset to datagrid (at design time, I do not know the number
of colums. could be
1 or 15). The dynamically added columns always come first. Don't
know if I can tell it
to add, say a CheckBox between my data column 4 and column 6 so that
the checkbox will be
in column 5. This is not important at the moment.
3. The page contains a datagrid and a submit button.
4. Set Trace="True"

This code always generates Column1, Column2, Column1, Column2, + my
data columns. Does not
matter whether AutoGenerateColumns="True" or "FALSE". I expect to see
only Column1, Column2 + my data columns.

During Page_Load, I have a few rows with the columns I just described
displayed on the screen. I clicked
two checkboxes under Column1 on Row 1 and Row2 and clicked "Submit".
Please see my "Trace Output" below.
I noticed the the CheckBox no longer uses the "Checked" property. How
would I determine which CheckBox is
checked under Column1? It is setting the value to "ON" instead of
Checked being TRUE. Also, I am searching
for "_ctl1" being my checkbox. I noticed it always the same for every
row.

Am I on the track here? Is there a better way to do this? My goal
behind this excerise is:

1. Dynamically add a CheckBox to the DataGrid in addition to whatever
columns I get back from my
stored procedure.
2. Identify which row(s) I want to check.
3. Process these checked records on PostBack.
4. That's it!
Thank you for the help!!

-Sydney

---------------- An excerpt of the source code ----------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Page.IsPostBack Then
ChkItems()
End If

Dim tc1 As New TemplateColumn()
tc1.HeaderTemplate = New DataGridTemplate(ListItemType.Header,
"Column1")
tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, "Column1")
tc1.EditItemTemplate = New DataGridTemplate(ListItemType.EditItem,
"Column1")
tc1.FooterTemplate = New DataGridTemplate(ListItemType.Footer,
"Column1")
DataGrid1.Columns.Add(tc1)
Dim tc2 As New TemplateColumn()
tc2.HeaderTemplate = New DataGridTemplate(ListItemType.Header,
"Column2")
tc2.ItemTemplate = New DataGridTemplate(ListItemType.Item, "Column2")
tc2.EditItemTemplate = New DataGridTemplate(ListItemType.EditItem,
"Column2")
tc2.FooterTemplate = New DataGridTemplate(ListItemType.Footer,
"Column2")
DataGrid1.Columns.Add(tc2)
[.. setup sql connection, command object here....]

da = New SqlDataAdapter(cmd)
da.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()

End Sub
sub chkitems()

dim iitems as datagriditemcollection
dim iitem as datagriditem
dim i as integer
dim cb as checkbox

iitems = datagrid1.items

for i = 0 to iitems.count -1
iitem = iitems.item(i)
cb = Ctype(iitem.findcontrol("_ctl1"), checkbox)
if not isnothing(cb) then
if cb is "on" then
response.write("Yes <br>")
else
response.write("No <br>")
end if
else
response.write("could not find it <br>")
end if

Next

end Sub
------------- Trace Output ------------------

DataGrid1:_ctl2 System.Web.UI.WebControls.DataGridItem 851 0
DataGrid1:_ctl2:_ctl8 System.Web.UI.WebControls.TableCell 142 0
DataGrid1:_ctl2:_ctl0 System.Web.UI.WebControls.TextBox 50 0
DataGrid1:_ctl2:_ctl1 System.Web.UI.WebControls.CheckBox 81 0
DataGrid1:_ctl2:_ctl9 System.Web.UI.WebControls.TableCell 140 0
DataGrid1:_ctl2:_ctl2 System.Web.UI.WebControls.TextBox 50 0
DataGrid1:_ctl2:_ctl3 System.Web.UI.WebControls.CheckBox 81 0
DataGrid1:_ctl2:_ctl10 System.Web.UI.WebControls.TableCell 140 0
DataGrid1:_ctl2:_ctl4 System.Web.UI.WebControls.TextBox 50 0
DataGrid1:_ctl2:_ctl5 System.Web.UI.WebControls.CheckBox 81 0
DataGrid1:_ctl2:_ctl11 System.Web.UI.WebControls.TableCell 140 0
DataGrid1:_ctl2:_ctl6 System.Web.UI.WebControls.TextBox 50 0
DataGrid1:_ctl2:_ctl7 System.Web.UI.WebControls.CheckBox 81 0
DataGrid1:_ctl2:_ctl12 System.Web.UI.WebControls.TableCell 12 32
DataGrid1:_ctl2:_ctl13 System.Web.UI.WebControls.TableCell 12 32
DataGrid1:_ctl2:_ctl14 System.Web.UI.WebControls.TableCell 14 36
DataGrid1:_ctl2:_ctl15 System.Web.UI.WebControls.TableCell 39 68
DataGrid1:_ctl2:_ctl16 System.Web.UI.WebControls.TableCell 15 36
DataGrid1:_ctl2:_ctl17 System.Web.UI.WebControls.TableCell 16 36
DataGrid1:_ctl2:_ctl18 System.Web.UI.WebControls.TableCell 15 36
DataGrid1:_ctl2:_ctl19 System.Web.UI.WebControls.TableCell 18 40
DataGrid1:_ctl2:_ctl20 System.Web.UI.WebControls.TableCell 11 32
DataGrid1:_ctl2:_ctl21 System.Web.UI.WebControls.TableCell 19 40
DataGrid1:_ctl2:_ctl22 System.Web.UI.WebControls.TableCell 11 32
DataGrid1:_ctl2:_ctl23 System.Web.UI.WebControls.TableCell 11 32
DataGrid1:_ctl2:_ctl24 System.Web.UI.WebControls.TableCell 17 40
DataGrid1:_ctl2:_ctl25 System.Web.UI.WebControls.TableCell 21 44
DataGrid1:_ctl2:_ctl26 System.Web.UI.WebControls.TableCell 15 36
DataGrid1:_ctl2:_ctl27 System.Web.UI.WebControls.TableCell 15 36
DataGrid1:_ctl2:_ctl28 System.Web.UI.WebControls.TableCell 14 36
Name Value
__VIEWSTATE
dDwtMTg3OTE4MzcyMzt0PDtsPGk8MT47PjtsPHQ8O2w8aTwxPj s+O2w8dDxAMDxwPHA8bDxQYWdlQ291bnQ7XyFJdGVtQ291bnQ7 XyF

[ .. more view state data deleted .. ]

DataGrid1:_ctl2:_ctl0
DataGrid1:_ctl2:_ctl1 on
DataGrid1:_ctl2:_ctl2
DataGrid1:_ctl2:_ctl4
DataGrid1:_ctl2:_ctl6
DataGrid1:_ctl3:_ctl0
DataGrid1:_ctl3:_ctl1 on
DataGrid1:_ctl3:_ctl2
DataGrid1:_ctl3:_ctl4
DataGrid1:_ctl3:_ctl6

Phillip Williams wrote:
Sydney,

Set the AutoGenerateColumns = false
http://msdn2.microsoft.com/en-us/lib...tecolumns.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"sy********@gmail.com" wrote:
Phillip,

Thanks for the response. I have made some progress after examing the
article under this link you provided:

http://msdn.microsoft.com/library/de...ynamically.asp

then click on "Creating Templates Programmatically in the DataGrid
Control". I decided to use the DataGrid control.

and here's an excerpt from this article:

' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim tc1 As New TemplateColumn()
tc1.HeaderTemplate = New _
DataGridTemplate(ListItemType.Header, "Column1")
tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, _
"Column1")
tc1.EditItemTemplate = New _
DataGridTemplate(ListItemType.EditItem, "Column1")
tc1.FooterTemplate = New _
DataGridTemplate(ListItemType.Footer, "Column1")
DataGrid1.Columns.Add(tc1)

Dim tc2 As New TemplateColumn()
tc2.HeaderTemplate = New _
DataGridTemplate(ListItemType.Header, "Column2")
tc2.ItemTemplate = New _
DataGridTemplate(ListItemType.Item, "Column2")
tc2.EditItemTemplate = New _
DataGridTemplate(ListItemType.EditItem, "Column2")
tc2.FooterTemplate = New _
DataGridTemplate(ListItemType.Footer, "Column2")
DataGrid1.Columns.Add(tc2)
SqlDataAdapter1.Fill(DsCategories1)
DataGrid1.DataBind()
End Sub

' Visual Basic
Private Class DataGridTemplate
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String

Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub

Sub InstantiateIn(ByVal container As Control) _
Implements ITemplate.InstantiateIn
Dim lc As New Literal()
Select Case templateType
Case ListItemType.Header
lc.Text = "<B>" & columnName & "</B>"
container.Controls.Add(lc)
Case ListItemType.Item
lc.Text = "Item " & columnName
container.Controls.Add(lc)
Case ListItemType.EditItem
Dim tb As New TextBox()
tb.Text = ""
container.Controls.Add(tb)
Case ListItemType.Footer
lc.Text = "<I>Footer</I>"
container.Controls.Add(lc)
End Select
End Sub
End Class
My question is: why it produced four additional columns instead of two
since the code asked to dynamically allocate two
more columns in addition to the data columns coming back from the
database? This is what it looks like without my data
columns showing.

Column1 Column2 Column1 Column2
Item Column1 Item Column2 Item Column1 Item Column2

-Sydney
Phillip Williams wrote:
You can design your templates programmatically.

This demo creates templates programmatically in asp.net 2.0
http://www.webswapp.com/codesamples/...e/default.aspx

This does the same concepts in asp.net 1.1
http://www.societopia.net/Samples/Da...edColumns.aspx

And this is and MSDN reference to do it in a repeater in asp.net.1.
http://msdn.microsoft.com/library/de...ynamically.asp
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"sy********@gmail.com" wrote:

> Hello,
>
> I would greatly appreciate if someone can show me how to dynamically
> build a Repeater
> with unknown number of columns at design time. I have looked various
> threads in this
> newsgroup, websites, MSDN and was not able to find something that would
> help me understand and code. I might not be searching for the right
> words or phrases. So if you know how to do this or know of links or
> websites that have information about this, please post them here.
>
> Thank you in advance !
>
> -Sydney
>
>



Apr 3 '06 #5

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

Similar topics

3
by: JT | last post by:
what is the correct way to dynamically build a call to a function in asp? i have the name of the function i want to call in a dictionary object, this works to execute the function but the...
0
by: Trillium | last post by:
I am trying to upgrade a straight ASP project into .NET, but I am new at ..NET, and there seem to be so many different ways to do things. I am new to ..NET and need some guidance as to which...
3
by: DaveF | last post by:
I need to put it in a specific place. Here is the old code switch(CAMERA_TYPE_ID) { case "0": case "7": // this is an image
1
by: Mark Fox | last post by:
Hello, I have a repeater and in each itemtemplate I have a radiobuttonlist. I am attempting to figure out how on postback I could iterate through the rows displayed by the repeater and for...
3
by: Simon | last post by:
Is it possible in VB to dynamically build a function call? eg Something like: strParams = strParam(0) & ", " & strParam(1) strFunc = "Logon" if not( frmMain.objMxAPI.strFunc( strParams )) ...
2
by: jack | last post by:
Hello, I need to dynamically add menu items to an existing menu on an MDI form. In the form load, when I create the menu items then add it to the menu control using the Add method, the entire...
0
by: mmumme | last post by:
Ok, here is what I am trying to do. I am building an online reporting tool where on the left side of the screen I have a listbox with fields available to report on. When a customer clicks on a...
1
by: james | last post by:
Hi guys, Does anyone know how to dynamically build a data entry form of the format Label: TextBox Label2: TextBox2 I have a datatable with columns that define the types and names. I guess...
2
by: Newbe developer | last post by:
I have a question how I can evaluate the item values. I want to know which radiobutton is true in order to save the value it represents. the code is this: <asp:Repeater id="futuregames"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.