Добавление гиперссылок в gridview в Ajax Accordion

В моем первом вопросе я добавил гиперссылки на вид сетки. Теперь я заключил тот же вид сетки в Ajax Accordion. К сожалению, это сделало мой метод добавления гиперссылок бесполезным, потому что метод 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;
        }
    }

person silentdrkangel    schedule 05.06.2011    source источник


Ответы (1)


Я смог исправить ошибку, заменив

HyperLink nameHl = (HyperLink)e.Row.Cells[0].Controls[0];

с

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

Решение предоставлено в теме форума asp.net: Преобразование поля в связанном с кодом GridView в гиперссылку< /а>

person silentdrkangel    schedule 07.06.2011