Solved

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

Posted on 2010-09-09
11
922 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
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.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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