c# - How to set title of rendered anchor tags for <asp:BulletedList DisplayMode="HyperLink"/> -
i have bulleted list in asp.net displaymode set hyperlink. when control rendered, links display fine additionally want set title each anchor tag. how do this?
aspx file:
<asp:bulletedlist id="bl1" runat="server" displaymode="hyperlink" datatextfield="anchortext" datavaluefield="url" />
code behind file: in code behind file data binding bl1
list.
list<urldata> listofurls
.
where urldata class has 3 public properties
class urldata { public string url {get; set;} public string anchortext {get; set;} public string titletext {get; set;} //public urldata initialise properties }
you have 2 options:-
option 1:
loop through items inside bulletedlist , add title
attribute this:-
protected void page_prerender(object sender, eventargs e) { foreach (listitem item in bl1.items) { item.attributes["title"] = gettooltip(item.value); } }
obviously have call datasource again respective tool-tip:-
private string gettooltip(string url) { return urldatalist().first(x => x.url == url).titletext; }
assuming, urldatalist()
return list<urldata>
binding bl1
. not best way since querying data twice.
option 2:
you can instead create same behaviour using repeater control (recommended way):-
<asp:repeater id="rptdemo" runat="server"> <headertemplate> <ul> </headertemplate> <itemtemplate> <li> <%# string.format("<a href=\"{0}\" title=\"{1}\">{2}</a>",eval("id"),eval("salary"),eval("country")) %> </li> </itemtemplate> <footertemplate> </ul> </footertemplate> </asp:repeater>
simply bind this:-
protected void page_load(object sender, eventargs e) { if (!page.ispostback) { rptdemo.datasource = urldatalist(); rptdemo.databind(); } }
Comments
Post a Comment