Hi
I have below code but rowcommand not getting executed
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");
}
}
Thanks
Ramco RamcoPosted May 16, 2023, 6:08 AM
Hi Jignesh
I have written this but still not working
Thanks
Ramco RamcoPosted May 16, 2023, 6:06 AM
Hi Naimish
I have written this - e.Row.Cells[i].Controls.Add(button);
Thanks
Naimish MakwanaPosted May 16, 2023, 5:46 AM
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: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
Jignesh KumarPosted May 16, 2023, 5:44 AM
you need to add commandName = "select"
Remove command name from Gridview and add to your button from where you want to trigger,