Render hyperlink control in Repeater
I've got a repeater and it is bound to a dictionary . Although I can access the HyperLink, I can't render one. I have this code:
<%# DataBinder.Eval((System.Collections.Generic.KeyValuePair<string, HyperLink>)Container.DataItem, "Value.NavigateUrl") %>
The Value.NavigateUrl
was a test to see if I could access that property, and I can. The output is the URL the hyperlink to link to. I also tried 'Text', which worked. This means that it's recognized as a HyperLink, and can 开发者_开发技巧be accessed as one, but I would like to render it as one. How can I do this?
There may be a more elegant solution, but this is what I came up with, a protected function that calls the RenderControl
method of the Hyperlink control.
In your code behind:
using System.IO;
...
protected string RenderLink(object h)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
HyperLink link = (HyperLink)h;
link.RenderControl(htmlWriter);
return sw.ToString();
}
Then just call that function from your Repeater:
<%# RenderLink(DataBinder.Eval((System.Collections.Generic.KeyValuePair<string, HyperLink>)Container.DataItem, "Value")) %>
Why don't you try to put it on a LITERAL control in the repeater... this will work.
精彩评论