Dynamically build Repeater Items - How??

S

sydney.luu

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
 
S

sydney.luu

Phillip,

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

http://msdn.microsoft.com/library/d...atingwebservercontroltemplatesdynamically.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
 
S

sydney.luu

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
dDwtMTg3OTE4MzcyMzt0PDtsPGk8MT47PjtsPHQ8O2w8aTwxPjs+O2w8dDxAMDxwPHA8bDxQYWdlQ291bnQ7XyFJdGVtQ291bnQ7XyF

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

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,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top