Hi
I have below code but rowcommand not getting executed
C#
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
for (int i = 8; i < e.Row.Cells.Count - 1; i++)
{
string cellValue = e.Row.Cells[i].Text;
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();
button.Text = cellValue;
button.CommandName = "Select";
button.CommandArgument = commandArgument;
button.OnClientClick = "GetModuleData(this)";
if (cellValue.ToUpper().Trim() == "A")
{
button.CssClass = "badge badge-danger";
}
}
}
}
***************************************
protected void grdPlanning_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Select")
{
BALBooks bALBooks = new BALBooks();
BALStudents bALStudents = new BALStudents();
StudentDetail Result = bALStudents.GetStudentDetailByLoginID(bALStudents.GetStudentIDByMobile(hdfMobile.Value));
ltrlHead.Text = Result.Name;
ltrlHead.Text = ltrlHead.Text + " : " + bALBooks.GetBookTitle(Convert.ToInt32(hdfBookId.Value));
BindData(Convert.ToInt32(hdfBookId.Value), Result.LoginCode);
}
}
catch (Exception ex)
{
Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(hdfLoginCode.Value));
ShowMessage("Oops...", ex.Message, "error");
}
}Code
Thanks

Jignesh KumarRank #91 · 22.9K reputationPosted May 16, 2023, 1:44 AM EDT
you need to add commandName = "select"
Remove command name from Gridview and add to your button from where you want to trigger,
" Text="" />Naimish MakwanaRank #324 · 7.7K reputationPosted May 16, 2023, 1:46 AM EDT
Based on the provided code, it appears that you are dynamically creating LinkButton controls in the
gr_RowDataBoundevent of a GridView. However, you haven't added these buttons to any container control or the GridView row itself.To make sure the
RowCommandevent is triggered when clicking on the LinkButton, you need to add the buttons to the row's cell or a container control, such as a Panel or TableCell. Here's an updated version of your code that adds the buttons to a TableCell:protected void gr_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType != DataControlRowType.DataRow) return; e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center; for (int i = 8; i < e.Row.Cells.Count - 1; i++) { string cellValue = e.Row.Cells[i].Text; 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(); button.Text = cellValue; button.CommandName = "Select"; button.CommandArgument = commandArgument; button.OnClientClick = "GetModuleData(this)"; if (cellValue.ToUpper().Trim() == "A") { button.CssClass = "badge badge-danger"; } TableCell cell = new TableCell(); cell.Controls.Add(button); e.Row.Cells.Add(cell); } } catch (Exception ex) { Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(hdfLoginCode.Value)); ShowMessage("Oops...", ex.Message, "error"); } }By adding the
buttoncontrol to thecellcontrol and then adding thecellto the row'sCellscollection, the buttons will be rendered correctly within the GridView. This should ensure that thegrdPlanning_RowCommandevent is triggered when clicking on the LinkButton.Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted May 16, 2023, 2:06 AM EDT
Hi Naimish
I have written this - e.Row.Cells[i].Controls.Add(button);
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted May 16, 2023, 2:08 AM EDT
Hi Jignesh
I have written this but still not working
Thanks