Repeater + Getting Column Names at Runtime

B

Brent

My SQL might generate a result like this:


Security 2005-06-30 2005-03-31 2004-12-31
IBM $65 $100 $123
(etc)

or like this:

Security 2005-06-30 2005-03-31
IBM $65 $100
(etc)

In other words, I don't know the number of columns until runtime. This
presents a problem in my repeater logic.

In the past I might do something like this:

==========================

<asp:Repeater id="dl" runat="server" EnableViewState="false">
<ItemTemplate>

<%# DataBinder.Eval(Container.DataItem, "Security")
<%# DataBinder.Eval(Container.DataItem, "2005-06-30") %>
<%# DataBinder.Eval(Container.DataItem, "2005-03-31") %>
<%# DataBinder.Eval(Container.DataItem, "2005-12-31") %>

</ItemTemplate>
</asp:Repeater>

==========================


This repeater will fail, however, if I have more or fewer date columns
than indicated.

So, the question is: how do I specify the number of columns at runtime?
Can I do a loop inside my repeater somehow?

I'd sure appreciate any help!

--Brent
 
G

Guest

Brent" <""b b i g l e r "@ y a h o o . said:
My SQL might generate a result like this:


Security 2005-06-30 2005-03-31 2004-12-31
IBM $65 $100 $123
(etc)

or like this:

Security 2005-06-30 2005-03-31
IBM $65 $100
(etc)

In other words, I don't know the number of columns until runtime. This
presents a problem in my repeater logic.

In the past I might do something like this:

==========================

<asp:Repeater id="dl" runat="server" EnableViewState="false">
<ItemTemplate>

<%# DataBinder.Eval(Container.DataItem, "Security")
<%# DataBinder.Eval(Container.DataItem, "2005-06-30") %>
<%# DataBinder.Eval(Container.DataItem, "2005-03-31") %>
<%# DataBinder.Eval(Container.DataItem, "2005-12-31") %>

</ItemTemplate>
</asp:Repeater>

==========================


This repeater will fail, however, if I have more or fewer date columns
than indicated.

So, the question is: how do I specify the number of columns at runtime?
Can I do a loop inside my repeater somehow?

I'd sure appreciate any help!

--Brent

You can create 2 repeater templates (e.g., MyTemplate1 and MyTemplate2)
dynamically as in this MSDN sample:
http://msdn.microsoft.com/library/d...atingwebservercontroltemplatesdynamically.asp

At run time, based on which dataset you would databind, you would assign
which template to use:

Repeater1.ItemTemplate = new MyTemplate1(ListItemType.Item);
or
Repeater1.ItemTemplate = new MyTemplate2(ListItemType.Item);
 
G

Guest

perhaps build the html in the repeater from the codebehind in the
ITEM_DATABOUND event of the repeater, instead of trying to do it all from the
ASPX view?
 
S

S. Justin Gengo

Brent,

Don't create any columns before runtime. Then use the Repeater's
OnItemDatabound event (which fires as each row of the datasource is gone
through) to check which columns are present. Then you may dynamically add
the columns from there.

It may help you to see some code from my website that shows how to make a
datagrid's rows clickable.

Private Sub DataGrid1_ItemDataBound(ByVal sender As System.Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Try
Dim itemType As ListItemType = e.Item.ItemType

If ((itemType = ListItemType.Pager) Or (itemType = ListItemType.Header) Or
(itemType = ListItemType.Footer)) Then
Return
Else
Dim button As LinkButton = CType(e.Item.Cells(3).Controls(0), LinkButton)
e.Item.Attributes("onClick") = Page.GetPostBackClientHyperlink(button, "")
e.Item.Attributes.Add("onMouseOver",
"javascript:this.style.cursor='pointer';")
e.Item.Attributes.Add("omMouseOut",
"javascript:this.style.cursor='normal';")
End If
Catch ex As Exception
Call ProcessError("There was an error setting up the datagrid.", ex)
End Try
End Sub

Note that "e" is the datagrid's row. The repeater's items will be accessed
similarly. Sorry, but I don't have any code that uses a repeater. But I'm
hoping this will give you some ideas and some keywords to google with.


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
B

Brent

Thanks for your suggestions, guys! I'll definitely look into the
onItemBound property. I think that'll fit the bill...

--Brent
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top