Access a cell and assign hyperlink

12 posts, 0 answers
  1. Ryan
    Ryan avatar
    56 posts
    Member since:
    Apr 2012

    Posted 27 Aug 2012 Link to this post

    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
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 27 Aug 2012 Link to this post

    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.
  3. Ryan
    Ryan avatar
    56 posts
    Member since:
    Apr 2012

    Posted 27 Aug 2012 Link to this post

    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
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Aug 2012 Link to this post

    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.
  5. mahdi
    mahdi avatar
    3 posts
    Member since:
    Jul 2019

    Posted 04 Jul in reply to Shinu Link to this post

    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

  6. Attila Antal
    Admin
    Attila Antal avatar
    318 posts

    Posted 08 Jul Link to this post

    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.
  7. mahdi
    mahdi avatar
    3 posts
    Member since:
    Jul 2019

    Posted 08 Jul in reply to Attila Antal Link to this post

    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

  8. Attila Antal
    Admin
    Attila Antal avatar
    318 posts

    Posted 11 Jul Link to this post

    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.
  9. mahdi
    mahdi avatar
    3 posts
    Member since:
    Jul 2019

    Posted 11 Jul in reply to Attila Antal Link to this post

    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

     

  10. Attila Antal
    Admin
    Attila Antal avatar
    318 posts

    Posted 16 Jul Link to this post

    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.
  11. mahdi
    mahdi avatar
    1 posts
    Member since:
    Feb 2019

    Posted 20 Jul in reply to Attila Antal Link to this post

    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

  12. Eyup
    Admin
    Eyup avatar
    3784 posts

    Posted 24 Jul Link to this post

    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.
Back to Top