Solved

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

Posted on 2010-09-09
11
937 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c#, case, if 4 39
.NET universe documentation poster 2 26
XML Constructor Throwing Incorrectly Structured Document Error 4 25
Javascript function 3 23
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 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