Solved

How can I dynamically set attributes in ASP.NET Repeater?

Posted on 2010-09-09
11
917 Views
Last Modified: 2012-06-27
I have the anchor below in a Repeater. If "calLink" contains "keepThis", I need to do two things:

1) Set class="thickbox".
2) Remove target attribute.

Any slick ideas on how to accomplish this?
<a href='<%# DataBinder.Eval(Container.DataItem, "calLink") %>' target='<%# DataBinder.Eval(Container.DataItem, "calTarget") %>' onclick='<%# DataBinder.Eval(Container.DataItem, "calOnClick") %>'><%# DataBinder.Eval(Container.DataItem, "calDate", "{0:MMMM d}") %>: <%# DataBinder.Eval(Container.DataItem, "calTitle") %></a>

Open in new window

0
Comment
Question by:mrcoulson
  • 5
  • 3
  • 3
11 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33637073
I think you are going to have to resort to using the ItemDataBound event of the repeater to do what you want. The repeater markup isn't really sophisticated enough performing complex logic.
0
 

Author Comment

by:mrcoulson
ID: 33637155
Okay, I've got my function, but it's empty because I don't know how to proceed.  Any suggestions?

Jeremy
0
 
LVL 11

Expert Comment

by:govindarajan78
ID: 33637188
have two anchor controls and make the visibility false based on the value. below is the sample we use to display/hide panel

<asp:Panel id="liv" runat="server" visible='<%#(DataBinder.Eval(Container.DataItem, "calLink").ToString()=="keepThis") %>'>

make sure the visible property is in single quotes not double quotes.

hope this helps
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:mrcoulson
ID: 33637314
govindarajan78: I'm going to try this, but I think I foresee a problem.  calLink will either contain or not contain "keepThis"; calLink == keepThis will never be true.

Jeremy
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 33637322
Firstly you'll need to change you'll need to add an "id" and "runat" attribute to your anchor so that you can access it from code-behind. The specifics depend on what type of item you are binding to, but the little sample that follows binds to a custom class of type "Wibble", which should hopefully demonstrate the concept at least:
<!-- Markup -->
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
    <ItemTemplate>
        
        <div>
            <a id="MyLink" runat="server" href='<%# DataBinder.Eval(Container.DataItem, "Url") %>'><%# DataBinder.Eval(Container.DataItem, "FullName") %></a>
        </div>
        
    </ItemTemplate>
</asp:Repeater>

<!-- Code-Behind -->
protected void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    // check we are binding a data row
    if (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item || e.Item.ItemType == System.Web.UI.WebControls.ListItemType.AlternatingItem)
    { 
        // grap the anchor tag for the current row
        HtmlAnchor anchor = e.Item.FindControl("MyLink") as HtmlAnchor;

        // grab the data source for the row
        Wibble data = e.Item.DataItem as Wibble;

        // chack some property and modify the anchor
        if (data.Url == "Two")
            anchor.Attributes.Add("style", "color:red;");
    }
}

Open in new window

0
 
LVL 11

Expert Comment

by:govindarajan78
ID: 33637353
thats right @jeremy that why i said 2 anchor controls. make sure those are server controls other wise visible property will not work
0
 

Author Comment

by:mrcoulson
ID: 33637412
govindarajan78: The code attached makes neither anchor show.

carl_tawn: Can I add an ID when the repeater could make many of the anchors?

Jeremy
<a runat="server" href='<%# DataBinder.Eval(Container.DataItem, "calLink") %>' target='<%# DataBinder.Eval(Container.DataItem, "calTarget") %>' onclick='<%# DataBinder.Eval(Container.DataItem, "calOnClick") %>' visible='<%#(DataBinder.Eval(Container.DataItem, "calLink").ToString()=="keepThis") %>'><%# DataBinder.Eval(Container.DataItem, "calDate", "{0:MMMM d}") %>: <%# DataBinder.Eval(Container.DataItem, "calTitle") %></a>
                    	                                    <a runat="server" href='<%# DataBinder.Eval(Container.DataItem, "calLink") %>' class="thickbox" visible='<%#(DataBinder.Eval(Container.DataItem, "calLink").ToString()=="keepThis") %>'><%# DataBinder.Eval(Container.DataItem, "calDate", "{0:MMMM d}") %>: <%# DataBinder.Eval(Container.DataItem, "calTitle") %></a>

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33637483
Yes. Each one will be given a unique ID in the outputted markup. The ItemDataBound event fires once for each row in the datasource so you are always dealing with the anchor tag in the context row.
0
 
LVL 11

Expert Comment

by:govindarajan78
ID: 33637729
@jeremy:
i never tried in anchor tag this is what i used in my project

<asp:Panel id="liv" runat="server" visible='<%#(DataBinder.Eval(Container.DataItem, "show").ToString()=="1") %>'>
<div style="text-align:center;color:#ff0000;">Live</div>                      
 </asp:Panel>
<asp:Panel ID="upc" runat="server" visible='<%#(DataBinder.Eval(Container.DataItem, "show").ToString()=="0") %>'>                             
<div style="text-align:center;color:#ff0000;">Not Live</div>                        
</asp:Panel>  
0
 

Author Comment

by:mrcoulson
ID: 33637751
Okay, I've compared both solutions and I'm into Carl's more.  It is simpler to read and seems more flexible for my needs.  Thanks for your input and education, govindarajan78!

Jeremy
0
 

Author Closing Comment

by:mrcoulson
ID: 33637763
Here's what I ended up using. It's very similar to Carl's. Thanks!

protected void rptCalendarSelection_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            // grap the anchor tag for the current row
            System.Web.UI.HtmlControls.HtmlAnchor anchor = e.Item.FindControl("calendarLink") as System.Web.UI.HtmlControls.HtmlAnchor;

            // chack some property and modify the anchor
            if (anchor.HRef.Contains("keepThis"))
            {
                anchor.Attributes.Add("class", "thickbox");
            }

        }
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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