jimbona27
asked on
formatting repeater data in markup
hi there,
hope someone can help here, im trying to use a repeater although Im not sure the correct way to format the data within the markup. Simple example below. My repeater that creates a hyperlink for each record I have from the database. The repeater creates a list of links with the url address and the text link being the same.
<asp:Repeater ID="test" runat="server">
<ItemTemplate>
<li><a href="<%# Container.DataItem %>" /><%# Container.DataItem %>
</a> </li>
</ItemTemplate>
</asp:Repeater>
This is fine normally but I want to change the format of the data for the url address.
to create html like this
<ul>
<li><a href="hello_there">hello there</a>
<li><a href="hello_again">hello again</a>
<li><a href="hello_today">hello today</a>
<li><a href="hello_to">hello to</a>
</ul>
im not sure whether I should be formatting the data in the code behind file or within the markup file?
Is it possible for the below:
<li><a href="<%# Replace(" ","_",Container.DataItem) %>" /><%# Container.DataItem %>
</a> </li>
or can I format the dataitem in the code behind file?
Many thanks,
hope someone can help here, im trying to use a repeater although Im not sure the correct way to format the data within the markup. Simple example below. My repeater that creates a hyperlink for each record I have from the database. The repeater creates a list of links with the url address and the text link being the same.
<asp:Repeater ID="test" runat="server">
<ItemTemplate>
<li><a href="<%# Container.DataItem %>" /><%# Container.DataItem %>
</a> </li>
</ItemTemplate>
</asp:Repeater>
This is fine normally but I want to change the format of the data for the url address.
to create html like this
<ul>
<li><a href="hello_there">hello there</a>
<li><a href="hello_again">hello again</a>
<li><a href="hello_today">hello today</a>
<li><a href="hello_to">hello to</a>
</ul>
im not sure whether I should be formatting the data in the code behind file or within the markup file?
Is it possible for the below:
<li><a href="<%# Replace(" ","_",Container.DataItem) %>" /><%# Container.DataItem %>
</a> </li>
or can I format the dataitem in the code behind file?
Many thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
the error refers to the markup where I have added the onitembound event attribute within the repeater markup
Can you post the code?
cs and aspx please
cs and aspx please
ASKER
thanks, will do when I can..
ASKER
ok backup now,
markup code:
<ul>
<asp:Repeater ID="test" OnDataBinding="test_ItemDa taBound" runat="server">
<ItemTemplate>
<li><a href="/<%# Container.DataItem %>/" />
<%# Container.DataItem %>
</a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
code behind:
protected void test_ItemDataBound(object sender, DataListItemEventArgs e)
{
Response.Write("s");
}
markup code:
<ul>
<asp:Repeater ID="test" OnDataBinding="test_ItemDa
<ItemTemplate>
<li><a href="/<%# Container.DataItem %>/" />
<%# Container.DataItem %>
</a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
code behind:
protected void test_ItemDataBound(object sender, DataListItemEventArgs e)
{
Response.Write("s");
}
ASKER
i get an error referring to the markup repeater line:
no overload for test_itemdatabound matches delegate system.eventhandler.
no overload for test_itemdatabound matches delegate system.eventhandler.
ASKER
got it working now although im stuck with the following:
if (e.Item.ItemType == ListItemType.Item)
{
/// replace spaces with underscores..
}
using the following markup I get an error referring to the name does not exist...
<%# DataBinder.Eval(Container. DataItem, "name") %>
if (e.Item.ItemType == ListItemType.Item)
{
/// replace spaces with underscores..
}
using the following markup I get an error referring to the name does not exist...
<%# DataBinder.Eval(Container.
ASKER
ok...............
got around it,
using
is this an okay way of doing it or is the on data bind event a better approach?
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("name", typeof(String)));
dt.Columns.Add(new DataColumn("url", typeof(String)));
// execute query
System.Data.OleDb.OleDbCon nection conn = new System.Data.OleDb.OleDbCon nection();
conn.ConnectionString = @"...";
conn.Open();
OleDbDataReader reader = null;
OleDbCommand cmd = new OleDbCommand("SELECT top 10 * FROM type", conn);
reader = cmd.ExecuteReader();
int counter = 0;
string test= string.Empty;
// populate datatable with DB info
while (reader.Read())
{
test= (string)reader["Name"];
dr = dt.NewRow();
dr[0] = test;
dr[1] = test.Replace(" ", "-");
dt.Rows.Add(dr);
counter++;
}
// bind data
DataList1.DataSource = dt;
DataList1.DataBind();
markup...
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<li><a href="x/<%# DataBinder.Eval(Container. DataItem, "url") %>/" />
<%# DataBinder.Eval(Container. DataItem, "name") %></a></li>
</ItemTemplate></asp:DataL ist>
got around it,
using
is this an okay way of doing it or is the on data bind event a better approach?
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("name", typeof(String)));
dt.Columns.Add(new DataColumn("url", typeof(String)));
// execute query
System.Data.OleDb.OleDbCon
conn.ConnectionString = @"...";
conn.Open();
OleDbDataReader reader = null;
OleDbCommand cmd = new OleDbCommand("SELECT top 10 * FROM type", conn);
reader = cmd.ExecuteReader();
int counter = 0;
string test= string.Empty;
// populate datatable with DB info
while (reader.Read())
{
test= (string)reader["Name"];
dr = dt.NewRow();
dr[0] = test;
dr[1] = test.Replace(" ", "-");
dt.Rows.Add(dr);
counter++;
}
// bind data
DataList1.DataSource = dt;
DataList1.DataBind();
markup...
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<li><a href="x/<%# DataBinder.Eval(Container.
<%# DataBinder.Eval(Container.
</ItemTemplate></asp:DataL
ASKER