Dynamically setting values in a repeater

J

John Haycock

Hi All

I have a user control that uses a repeater to build a list of menu links:
<itemtemplate>
<li>
<a href='news.aspx?newsID=<%# DataBinder.Eval(Container.DataItem,"newsID")
%>'>
<%# DataBinder.Eval(Container.DataItem, "newsHeadline") %>
</a>
</li>
</itemtemplate>

I want to reuse this on many pages but I can't figure out how to dynamically
change or build the url in the repeater's itemTemplate. E.g if the menu was
on the projects page the url would need to be:
<a href='projects.aspx?projectID=<%#
DataBinder.Eval(Container.DataItem,"projectID") % >'>
<%# DataBinder.Eval(Container.DataItem, "projectHeadline") %>
</a>
Cheers
John
 
G

Guest

I created a simple example (ready to use) that sholuld explain the concept.
Example consist of two files: user control and page. in order to simplify the
code i used script runat server instead of code behind.

-- BEGIN USER CONTROL

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="MenuLinks.ascx.cs" Inherits="MenuLinks" %>

<script runat="server">

/// <summary>
///
/// </summary>
public string DataValueField
{
get
{
return (string)ViewState["DataValueField"];
}
set
{
ViewState["DataValueField"] = value;
}
}

/// <summary>
///
/// </summary>
public string DataValueFormatString
{
get
{
string value = (string) ViewState["DataValueFormatString"];
return value == null ? "{0}" : value;
}
set
{
ViewState["DataValueFormatString"] = value;
}
}

/// <summary>
///
/// </summary>
public string DataTextField
{
get
{
return (string)ViewState["DataTextField"];
}
set
{
ViewState["DataTextField"] = value;
}
}

/// <summary>
///
/// </summary>
public object DataSource
{
get
{
return links.DataSource;
}
set
{
links.DataSource = value;
}
}

</script>

<asp:Repeater runat="server" ID="links">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<a href='<%# String.Format(DataValueFormatString,
DataBinder.Eval(Container.DataItem, DataValueField)) %>'>
<%# DataBinder.Eval(Container.DataItem, DataTextField) %>
</a>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>


-- END USER CONTROL


-- BEGIN ASPX PAGE

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs"
Inherits="Default3" %>

<%@ Register Src="MenuLinks.ascx" TagName="MenuLinks" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// news menu
MenuLinks1.DataSource = GenerateDataSource("NewsId", "NewsHeadline", 10);
MenuLinks1.DataBind();

// project menu
MenuLinks2.DataSource = GenerateDataSource("ProjectId",
"ProjectHeadline", 10);
MenuLinks2.DataBind();
}
}

/// <summary>
///
/// </summary>
/// <param name="idColumn"></param>
/// <param name="descriptionColumn"></param>
/// <param name="rowCount"></param>
/// <returns></returns>
private System.Data.DataTable GenerateDataSource(string idColumn, string
descriptionColumn, int rowCount)
{
System.Data.DataTable dataTable = new System.Data.DataTable();
System.Data.DataRow dataRow;

dataTable.Columns.Add(idColumn, typeof(int));
dataTable.Columns.Add(descriptionColumn, typeof(string));

for (int i = 0; i < rowCount; i++)
{
dataRow = dataTable.NewRow();
dataRow[0] = i;
dataRow[1] = "description" + i.ToString();
dataTable.Rows.Add(dataRow);
}

return dataTable;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:MenuLinks ID="MenuLinks1" runat="server"
DataTextField="NewsHeadline" DataValueField="NewsId"
DataValueFormatString="news.aspx?newsid={0}"/>

<uc1:MenuLinks ID="MenuLinks2" runat="server"
DataTextField="ProjectHeadline" DataValueField="ProjectId"
DataValueFormatString="projects.aspx?projectid={0}"/>

</div>
</form>
</body>
</html>


-- END ASPX PAGE


Milosz Skalecki
MCP, MCAD
 
J

John Haycock

Thank you Milosz for your detailed answer

I shall try and digest this and feedback in the next day or so

John


Milosz Skalecki said:
I created a simple example (ready to use) that sholuld explain the concept.
Example consist of two files: user control and page. in order to simplify
the
code i used script runat server instead of code behind.

-- BEGIN USER CONTROL

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="MenuLinks.ascx.cs" Inherits="MenuLinks" %>

<script runat="server">

/// <summary>
///
/// </summary>
public string DataValueField
{
get
{
return (string)ViewState["DataValueField"];
}
set
{
ViewState["DataValueField"] = value;
}
}

/// <summary>
///
/// </summary>
public string DataValueFormatString
{
get
{
string value = (string) ViewState["DataValueFormatString"];
return value == null ? "{0}" : value;
}
set
{
ViewState["DataValueFormatString"] = value;
}
}

/// <summary>
///
/// </summary>
public string DataTextField
{
get
{
return (string)ViewState["DataTextField"];
}
set
{
ViewState["DataTextField"] = value;
}
}

/// <summary>
///
/// </summary>
public object DataSource
{
get
{
return links.DataSource;
}
set
{
links.DataSource = value;
}
}

</script>

<asp:Repeater runat="server" ID="links">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<a href='<%# String.Format(DataValueFormatString,
DataBinder.Eval(Container.DataItem, DataValueField)) %>'>
<%# DataBinder.Eval(Container.DataItem, DataTextField) %>
</a>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>


-- END USER CONTROL


-- BEGIN ASPX PAGE

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs"
Inherits="Default3" %>

<%@ Register Src="MenuLinks.ascx" TagName="MenuLinks" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// news menu
MenuLinks1.DataSource = GenerateDataSource("NewsId", "NewsHeadline", 10);
MenuLinks1.DataBind();

// project menu
MenuLinks2.DataSource = GenerateDataSource("ProjectId",
"ProjectHeadline", 10);
MenuLinks2.DataBind();
}
}

/// <summary>
///
/// </summary>
/// <param name="idColumn"></param>
/// <param name="descriptionColumn"></param>
/// <param name="rowCount"></param>
/// <returns></returns>
private System.Data.DataTable GenerateDataSource(string idColumn, string
descriptionColumn, int rowCount)
{
System.Data.DataTable dataTable = new System.Data.DataTable();
System.Data.DataRow dataRow;

dataTable.Columns.Add(idColumn, typeof(int));
dataTable.Columns.Add(descriptionColumn, typeof(string));

for (int i = 0; i < rowCount; i++)
{
dataRow = dataTable.NewRow();
dataRow[0] = i;
dataRow[1] = "description" + i.ToString();
dataTable.Rows.Add(dataRow);
}

return dataTable;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:MenuLinks ID="MenuLinks1" runat="server"
DataTextField="NewsHeadline" DataValueField="NewsId"
DataValueFormatString="news.aspx?newsid={0}"/>

<uc1:MenuLinks ID="MenuLinks2" runat="server"
DataTextField="ProjectHeadline" DataValueField="ProjectId"
DataValueFormatString="projects.aspx?projectid={0}"/>

</div>
</form>
</body>
</html>


-- END ASPX PAGE


Milosz Skalecki
MCP, MCAD


John Haycock said:
Hi All

I have a user control that uses a repeater to build a list of menu links:
<itemtemplate>
<li>
<a href='news.aspx?newsID=<%#
DataBinder.Eval(Container.DataItem,"newsID")
%>'>
<%# DataBinder.Eval(Container.DataItem, "newsHeadline") %>
</a>
</li>
</itemtemplate>

I want to reuse this on many pages but I can't figure out how to
dynamically
change or build the url in the repeater's itemTemplate. E.g if the menu
was
on the projects page the url would need to be:
<a href='projects.aspx?projectID=<%#
DataBinder.Eval(Container.DataItem,"projectID") % >'>
<%# DataBinder.Eval(Container.DataItem, "projectHeadline") %>
</a>
Cheers
John
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top