Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Add new Attributes at runtime to Repeater control

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!
Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

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>
Avatar of Tom Knowlton

ASKER

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?
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.
ASKER CERTIFIED SOLUTION
Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
McExp:

Does your solution allow me to apply the Attribute to specific rows of the repeater?
Yes, what is your criteria?

All you need to do is add a conditional statment around the  "lbTempTest.Attributes.Add" statment
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
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?');");
            }
        }
    }