This is a migrated thread and some comments may be shown as answers.

Server side onclick event for columns in MasterTableView

3 Answers 350 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anuj
Top achievements
Rank 1
Anuj asked on 13 Mar 2012, 09:47 PM
I have below telrik grid. 

As I am populating my grid dynamically, I have made AutoGenerateColumn = true. In the column section under MasterTableView I have created two hyperlink (i.e. GridHyperLinkColumn). I want to execute a server side event when user clicks on that column (i.e. Just like onclick event for both hyperlink)



<telerik:RadGrid ID="gvInvoice" Width="98%" AllowPaging="true" AllowSorting="true" PageSize="100"
                                    runat="server" AutoGenerateColumns="true" CellSpacing="0" GridLines="None" Skin="Office2010Blue"
                                    OnPageIndexChanged="gvInvoice_PageIndexChanged"
                                    ShowFooter="True" Height="100%">
                                    <MasterTableView>
                                        <Columns>
                                          <telerik:GridHyperLinkColumn Text="Add Comments" UniqueName="Comments"  HeaderStyle-Width="120px" HeaderText="Comments" NavigateUrl="#"></telerik:GridHyperLinkColumn>
                                          <telerik:GridHyperLinkColumn Text="Add Documents" UniqueName="Documents" HeaderStyle-Width="120px" HeaderText="Document" NavigateUrl="#"></telerik:GridHyperLinkColumn>
                                        </Columns>
                                    </MasterTableView>
                                   
                                    <PagerStyle Mode="NextPrevAndNumeric" />
                                    <ClientSettings>
                                    <Scrolling AllowScroll="true" SaveScrollPosition="true" UseStaticHeaders="true" /></ClientSettings>
                                </telerik:RadGrid> 

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 14 Mar 2012, 05:54 AM
Hello,

There is no direct server side onclick event for GridHyperLinkColumn. One option is you can access the GridHyperLinkColumn as HyperLink from code behind and Can attach NavigateUrl property like below.
C#:
protected void gvInvoice_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
    GridDataItem dataItem = (GridDataItem)e.Item;
    HyperLink hlnk = (HyperLink)dataItem["EmployeeID"].Controls[0];
    hlnk.NavigateUrl = //your url
  }
}
If you need OnClick event for HyperLinkColumn, you can try the approach in the following forum thread.
radgrid hyperlink column
Another option is  you can use GridButtonColumn with ButtonType as link Button instead of GridHyperLinkColumn.

-Shinu.
0
Anuj
Top achievements
Rank 1
answered on 14 Mar 2012, 07:21 AM
But I have two link. How would i identify that user has clicked which link ?

Both link has different functionality to perform.
0
Shinu
Top achievements
Rank 2
answered on 14 Mar 2012, 01:40 PM
Hello,

If you are following the HyperLink NavigateUrl method,you can identify the clicked link using its UniqueName.Please take a look into the following code.
aspx:
<telerik:GridHyperLinkColumn Text="Add Comments" UniqueName="Comments"  HeaderStyle-Width="120px" HeaderText="Comments" NavigateUrl="#"></telerik:GridHyperLinkColumn>
<telerik:GridHyperLinkColumn Text="Add Documents" UniqueName="Documents" HeaderStyle-Width="120px" HeaderText="Document" NavigateUrl="#"></telerik:GridHyperLinkColumn>

C#:
protected void gvInvoice_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
    GridDataItem dataItem = (GridDataItem)e.Item;
    HyperLink hyperLink1 = (HyperLink)dataItem["Comments"].Controls[0];
    hyperLink1.NavigateUrl = //your url
    HyperLink hyperLink2 = (HyperLink)dataItem["Documents"].Controls[0];
    hyperLink2.NavigateUrl = //your url
  }
}

For the second method provided as a forum thread, you can pass an argument in the ajaxRequest  to identify the links.Please try the following code.
C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            ((e.Item as GridDataItem)["Comments"].Controls[0] as HyperLink).Attributes["onclick"] = "raisePostBackComments();";
            ((e.Item as GridDataItem)["Documents"].Controls[0] as HyperLink).Attributes["onclick"] = "raisePostBackDocuments();";
        }
    }

Javascript:
function raisePostBackComments()
    {
        var ajaxManager = $find("<%= RadAjaxManager1.ClientID %>");
        ajaxManager.ajaxRequest("Comments");
    }
function raisePostBackDocuments()
 
    {
        var ajaxManager = $find("<%= RadAjaxManager1.ClientID %>");
        ajaxManager.ajaxRequest("Documents");
    }

C#:
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
    if(e.Argument == "Comments")
    {
      // for first hyperlink
    }
    if(e.Argument == "Documents")
    {
      // for second hyperlink
    }
     
}

-Shinu.
Tags
Grid
Asked by
Anuj
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Anuj
Top achievements
Rank 1
Share this question
or