在我的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;
}
}