Need to add a linkbutton control to the header of gridboundcolumn[autogenerated column] of radgrid

8 posts, 0 answers
  1. Pradeep Enugala
    Pradeep Enugala avatar
    55 posts
    Member since:
    Apr 2010

    Posted 04 Sep 2012 Link to this post

    Hi Telerik team,

    In one of my asp page,i'm displaying the radgrid whose datasource is autogenerated columns[autogeneratedcolumns property is set to true for radgrid].Now my new requirement is ,i need to add a control [Link button]to the header of the each autogeneratedcolumn.
    I have tried with ItemDataBound,ItemCreated,ColumnCreated events.
    finding the header item in these events and then adding the control to the cell..
     if (e.Item is GridHeaderItem)
            {
                TableCell cell = new TableCell();
                LinkButton lnk_edit = new LinkButton();
                lnk_edit.ID = "lnk_" + e.Column.OrderIndex;
                lnk_edit.Text = "Edit";
                lnk_edit.Width = Unit.Pixel(50);
      cell.Controls.Add(lnk_edit);
    }

    But unable to add the LinkButton control to the header cell of each column.

    Please reply ASAP.

    Thankyou,
    Pradeep
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 04 Sep 2012 Link to this post

    Hi,

    I suppose you want to add a linkbutton to all the boundcolumns. Here is the sample code.
    C#:
    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
     if (e.Item is GridHeaderItem)
     {
      GridHeaderItem item = e.Item as GridHeaderItem;
     foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
     {
      LinkButton link = new LinkButton();
       link.ID = "LinkButton1";
       link.Text = "Edit";
       if (col.ColumnType == "GridBoundColumn")
       {
          item[col.UniqueName].Controls.Add(link);
      }
    }
     }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Pradeep Enugala
    Pradeep Enugala avatar
    55 posts
    Member since:
    Apr 2010

    Posted 04 Sep 2012 Link to this post

    Hi Shinu,

    Thanks for the reply.Its working fine.
  5. Prince
    Prince avatar
    11 posts
    Member since:
    Aug 2012

    Posted 05 Sep 2012 Link to this post

    hi Shinu
    what if i want to add linkbutton to each and every row in the grid. Also i am adding columns and binding the datasource in code behind.
    let me explain you the scenario
    i have a DataGrid which is binded with the datasource. now i want to add another column of link type where in when i click on that link it pops up another page.
    i want to do all these things in code behind
    thanks
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 05 Sep 2012 Link to this post

    Hi Prince,

    You can add a template column dynamically as shown below.
    C#:
    string templateColumnName = "Name";
      GridTemplateColumn templateColumn = new GridTemplateColumn();
      templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
      templateColumn.HeaderText = templateColumnName;
    public partial class TemplateColumn : ITemplate
    {
     public void InstantiateIn(Control container)
     {
      LinkButton link = new LinkButton();
      link.Text = "Sample";
      link.ID = "LinkButton1";
      link.PostBackUrl = "Page.aspx";
      container.Controls.Add(link);
     }
    }

    Also check this help documentation which explains more about creating grid from code behind.

    Thanks,
    Shinu.
  7. Prince
    Prince avatar
    11 posts
    Member since:
    Aug 2012

    Posted 05 Sep 2012 Link to this post

    hi Shinu
    i am very new telerik grid. i don't know much about this
    can you please elaborate it little more
    i am pasting my code in which i have added columns to it.
    now i want to add one more column which has link button in each cell and when you click on that link it pops up another aspx page.
    (very similar to AutoGenerateEditColumn) the only problem with autogeneratededitcolumn is it gives inline editing
    instead of that i want to pop up another page

    thanks
    prince
  8. Prince
    Prince avatar
    11 posts
    Member since:
    Aug 2012

    Posted 05 Sep 2012 Link to this post

    hey shinu
    i am able to bring up link buttons in each row. now how do i add querystring to postback url
    for example
    when i am clicking link button of first row it should navigate to another page with querystring as first row id and first row name which is there in the radgrid table.

    thanks
    prince
  9. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 06 Sep 2012 Link to this post

    Hi,

    Try the following code to achieve your scenario.
    C#:
    public static string value;
    public partial class TemplateColumn : ITemplate
    {
      public void InstantiateIn(Control container)
      {
         Button btn = new Button();
         btn.Text = "Sample";
         btn.ID = "Button1";
         btn.OnClientClick = "OnClientClick(" + value + ");return false";
         container.Controls.Add(btn);
       }
    }
    JS:
    function OnClientClick(value)
    {
      var oWnd = radopen("Window.aspx?CustomerID=" + value, "RadWindow1");
    }

    Thanks,
    Shinu.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017