HyperLinkColumn : URL lost with PageMethods

5 posts, 1 answers
  1. luc bonenfant
    luc bonenfant avatar
    53 posts
    Member since:
    Sep 2007

    Posted 28 Sep 2015 Link to this post

    Hello,

    Below is some code to demonstrate my problem​ :

    I use a grid with 3 pages, each containing ​only one ​row for simplicity.. The ​grid contains ​an hyperlink column  (another is visible here only for verification).

    The first page is displayed by default and then we can observe ​a correct URL to the passage of the mouse over it.
    Changing page call​s PageMethods to get data client side, and ​oddly the url is lost... However, verification column displays the correct URL.

     Thank you in advance for your help.

     

    TestHyperLinkColumn.aspx :

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestHyperLinkColumn.aspx.cs" Inherits="Test.TestHyperLinkColumn" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
     
        <telerik:RadCodeBlock runat="server" ID="RadScriptBlock1">
     
            <script type="text/javascript">
                //<![CDATA[
     
                function RadGrid1_Command(sender, args) {
                    args.set_cancel(true);
                    $find("<%= RadAjaxLoadingPanel1.ClientID %>").show("<%= RadGrid1.ClientID %>");
     
     
                    var currentPageIndex = sender.get_masterTableView().get_currentPageIndex();
                    PageMethods.GetData(currentPageIndex + 1, updateGrid);
                }
     
                function updateGrid(result) {
                    //Bind Grid
                    var tableView = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
                    var $ = $telerik.$;
                    result = $.parseJSON(result);
                    tableView.set_dataSource(result);
                    tableView.dataBind();
                    $find("<%= RadAjaxLoadingPanel1.ClientID %>").hide("<%= RadGrid1.ClientID %>");
                }
     
                //]]>
     
            </script>
     
        </telerik:RadCodeBlock>
     
        <telerik:RadScriptManager runat="server" EnableCdn="true" ID="RadScriptManager1" EnablePageMethods="true" />
     
         
        <telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1">
        </telerik:RadAjaxLoadingPanel>
     
     
        <div>
           <telerik:RadGrid runat="server" ID="RadGrid1" AllowFilteringByColumn="true" FilterType="CheckList" AllowPaging="true" PagerStyle-AlwaysVisible="true" AllowSorting="true">
                <MasterTableView AutoGenerateColumns="False" PageSize="1" AllowFilteringByColumn="false">
                    <Columns>
                        <telerik:GridHyperLinkColumn DataTextField="Link" HeaderText="Link" Target="_blank" DataType="System.String" DataNavigateUrlFields = "LinkURL">
                        </telerik:GridHyperLinkColumn>
                        <telerik:GridBoundColumn DataField="LinkURL" HeaderText="LinkURL" DataType="System.String" Visible="true">
                        </telerik:GridBoundColumn>
                    </Columns>
                </MasterTableView>
                <PagerStyle AlwaysVisible="true" Mode="NumericPages" />
                <ClientSettings EnableRowHoverStyle="true">
                    <ClientEvents OnCommand="RadGrid1_Command" />
                </ClientSettings>
            </telerik:RadGrid>
        </div>
         
        </div>
        </form>
    </body>
    </html>

     

    TestHyperLinkColumn.aspx.cs :

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.Services;
    using System.Web.UI;
     
    namespace Test
    {
        public partial class TestHyperLinkColumn : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    RadScriptManager1.EnablePageMethods = true;
     
                    DataTable data = GetDataTable(1, true);
                    RadGrid1.MasterTableView.DataSource = data;
                    RadGrid1.DataBind();
     
                }
            }
     
            private static string ConvertDataTabletoString(System.Data.DataTable dt)
            {
                System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                Dictionary<string, object> row;
                foreach (System.Data.DataRow dr in dt.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (System.Data.DataColumn col in dt.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
                return serializer.Serialize(rows);
            }
     
            private static DataTable GetDataTable(int pageIndex, bool first = false)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Link", typeof(string));
                dt.Columns.Add("LinkURL", typeof(string));
                DataRow dr = dt.NewRow();
                dr[0] = string.Format("Link{0}", pageIndex);
                dr[1] = string.Format("http://www.link{0}.com", pageIndex);
                dt.Rows.Add(dr);
                pageIndex++;
                if (first)
                {   //Adds 2 rows only for pager because data not used (loaded from PageMethods)
                    dr = dt.NewRow();
                    dr[0] = "";
                    dr[1] = "";
                    dt.Rows.Add(dr);
                    pageIndex++;
                    dr = dt.NewRow();
                    dr[0] = "";
                    dr[1] = "";
                    dt.Rows.Add(dr);
                }
                return dt;
            }
     
            [WebMethod]
            public static string GetData(int PageIndex)
            {
                DataTable dt = GetDataTable(PageIndex);
     
                string result = ConvertDataTabletoString(dt);
                return result;
            }
        }
    }
     

  2. luc bonenfant
    luc bonenfant avatar
    53 posts
    Member since:
    Sep 2007

    Posted 01 Oct 2015 in reply to luc bonenfant Link to this post

    Any idea please ?
  3. Answer
    Angel Petrov
    Admin
    Angel Petrov avatar
    1082 posts

    Posted 01 Oct 2015 Link to this post

    Hello,

    The described behavior is expected, it is caused by the fact that the initial hyperlink url is set on the server(the entire link is provided) but the subsequent on the client(which is build by  the DataNavigateUrlFormatString that the columns uses). In order for the hyperlink column to work correctly one should set a DataNavigateUrlFormatString. The DataNavigateUrlFields values will later be used to fill the placeholders inside the provided format string. If we refer to the code from the thread you can do something like this.

    C#:
    private static DataTable GetDataTable(int pageIndex, bool first = false)
      {
          DataTable dt = new DataTable();
          dt.Columns.Add("Link", typeof(string));
          dt.Columns.Add("LinkURL", typeof(string));
          DataRow dr = dt.NewRow();
          dr[0] = string.Format("Link{0}", pageIndex);
          dr[1] =pageIndex.ToString();
          dt.Rows.Add(dr);
          pageIndex++;
          if (first)
          {   //Adds 2 rows only for pager because data not used (loaded from PageMethods)
              dr = dt.NewRow();
              dr[0] = "";
              dr[1] = "";
              dt.Rows.Add(dr);
              pageIndex++;
              dr = dt.NewRow();
              dr[0] = "";
              dr[1] = "";
              dt.Rows.Add(dr);
          }
          return dt;
      }


    ASPX:
    <telerik:GridHyperLinkColumn DataTextField="Link" HeaderText="Link" Target="_blank"
                          DataNavigateUrlFormatString="http://www.link{0}.com" DataType="System.String" DataNavigateUrlFields = "LinkURL">
                      </telerik:GridHyperLinkColumn>


    Regards,
    Angel Petrov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  4. luc bonenfant
    luc bonenfant avatar
    53 posts
    Member since:
    Sep 2007

    Posted 01 Oct 2015 in reply to Angel Petrov Link to this post

    Thank you for help, ​it's better by using Format but i still ​have a small problem ​in this case ​: it's working if i do DataNavigateUrlFormatString="http://{0}"​, ​but if users generate ​some URL from httpS://...

     

  5. luc bonenfant
    luc bonenfant avatar
    53 posts
    Member since:
    Sep 2007

    Posted 01 Oct 2015 in reply to luc bonenfant Link to this post

    Sorry for my previous post, in fact http is transform to https automatically by navigator when needed.
    So, it's ok. Thanks a lot.

     

Back to Top