title: Adding hyperlinks to gridview encased in Ajax Accordion
body: <p>In my <a href="https://stackoverflow.com/questions/5954361/hyperlink-in-gridview-binding-to-wrong-row-by-1">first question</a> I got to adding hyperlinks to a gridview. Now I've encased the same gridview in an Ajax Accordion. Unfortunately, it rendered my method of adding hyperlinks useless cause the OnRowDataBound method returns the following error:</p>
  
  <p>"Unable to cast object of type 'System.Web.UI.LiteralControl' to type System.Web.UI.WebControls.HyperLink'."</p>
  
  <p>on the line:</p>
  
  <p><code>HyperLink nameHl = (HyperLink)e.Row.Cells[0].Controls[0];</code></p>
  
  <p>Now I've tried the:</p>
  
  <p><code>NavigateUrl='&lt;%# "http://websitelink" + DataBinder.Eval(Container.DataItem,"Name") %&gt;'</code></p>
  
  <p>way, but with the naming convention of the link's suffix and how I need to replace the black spaces with "+". this isn't really going to work for my project. Additionally, the website link may not always be the same, so I would rather have the hyperlink defined on server side than client side. </p>
  
  <p>Any help would be greatly appreciated. My code is as follows:</p>
  
  <p>Client Side:</p>
  
  <pre><code>&lt;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"&gt;
   &lt;HeaderTemplate&gt;
          &lt;%#DataBinder.Eval(Container.DataItem,"Rpt_Grouping") %&gt;
   &lt;/HeaderTemplate&gt;
   &lt;ContentTemplate&gt;
          &lt;asp:HiddenField ID="hlbl_categoryID" runat="server" Value='&lt;%#DataBinder.Eval(Container.DataItem,"Rpt_Grouping") %&gt;' /&gt;
          &lt;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"&gt;
                 &lt;Columns&gt;
                       &lt;asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Report Name" HeaderStyle-BackColor="#d1d1d1" HeaderStyle-ForeColor="#777777"&gt;
                             &lt;ItemTemplate&gt;
                                     &lt;asp:HyperLink ID="contentLink" runat="server" Text='&lt;%#DataBinder.Eval(Container.DataItem,"Name") %&gt;' /&gt;
                             &lt;/ItemTemplate&gt;
                       &lt;/asp:TemplateField&gt;
                       &lt;asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Report Description" HeaderStyle-BackColor="#d1d1d1" HeaderStyle-ForeColor="#777777"&gt;
                             &lt;ItemTemplate&gt;
                                     &lt;%#DataBinder.Eval(Container.DataItem, "Description")%&gt;
                             &lt;/ItemTemplate&gt;
                       &lt;/asp:TemplateField&gt;
                 &lt;/Columns&gt;
           &lt;/asp:GridView&gt;
       &lt;/ContentTemplate&gt;&lt;/asp:Accordion&gt;
  </code></pre>
  
  <p>Server Side:</p>
  
  <pre><code>        // 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;
          }
      }
  </code></pre>