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

Access a cell and assign hyperlink

11 Answers 525 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 27 Aug 2012, 11:11 AM
Hi,
    I have several boundcolumn in my radgrid. I want to access one of the cell and assign hyperlink dynamically. How do I do this. Can anybody help. thanks in advance
Ryan

11 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 27 Aug 2012, 11:13 AM
Hi,

In the case of adding controls to the cells of GridBoundColumn,you cannot use ItemCreated only, but a combination of both ItemCreated and ItemDataBound.Here is the sample code.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
 if (e.Item is  GridDataItem)
 {
   GridDataItem item = (GridDataItem)e.Item;
   TableCell cell = (TableCell)item["UniqueName"];
   HyperLink hyperLink= new HyperLink ();
   hyperLink.Text = cell.Text;
   hyperLink.NavigateUrl = "page.aspx";
   hyperLink.ID = "hyperLink1";     
   cell.Controls.Add(hyperLink);
 }
}
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
  GridDataItem item = (GridDataItem)e.Item;
   TableCell cell = (TableCell)item["UniqueName"];
   HyperLink hyperLink= new HyperLink ();
   hyperLink.Text = cell.Text;
   hyperLink.NavigateUrl = "page.aspx";
   hyperLink.ID = "hyperLink1";     
   cell.Controls.Add(hyperLink);
 }
}

Thanks,
Shinu.
0
Ryan
Top achievements
Rank 1
answered on 28 Aug 2012, 03:45 AM
Hi Shinu,

How can i set a custom text for the AutoGeneratedEdit column in my RadGrid. Please provide a solution as soon as possible.

Thanks,
Ryan
0
Shinu
Top achievements
Rank 2
answered on 28 Aug 2012, 05:02 AM
Hi,

Please try the following code snippet.

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{      
   if (e.Item is GridDataItem)
   {
      GridDataItem item = (GridDataItem)e.Item;
      ((LinkButton)item["AutoGeneratedEditColumn"].Controls[0]).Text = "Your text";            
   }
}

Thanks,
Shinu.
0
mahdi
Top achievements
Rank 1
answered on 05 Jul 2019, 01:18 AM

hi shinu 

your code works greats for me since the column type is 'GridBoundColumn' but when its comes to 'GridTemplateColumn'  which i used  rad combobox in it do not work. how can add hyperlink to control which i explained above.

thanks in advance 

sarami

0
Attila Antal
Telerik team
answered on 08 Jul 2019, 10:35 AM
Hi Sarami,

When using GridTemplateColumn, the controls in it have a known ID defined by the developer. Since the IDs are known, the developer can search for the control by its ID rather than trying to accesses the first control of the Controls Collection.

Example code for finding a control:

var SomeControl = dataItem["DocumentUploadLink"].FindControl("SomeControlID");

Please take a look at the Accessing Cells and Rows article. There you can find more details on accessing components of the grid, get values of cells and also finding controls in them.

Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
mahdi
Top achievements
Rank 1
answered on 08 Jul 2019, 08:24 PM

Hi Attila 

And Thanks For Your Quick replay

would you mind to correcting me due to below code:

  <telerik:GridTemplateColumn HeaderText="QuotationNO"  HeaderStyle-Width="250px" UniqueName="QuotationNo">
                        <ItemTemplate>
                            <%#DataBinder.Eval(Container.DataItem, "QuotationName")%>
                        </ItemTemplate>
                          
                        <EditItemTemplate>
                            <telerik:RadComboBox RenderMode="Lightweight" runat="server" ID="RadComboBox2" EmptyMessage="......" UniqueName="QuotationNo"   DataTextField="QuotationName" Skin="Default" OnTextChanged ="RadComboBox2_TextChanged"
                                Height="140px" Width="140px"
                                DataValueField="QuotationID" DataSourceID="RepeatQoutationDatasource" SelectedValue='<%#Bind("QuotationID")%>'>
                            </telerik:RadComboBox>
                       
                        </EditItemTemplate>
 
<HeaderStyle Width="190px"></HeaderStyle>
 
<ItemStyle Width="190px"></ItemStyle>
                    </telerik:GridTemplateColumn>

 

and my VB code

Dim item As GridDataItem = CType(e.Item, GridDataItem)
                   Dim cell As TableCell = CType(item("QuotationNo"), TableCell)
                   Dim hyperLink1 As HyperLink = New HyperLink()
                    
 
 
                   '
                   Dim value = DataBinder.Eval(item.DataItem, "Autoid")
 
                   hyperLink1.Text = DataBinder.Eval(item.DataItem, "QuotationName")
                   hyperLink1.NavigateUrl = "PDFLoading.aspx?AutoID=B" & value & "&TYPE=Quotation"
                   hyperLink1.Target = "Blank"
                   hyperLink1.ID = "hyperLink1"
                   cell.Controls.Add(hyperLink1)

 

after triger Above Code i am getting Double text in Quotation cell which one of them is hyperlinked .

thanks in advance 

sarami

0
Attila Antal
Telerik team
answered on 11 Jul 2019, 04:25 PM
Hi Sarami,

I am afraid I will need some more information to see and understand what is happening in the grid. 

You can find attached a sample WebForms page with a Grid and template column. I would like you to modify this sample to produce the error share it with us. Once we can see the error we would be able to tell you how to fix it.

Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
mahdi
Top achievements
Rank 1
answered on 11 Jul 2019, 11:40 PM

Hi Attila

I added a "XML" file to project and customized grid to make it looks same as i want . 

i am getting Double text in Quotation cell which one of them is hyperlinked .

i am not able to attach zip file so please kindly get it from here...(Download)

thanks in advance

sarami

 

0
Attila Antal
Telerik team
answered on 16 Jul 2019, 02:39 PM
Hi Sarami,

Thank you for the sample. Now I can see the reason for having the text displayed twice. Besides the text evaluated in the ItemTemplate, a Hyperlink is being added into the cell additionally.

The following Evaluation displays the text:

<telerik:GridTemplateColumn HeaderText="QuotationNO" HeaderStyle-Width="250px" UniqueName="QuotationNo">
    <ItemTemplate>
        <%#DataBinder.Eval(Container.DataItem, "ShipName")%>
    </ItemTemplate>
</telerik:GridTemplateColumn>

Then, an additional link is added to the cell in the code behind:

Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
 
    If TypeOf e.Item Is GridDataItem Then
 
        Dim item As GridDataItem = CType(e.Item, GridDataItem)
 
        Dim cell As TableCell = CType(item("QuotationNo"), TableCell)
        Dim hyperLink1 As HyperLink = New HyperLink()
        '
        Dim value = DataBinder.Eval(item.DataItem, "OrderID")
 
        hyperLink1.Text = DataBinder.Eval(item.DataItem, "ShipName")
        hyperLink1.NavigateUrl = "PDFLoading.aspx?AutoID=B" & value & "&TYPE=Quotation"
        hyperLink1.Target = "Blank"
        hyperLink1.ID = "hyperLink1"
        cell.Controls.Add(hyperLink1)
    End If
 
End Sub

Since you are using TemplateColumn, you can place a Hyperlink directly in the ItemTemplate of the column. This way you will not need to add it on server-side.

<telerik:GridTemplateColumn HeaderText="QuotationNO" HeaderStyle-Width="250px" UniqueName="QuotationNo">
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server" Target="_blank" NavigateUrl='<%# "PDFLoading.aspx?AutoID=B" & Eval("OrderID") & "&TYPE=Quotation"  %>'><%# Eval("ShipName") %></asp:HyperLink>
    </ItemTemplate>
</telerik:GridTemplateColumn>

Result:



The links will point to the following address: http://localhost:60039/PDFLoading.aspx?AutoID=B8&TYPE=Quotation


Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
mahdi
Top achievements
Rank 1
answered on 21 Jul 2019, 03:01 AM

hi attila and thanks you bud.

what about if i want to do it with code behind server side code i mean , cuz in my case  i need to check some database matter and then add hyperlink to column ,thats mean the hyperlink is conditional and some of row dosnt have hyperlink even

thanks in advance

sarami

0
Eyup
Telerik team
answered on 24 Jul 2019, 12:41 PM
Hello Sarami,

You can achieve this requirement using the ItemDataBound event where you can access the generated control using the FindControl() approach:
https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-editinsert-mode

Then, you can set the Visible property of the Hyperlink control or modify it per your own preference.

In addition, I am also sending a sample RadGrid web site to demonstrated different ways of creating link columns.

Regards,
Eyup
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Ryan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ryan
Top achievements
Rank 1
mahdi
Top achievements
Rank 1
Attila Antal
Telerik team
mahdi
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or