Solved

Add new Attributes at runtime to Repeater control

Posted on 2007-11-16
8
2,813 Views
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


Thanks!
0
Comment
Question by:knowlton
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:McExp
Comment Utility
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">
    </asp:BulletedList>
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
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?
0
 
LVL 16

Expert Comment

by:McExp
Comment Utility
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.
0
 
LVL 16

Accepted Solution

by:
McExp earned 500 total points
Comment Utility
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)?');");

    }
 

</script>
 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <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">

    <ItemTemplate>

    <asp:LinkButton ID="lbTesting" CommandArgument='<%# Eval("TeamId") %>' runat="server"><%# Eval("TeamName") %></asp:LinkButton>

    </ItemTemplate>

    </asp:Repeater>

    </form>

</body>

</html>

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 5

Author Comment

by:knowlton
Comment Utility
McExp:

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

Expert Comment

by:McExp
Comment Utility
Yes, what is your criteria?

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

Author Comment

by:knowlton
Comment Utility
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.

Tom
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
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?');");
            }
        }
    }
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now