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

Adding Push Button in each row dynamically

11 Answers 199 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Prince
Top achievements
Rank 1
Prince asked on 05 Sep 2012, 06:39 AM
i want to add PUSH button in each row dynamically
datasource is coming from other aspx page
Binding of datsource and column creation is done in code behind.
so adding button should also be done dynamically
Please help me regarding the same
thanks
prince

11 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Sep 2012, 07:04 AM
Hi,

I suppose you want to add a Button in every row dynamically. This can be accomplished by adding a TemplateColumn in which Button can be added in the ItemTemplate.
C#:
string ColumnName = "Name";
GridTemplateColumn templateColumn = new GridTemplateColumn();
templateColumn.ItemTemplate = new MyTemplate(ColumnName);
public partial class TemplateColumn : ITemplate
{
 public void InstantiateIn(Control container)
 {
  Button btn = new Button();
  btn.Text = "Text";
  btn.ID = "Button1";
  container.Controls.Add(btn);
 }

Thanks,
Princy.
0
Prince
Top achievements
Rank 1
answered on 05 Sep 2012, 07:21 AM
hey princy,
i am very new to telerik. Can you elaborate little more.
how are you going to add it to the grid. i am pasting my code

DataTable

 

 

dt = new DataTable();

dt = i_datatable;

 

//RadGrid1.MasterTableView.Columns.Clear();

 

foreach (DataColumn dc in dt.Columns)

{

 

 

//GridEditCommandColumn editColumn = new GridEditCommandColumn();

 

 

BoundColsEntity objcolentity = new BoundColsEntity();

objcolentity = LstBoundColsEntity[LstColCount[dc.ColumnName.ToString()]];

 

GridBoundColumn boundcolumn1 = new GridBoundColumn();

boundcolumn1.DataField = dc.ColumnName;

boundcolumn1.UniqueName = dc.ColumnName;

boundcolumn1.HeaderText = objcolentity.ColumnDisplayName;

boundcolumn1.AllowFiltering = objcolentity.Allowfiltering;

boundcolumn1.Visible = objcolentity.Visiblity;

RadGrid1.MasterTableView.Columns.Add(boundcolumn1);

}

now after this i want to add one more column and all the cells in that column will have button (lets say GO).
and when i click in go button it should pop a new page.
0
Prince
Top achievements
Rank 1
answered on 05 Sep 2012, 07:43 AM
hey Princy
i am able to bring up buttons in each row. but now the problem is how do i assign event to that button
for example when i click on the button of a particular row. it should pop up a new window with a query string

thanks
prince
0
Princy
Top achievements
Rank 2
answered on 05 Sep 2012, 09:07 AM
Hi,

You can attach client event for the button to open a Radwindow and pass the query string as shown below.
C#:
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
    GridDataItem item = (GridDataItem)e.Item;
    Button btn = new Button();
    btn.Text = "Text";
    btn.ID = "Button1";
    string value = item["UniqueName"].Text;
    btn.OnClientClick = "OnClientClick("+value+");return false";
    item["UniqueName"].Controls.Add(btn);
   }
}
JS:
function OnClientClick(value)
 {
   var oWnd = radopen("Page.aspx?ID=" + value, "RadWindow1");
 }

Thanks,
Princy.
0
Prince
Top achievements
Rank 1
answered on 05 Sep 2012, 09:43 AM
hey princy
let me explain you my scenario
i have added button with gridtemplatecolumn which you have mentioned in the previous posts. now i am getting buttons in each row.
what i want now is when i click on that button
it should pop up a window with query string as the value of first and second cell of that same row.
i have attached the screenshot of my output
so when i click "first row" edit button it should give me "Testfortrig" as my querystring

i hope its clear to you. please reply as soon as possible
thanks
prince
0
Princy
Top achievements
Rank 2
answered on 06 Sep 2012, 05:57 AM
Hi Prince,

You can attach the client event to the dynamically added button and then pass the query string as shown below.
C#:
public static string value;
public partial class TemplateColumn : ITemplate
{
  public void InstantiateIn(Control container)
  {
     Button btn = new Button();
     btn.Text = "Edit";
     btn.ID = "Button1";
     btn.OnClientClick = "OnClientClick(" + value + ");return false";
     container.Controls.Add(btn);
   }
}
function OnClientClick(value)
{
  var window = radopen("Page.aspx?ID=" + value, "RadWindow1");

Thanks,
Princy.
0
Prince
Top achievements
Rank 1
answered on 06 Sep 2012, 06:24 AM
problem is that value has to come dynamically from the row.i have attached the screenshot of my output
let me explain you by that
lets say i clicked on "first row edit button" then in the querystring i want first row client_id
and when i click on "second row edit button" then i want second row client_id in my querystring

i hope now its clear to you
please reply me as soon as possible

thanks for your valuable time
prince
0
Princy
Top achievements
Rank 2
answered on 07 Sep 2012, 07:06 AM
Hi,

You can set the DataKeyNames and pass the value as shown below.
C#:
public static string value;
RadGrid1.MasterTableView.DataKeyNames = new string[] { "EmployeeID" };
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
    GridDataItem item = (GridDataItem)e.Item;
    value = item.GetDataKeyValue("EmployeeID").ToString();
  }
}
public partial class TemplateColumn : ITemplate
{
  public void InstantiateIn(Control container)
  {
     Button btn = new Button();
     btn.Text = "Edit";
     btn.ID = "Button1";
     btn.OnClientClick = "OnClientClick(" + value + ");return false";
     container.Controls.Add(btn);
   }
}

Thanks,
Princy.
0
Prince
Top achievements
Rank 1
answered on 07 Sep 2012, 07:46 AM
hey Princy
i am getting NullReferenceException while try that. i have attached the screenshot. please have a look at it and help me out with the same

thank you
prince
0
Princy
Top achievements
Rank 2
answered on 10 Sep 2012, 06:54 AM
Hi Prince,

Please try the following code snippet.
C#:
protected void Page_Init(object sender, EventArgs e)
{
    GridTemplateColumn templateColumn = new GridTemplateColumn();
    grid.MasterTableView.Columns.Add(templateColumn);
    templateColumn.ItemTemplate = new MyTemplate();
}
 
public partial class MyTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        Button btn = new Button();
        btn.Text = "Text";
        btn.ID = "Button1";
        container.Controls.Add(btn);
    }
}
 
void grid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem ditem = (GridDataItem)e.Item;
        Button btn = (Button)ditem.FindControl("Button1");
        string value = ditem["client_id"].Text;
        btn.OnClientClick = "OnClientClick(" + value + ");return false";
    }
}
JS:
<script type="text/javascript">
    function OnClientClick(value)
    {
        var window = radopen("Page.aspx?ID=" + value, "RadWindow1");
    
</script>

Thanks,
Princy.
0
Prince
Top achievements
Rank 1
answered on 10 Sep 2012, 10:15 AM
hey Princy,
thanks allot. its working now
but the only problem is its not opening as a pop. a separate window is coming up
Tags
Grid
Asked by
Prince
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Prince
Top achievements
Rank 1
Share this question
or