Ramco Ramco

Ramco Ramco

  • 442
  • 3.4k
  • 528.4k

RowCommand not working

May 8 2023 4:11 AM

Hi

  RowCommand not working

<asp:GridView ID="grdPlanning" class="table table-bordered" runat="server" OnRowDataBound="gr_RowDataBound" 
    onsorting="grdPlanning_Sorting" AllowSorting="True" OnRowCommand="grdPlanning_RowCommand" AutoGenerateColumns="true" DataKeyNames="BookID" >
    <Columns>
            <asp:TemplateField HeaderText="Session Date">
                <ItemTemplate>
                    <asp:TextBox ID="txtSessionDate" runat="server" CssClass="form-control daterange-single" Text=''></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>
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");
    }
}

---------------------------------------------------
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;
        e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;

        TextBox txtSessionDate = (TextBox)e.Row.FindControl("txtSessionDate");
        string Remark = DataBinder.Eval(e.Row.DataItem, "Remarks").ToString(); // Replace "ColumnName" with the actual name of your column
        if (Remark == "Done" || Remark == "Scheduled")
        {
            txtSessionDate.Enabled = false;
        }

        if (Remark == "Deleted")
        {
            e.Row.Visible = false;
        }

        for (int i = init+1; i< e.Row.Cells.Count-1; i++)
        {
            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 = row[i].ToString();
            button.CommandName = "Select";
            button.CommandArgument = commandArgument;
            button.OnClientClick = "GetModuleData(this)";

            if (row[i].ToString().ToUpper() == "A")
            {
                button.CssClass = "badge badge-danger";
            }
            else if (row[i].ToString().ToUpper() == "B")
            {
                button.CssClass = "badge badge-warning";
            }
            else if (row[i].ToString().ToUpper() == "ND")
            {
                button.CssClass = "badge badge-secondary";
                button.OnClientClick = "return false;";
            }
            
            else if (row[i].ToString().ToUpper() == "HB")
            {
                button.CssClass = "badge badge-primary";
            }

            e.Row.Cells[i].Controls.Add(button);
            e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
        }
    }
    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


Answers (2)