Solved

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

Posted on 2010-09-09
11
910 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

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

 
LVL 11

Expert Comment

by:govindarajan78
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

12 Experts available now in Live!

Get 1:1 Help Now