Dynamically add OnClick event to LinkButton inside ASP.Net GridView using RowDataBound event

ramco1917
 
on Jul 13, 2022 10:57 PM
446 Views

hi

With below code can we create onClick event.

var row = (DataRowView)e.Row.DataItem;
var currentPerson = row[0].ToString();
var currentDate = row.DataView.Table.Columns[i];
var commandArgument = currentPerson + ":" + currentDate;
LinkButton button = new LinkButton { 
    Text = row[i].ToString(), 
    CommandName = "Select", 
    CommandArgument = commandArgument, 
    CssClass = "badge badge-warning badge-pill", 
    OnClick = "Linkbtnclass_Click", 
    OnClientClick = "GetModuleData(this)" 
};
e.Row.Cells[i].Controls.Add(button);

Thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 14, 2022 03:59 AM

The way you are doing is not possible.

You need to add TemplateField and inside the TemplateField toy have to add the control.

Refer below article.

Dynamically add BoundField and TemplateField Columns to GridView in ASP.Net