473,395 Members | 1,797 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,395 software developers and data experts.

Dynamically setting values in a repeater

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

Oct 9 '06 #1
2 5541
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" wrote:
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

Oct 9 '06 #2
Thank you Milosz for your detailed answer

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

John
"Milosz Skalecki" <mi*****@REMOVEITwp.plwrote in message
news:8E**********************************@microsof t.com...
>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" wrote:
>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



Oct 10 '06 #3

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

Similar topics

12
by: Jerad Rose | last post by:
I searched for a while trying to find the answer to this, but to no avail. I am trying to find the best way (or any way) to dynamically show and hide groups of TR's. For example, I have a...
2
by: Frank | last post by:
Hi alll, How can I bind a dynamically created dataTable to a repeater? I do not know the number of columns and the column names at design time. I try to do the same thing as for datagrid:...
0
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
1
by: MattB | last post by:
OK, never mind my last post. It was easy enough to refer to the table the repeater is bound to, but I made a big, incorrect assumption in that post. In my last post I thought I was successfully...
0
by: dutone | last post by:
I have searched high and low for a solution to this and I dont think its possible.... but I hope not. What I have is a list of items with various values that can be changed via a drop down. Each...
4
by: sydney.luu | last post by:
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,...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
1
by: hardieca | last post by:
Hi, I'm building a multi-lingual CMS. The user can add as many languages as he likes. The user will be able to create sections for different content (General, News Releases, etc...) in the db...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.