Thursday, November 17, 2022

Grid OnRowCommand and OnSelectedIndexChanged difference ASP.NET C#

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="ID,MainID" OnRowDataBound="GridView1_RowDataBound" CssClass="table table-hover table-bordered table-responsive" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="20" FirstPageText="First" PreviousPageText="Previous" NextPageText="Next" LastPageText="Last" AllowSorting="True" OnRowDeleting="GridView1_RowDeleting" OnInit="GridView1_Init" OnRowCommand="GridView1_RowCommand" OnSelectedIndexChanged="OnSelectedIndexChanged">


<Columns>

 <asp:TemplateField HeaderText="Block/Unblock" Visible="true">

                <ItemTemplate>

                    <asp:LinkButton ID="ib_block" runat="server" CssClass="btn btn-danger" CommandName="select"

                        ToolTip="Block/Unblock"> Block</asp:LinkButton>

                </ItemTemplate>

            </asp:TemplateField>

</Columns>

</asp:Gridview>


protected void OnSelectedIndexChanged(object sender, EventArgs e)

        {

            GridViewRow row = GridView1.SelectedRow;

            Label erpid_value = (row.FindControl("Label1") as Label);

            LinkButton ib_block = (row.FindControl("ib_block") as LinkButton);

            //DropDownList drddlDiscipline= (row.FindControl("drddlDiscipline") as DropDownList);

            //RadioButtonList ddlPositions= (row.FindControl("ddlPositions") as RadioButtonList);

            //DropDownList ddlInstitute = (row.FindControl("ddlInstitute") as DropDownList);

            if (ib_block.Text == "Block")

            {

                // Response.Write(erpid_value.Text+", "+ drddlDiscipline.SelectedValue.ToString() + ", " + ddlInstitute.SelectedValue.ToString() + ", " );

                SqlParameter[] param = { new SqlParameter("erpid", erpid_value.Text)

                };

                new DataAccess().Write("sp_Blockscientistvacancy", param);

                //ib_block.Text = "UnBlock";

            }

            if (ib_block.Text == "UnBlock")

            {

                SqlParameter[] param = { new SqlParameter("@erpid", erpid_value.Text) };

                new DataAccess().Write("sp_unBlockscientistvacancy", param);

                // ib_block.Text = "Block";

            }

            ApplyFilter();

            //tb_news_id.Text = (row.FindControl("lbl_news_id") as Label).Text;

            //tb_news.Text = (row.FindControl("lbl_news") as Label).Text;

            //tb_newshindi.Text = (row.FindControl("lbl_hnews") as Label).Text;

            //tb_news_id.Enabled = true;

            //tb_news.Enabled = true;

            //tb_newshindi.Enabled = true;

            //btn_update.Enabled = true;

        }







------------------------------------



and on row command


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

        {

            if (e.CommandName == "Delete")

            {

                // get the categoryID of the clicked row

                int categoryID = Convert.ToInt32(e.CommandArgument);

                // Delete the record 

                DeleteRecord(categoryID);

                // Insert Operation Logs

                new UserLogs().InsertLog(Session["UserID"].ToString(), "User Deletion");

            }

            else if (e.CommandName == "DeleteAIPR")

            {

                Int64 FMSID = Convert.ToInt64(e.CommandArgument);

                SqlParameter[] param = { new SqlParameter("FMSID", FMSID) };

                new DataAccess().Write("DeleteAIPR", param);

                ApplyFilter();

            }

        }




<asp:TemplateField>

                <ItemTemplate>

                    <asp:HyperLink ID="HyperLinkAIPR" runat="server" NavigateUrl='<%# Eval("FMSID", "upload_aipr.aspx?FMSID={0}") %>' Text="Upload AIPR" Visible='<%# Eval("AiprTime").ToString()!= "" ? Convert.ToDateTime(Eval("AiprTime")).Year == DateTime.Now.Year ? false : true: true %>'></asp:HyperLink>

                    <asp:LinkButton ID="LinkDeleteAIPR" runat="server" CausesValidation="False" CommandArgument='<%# Eval("FMSID")%>' CommandName="DeleteAIPR" Text="Delete AIPR" Visible='<%# Eval("AiprTime").ToString()!= "" ? Convert.ToDateTime(Eval("AiprTime")).Year == DateTime.Now.Year ? true : false: false %>'></asp:LinkButton>

                </ItemTemplate>

            </asp:TemplateField>


--------------

 - e.commanargument (ID) in GridView1_RowCommand


Note:- when we use CommandName="select" than  OnRowCommand  and OnSelectedIndexChanged  event will be fire, where as on other commandname= "Delete" only OnRowCommand  will be fire and OnSelectedIndexChanged   will not be fire.

<asp:TemplateField HeaderText="Block/Unblock" Visible="true">

                <ItemTemplate>

                    <asp:LinkButton ID="ib_block" runat="server" CssClass="btn btn-danger" CommandName="select"

                        ToolTip="Block/Unblock"> Block</asp:LinkButton>

                </ItemTemplate>

            </asp:TemplateField>

            <asp:TemplateField>

No comments:

Post a Comment