Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Add new Attributes at runtime to Repeater control

Posted on 2007-11-16
Last Modified: 2012-06-27
After the datasource has been assigned to a repeater control, is it possible, in the C# code behind, to add attributes to certain LinkButtons (the repeater is a list of LinkButtons)?

LinkButton.Attributes.Add("onClick", "return confirm('Are you sure you want to delete this item (and sub items if this is a folder)?');");

but only on certain ones, not on all of them

Question by:knowlton
  • 4
  • 4
LVL 16

Expert Comment

ID: 20302712
The simplest solution would be to replace your repeater of LinkButtons into a bullited list of LinkButtons, this will enable you to (from server side code) itterate over the BulitedList Controls item collection, see below for an example: -

    <asp:BulletedList ID="BulletedList1" runat="server" DisplayMode="LinkButton">

Author Comment

ID: 20304381
As long as I could still build it from a database that would be fine.

Not very familiar with a bulleted list or the  advantages over a repeater.

Why wouldn't a repeater work?
LVL 16

Expert Comment

ID: 20305533
Apologies, The BullitedList will not give you the functionality you require. It does give you a neat way of creating a list of link buttons however it doens't expose them to allow you to apply an on click function.

I'll work up an example using the Repeater class.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 16

Accepted Solution

McExp earned 500 total points
ID: 20305590
See the code below for an example of how to get access to your LinkButton during the ItemDataBound event
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        string itemToProcess = e.CommandArgument.ToString();
        //Now Do Something?
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        LinkButton lbTempTest = e.Item.FindControl("lbTesting") as LinkButton;
        lbTempTest.Attributes.Add("onClick", "return confirm('Are you sure you want to delete this item (and sub items if this is a folder)?');");
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Main %>" ProviderName="<%$ ConnectionStrings:Main.ProviderName %>" SelectCommand="SELECT [TeamName], [TeamId] FROM [Teams]"></asp:SqlDataSource>
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound">
    <asp:LinkButton ID="lbTesting" CommandArgument='<%# Eval("TeamId") %>' runat="server"><%# Eval("TeamName") %></asp:LinkButton>

Open in new window


Author Comment

ID: 20306676

Does your solution allow me to apply the Attribute to specific rows of the repeater?
LVL 16

Expert Comment

ID: 20307460
Yes, what is your criteria?

All you need to do is add a conditional statment around the  "lbTempTest.Attributes.Add" statment

Author Comment

ID: 20308269
I think my criteria will be based on LinkButton "Text"

If "LinkButton.Text == <some value>" then add attribute

That sort of thing.

I think what you have already provided me should be enough!!

I'll know for sure when I get in to work on Monday.


Author Comment

ID: 20316526
For my future reference:

    protected void FirstUseStepsStatus_ItemDataBound(object sender, RepeaterItemEventArgs e)
        LinkButton lbTempTest = e.Item.FindControl("LinkButtonStep") as LinkButton;
        if (lbTempTest != null)
            if (lbTempTest.CommandArgument == "2")
                lbTempTest.Attributes.Add("onClick", "return confirm('Do you want to add another Debt Item?');");

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delete HTML table rows 12 35
Deploying to Azure 3 39
C# LINQ ForEach() question 6 54
using CK editor in iframes 5 18
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question