locked
unable to get the hyperlink in gridview -templatefield RRS feed

  • Question

  • User751840860 posted

    the below is my ascx page :

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestingVisualWebPart1.ascx.cs" Inherits="DemoSPProject.TestingVisualWebPart1.TestingVisualWebPart1" %>

    <p>
        &nbsp;</p>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

    <asp:GridView runat="server" ID="grdvushowsites" AutoGenerateColumns="false" OnRowDataBound="grdvushowsites_RowDataBound"
        EnableModelValidation="true" Width="712px" Height="3px">
        <Columns>
            <asp:TemplateField HeaderText="dept name" ItemStyle-Width="15%">
                <ItemTemplate>
                    <asp:Label ID="lbldeptname" runat="server" Text='<%# Eval("DeptName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Dept URL" ItemStyle-Width="15%">
                <ItemTemplate>
                    <asp:HyperLink ID="hlinkdepturl" runat="server" NavigateUrl='<%# Eval("DeptURL") %>' Text='<%# Bind("DeptName") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    my code behind is shown below:

    protected void showsubsites()
            {
                    try
                    {
                        string myurl= SPContext.Current.Web.Url.ToString();

         using(SPSite site = new SPSite(myurl) )
        { 
           foreach(SPWebInfo webInfo in site.AllWebs.WebsInfo) 
           { 

                 _depts.Add(new AllDepts { DeptName= webInfo.Title, DeptURL =webInfo.ServerRelativeUrl, }); 
               //  response.(webInfo.ServerRelativeUrl + ": " + web.Title);  
           } 
        }



                        int count=1;
                  DataTable dt = new DataTable();
                  int rowcount = 0;
          // DataTable dt1=new DataTable();
           dt.Columns.Add(new DataColumn("DeptName", typeof(string)));
           dt.Columns.Add(new DataColumn("DeptURL", typeof(string)));
           DataRow drDept = null;

           foreach (var vardept in _depts)
           { 
              drDept = dt.NewRow();
              drDept["DeptName"] = vardept.DeptName.ToString();
              drDept["DeptURL"] = vardept.DeptURL.ToString();
              dt.Rows.Add(drDept);
           }

        //   foreach (var f

     grdvushowsites.DataSource = dt.DefaultView;
     grdvushowsites.DataBind();
                }
                catch (Exception)
                {
                }
            }

            protected void grdvushowsites_RowDataBound(object sender, GridViewRowEventArgs e)
            {protected void showsubsites()
            {
                    try
                    {
                        string myurl= SPContext.Current.Web.Url.ToString();

         using(SPSite site = new SPSite(myurl) )
        { 
           foreach(SPWebInfo webInfo in site.AllWebs.WebsInfo) 
           { 

                 _depts.Add(new AllDepts { DeptName= webInfo.Title, DeptURL =webInfo.ServerRelativeUrl, }); 
               //  response.(webInfo.ServerRelativeUrl + ": " + web.Title);  
           } 
        }



                        int count=1;
                  DataTable dt = new DataTable();
                  int rowcount = 0;
          // DataTable dt1=new DataTable();
           dt.Columns.Add(new DataColumn("DeptName", typeof(string)));
           dt.Columns.Add(new DataColumn("DeptURL", typeof(string)));
           DataRow drDept = null;

           foreach (var vardept in _depts)
           { 
              drDept = dt.NewRow();
              drDept["DeptName"] = vardept.DeptName.ToString();
              drDept["DeptURL"] = vardept.DeptURL.ToString();
              dt.Rows.Add(drDept);
           }

        //   foreach (var f

     grdvushowsites.DataSource = dt.DefaultView;
     grdvushowsites.DataBind();
                }
                catch (Exception)
                {
                }
            }

            protected void grdvushowsites_RowDataBound(object sender, GridViewRowEventArgs e)
            {

    }

    my aim is to show the subsites in a site. here  am getting the deptname - name of the department- correctly.

    but somehow , am not getting the subsites' link.

    should i get them through onrowdatabound nly?

    or it should display automatically?

    please advice. though i wrote some sharepoint  code here, remaining is pure asp.net gridview only.

    so, would like to know should i add cod in onrowdatabound as well?

    Monday, October 31, 2016 12:51 PM

All replies

  • User1724605321 posted

    Hi SaMolPP,

    but somehow , am not getting the subsites' link.

    What do you mean by not get the link ? when click on the hyperlink , page not get redirect ? You bind the hyperlink with text "Text='<%# Bind("DeptName") %>'" , that means you will see the DeptName show as below , but when you hover over the link , the url will shown as shown below :

    Also debug your application to check whether you get the correct url from sharepoint . If i misunderstand your problem, please explain more about it .

    Best Regards,

    Nan Yu

    Tuesday, November 1, 2016 5:49 AM
  • User751840860 posted

    yes, when i click on the dept url field, i am not able to navigate to the target sub site.

    its acting similar to the deptname field only. 

    the same code is working in some other envrironment, but the env. where am testing now , is not working . am getting the depturl field also same as the deptname. if the name of the sub site is mumbai, the sub site url should be http://srv1:9876/sites/dms/mumbai and the deptname is mumbai.

    but the strange thing is that, when i fetched the url property , its giving me correct url. i have appended all the substes using stringbuilder and displayed for testing purposes.

    but when it comes to display, its not shown as hyperlink.

    Thanks for your time and reply!

    Tuesday, November 1, 2016 10:08 AM
  • User1724605321 posted

    Hi , 

    That is strange . I also test the hyperlink in gridview :

     <asp:TemplateField HeaderText="Dept URL" ItemStyle-Width="15%">
                <ItemTemplate>
                    <asp:HyperLink ID="hlinkdepturl" runat="server" NavigateUrl='<%# Eval("DeptURL") %>' Text='<%# Bind("DeptName") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>

    And it works. Please use F12 develop tools to check what is markup of hyperlink , does it like :

    <a id="grdvushowsites_hlinkdepturl_0" href="WebForm1.aspx">DeptName</a>


    Best Regards,

    Nan Yu

    Wednesday, November 2, 2016 8:42 AM