添加到Ajax Accordion中包含的gridview的超链接

时间:2011-06-05 16:59:51

标签: ajax gridview hyperlink accordion

在我的first question中,我需要添加到gridview的超链接。现在我在Ajax手风琴中包含了相同的gridview。不幸的是,它使我添加超链接的方法无效,导致OnRowDataBound方法返回以下错误:

“无法将类型为'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;
        }
    }

1 个答案:

答案 0 :(得分:0)

我能够通过替换

来修复错误
HyperLink nameHl = (HyperLink)e.Row.Cells[0].Controls[0];

HyperLink nameHl = ((HyperLink)(e.Row.FindControl("contentLink")));

解决方案归功于asp.net论坛主题:Convert a field in a code bound GridView to a hyperlink