Adding hyperlinks to gridview encased in Ajax Accordion
In my first question I got to adding hyperlinks to a gridview. Now I've encased the same gridview in an Ajax Accordion. Unfortunately, it rendered my method of adding hyperlinks useless cause the OnRowDataBound method returns the following error:
"Unable to cast object of type 'System.Web.UI.LiteralControl' to type System.Web.UI.WebControls.HyperLink'."
on the line:
HyperLink nameHl = (HyperLink)e.Row.Cells[0].Controls[0];
Now I've tried the:
NavigateUrl='<%# "http://websitelink" + DataBinder.Eval(Container.DataItem,"Name") %>'
way, but with the naming convention of the link's suffix and how I need to replace the black spaces with "+". this isn't really going to work for my project. Additionally, the website link may not always be the same, so I would rather have the hyperlink defined on server side than client side.
Any help would be greatly appreciated. My code is as follows:
Client Side:
<asp:Accordion ID="Accordion1" runat="server" FadeTransitions="true" Width="935px"
SuppressHeaderPostbacks="true" OnItemDataBound="Accordion1_ItemDataBound"
CssClass="acc-content" HeaderCssClass="acc-header" HeaderSelectedCssClass="acc-selected" TransitionDuration="250" FramesPerSecond="40" RequireOpenedPane="False">
<HeaderTemplate>
<%#DataBinder.Eval(Container.DataItem,"Rpt_Grouping") %>
</HeaderTemplate>
<ContentTemplate>
<asp:HiddenField ID="hlbl_categoryID" runat="server" Value='<%#DataBinder.Eval(Container.DataItem,"Rpt_Grouping") %>' />
<asp:GridView ID="accGvReportList" runat="server" RowStyle-BackColor="#ededed" RowStyle-HorizontalAlign="Left" AutoGenerateColumns="false" GridLines="None" CellPadding="2" CellSpacing="2" Width="100%" OnRowDataBound="accGvReportList_RowDataBound">
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Report Name" HeaderStyle-BackColor="#d1d1d1" HeaderStyle-ForeColor="#777777">
<ItemTemplate>
<asp:HyperLink ID="contentLink" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Report Description" HeaderStyle-BackColor="#d1d1d1" HeaderStyle-ForeColor="#777777">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "Description")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate></asp:Accordion>
Server Side:
// binds the DB table to the grid inside the accordion tool
protected void Accordion1_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
{
if (e.ItemType == AjaxControlToolkit.AccordionItemType.Content)
开发者_高级运维 {
string listPath = "/subcat%";
string categoryValue = ((HiddenField)e.AccordionItem.FindControl("hlbl_categoryID")).Value;
DataTable dtReportList = objInfoDal.getReportListDetails(listPath, ddlFolders.SelectedValue.ToString(), categoryValue);
GridView grd = new GridView();
grd = (GridView)e.AccordionItem.FindControl("accGvReportList");
grd.DataSource = dtReportList;
grd.DataBind();
}
}
// hyperlink binding by row for first column in gridview in the accordion tool
protected void accGvReportList_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Changes text in the first column into HyperLinks
HyperLinkField nameLink = gvReportList.Columns[0] as HyperLinkField;
string linkPath = "http://websitelink";
if (e.Row.RowType == DataControlRowType.DataRow)
{
//applies a unique suffix to the address depending on the link name
HyperLink nameHl = (HyperLink)e.Row.Cells[0].Controls[0];
string nameText = nameHl.Text;
string linkSuffix = nameText.Replace(" ", "+");
nameHl.NavigateUrl = linkPath + linkSuffix;
}
}
I was able to fix the error by replacing
HyperLink nameHl = (HyperLink)e.Row.Cells[0].Controls[0];
with
HyperLink nameHl = ((HyperLink)(e.Row.FindControl("contentLink")));
Solution credited to asp.net forum topic: Convert a field in a code bound GridView to a hyperlink
精彩评论