在我的第一个问题中,我开始向网格视图添加超链接。现在,我将相同的网格视图封装在Ajax协议中。不幸的是,它使我添加超链接的方法变得无用,因为OnRowDataBound方法返回以下错误:
"无法将类型为"System.Web.UI.LitectControl"的对象强制转换为类型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;
}
}
我能够通过更换来修复错误
HyperLink nameHl = (HyperLink)e.Row.Cells[0].Controls[0];
带有
HyperLink nameHl = ((HyperLink)(e.Row.FindControl("contentLink")));
asp.net论坛主题的解决方案:将代码绑定的GridView中的字段转换为超链接