Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-09-09
11
Medium Priority
?
1,075 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

916 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