在我的first question中,我需要添加超链接到gridview。现在我已经在Ajax Accordion中包装了相同的网格视图。不幸的是,它使我的方法添加超链接无用,导致OnRowDataBound方法返回以下错误:在Ajax手风琴中添加超链接到gridview
“无法强制类型为'System.Web.UI.LiteralControl'的对象类型为System.Web.UI.WebControls.HyperLink ”“。
就行了:
HyperLink nameHl = (HyperLink)e.Row.Cells[0].Controls[0];
现在,我已经试过:
NavigateUrl='<%# "http://websitelink" + DataBinder.Eval(Container.DataItem,"Name") %>'
的方式,而是用链接的后缀的命名约定,以及如何我需要更换带“+”的黑色空格。这并不适合我的项目。此外,网站链接可能并不总是相同的,所以我宁愿在服务器端定义超链接而不是客户端。
任何帮助将不胜感激。我的代码如下:
客户端:
<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>
服务器端:
// 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;
}
}