0

I am working on asp.net gridview on webforms and using Gridview RowCommand method of GridView OnRowCommand event for the Buttons View, Edit & Update inside TemplateField.

if (e.CommandName == "EditContract") 
  {
            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;

            int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
            gvContract.SelectRow(rowIndex);

            using (SqlCommand cmd = new SqlCommand("spContractEdit", myObj.DbConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@RoleName", SqlDbType.Char).Value = Session["RoleName"];
                cmd.Parameters.Add("@UnitName", SqlDbType.Char).Value = Session["UnitName"];
                cmd.Parameters.Add("@ContrSerialNo", SqlDbType.Int).Value = SerialNo;

                dAdapter = new SqlDataAdapter(cmd);

                DataTable DtContract = new DataTable();
                dAdapter.Fill(DtContract);
     }

if (e.CommandName == "UpdateContract") 
           {

            lblMessage.Text = "";
            lblFile.Text = "";

            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;

            using (SqlCommand cmd = new SqlCommand("spContractUpdate", myObj.DbConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;

             }
          } 

This code is working fine when the page loads first time but has 2 problems after that. i.e. 1. It is editing and updating data when the page gets load for the first time but when I try to edit the same row or any other then it doesn't. I know it is because I have defined the !Page.IsPostback method on the Page_Load event but I do not know how to tackle this situation. 2. Problem is How to restrict the gridview row to update only that data whose row is selected?

Please suggest me a solution.

Gridview Sample

Nagib Mahfuz
  • 833
  • 10
  • 19
Ghaffar
  • 11
  • 3

1 Answers1

0

First of all, you should bind the Gridview in UpdateContract.Like below

if (e.CommandName == "UpdateContract") {

            lblMessage.Text = "";
            lblFile.Text = "";

            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;

            using (SqlCommand cmd = new SqlCommand("spContractUpdate", myObj.DbConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;

***You need to bind Gridview here then it will not have to go for postback every time ***
                Gridview.datasource= datatable;
                Gridview.Databind();
}

You can use updatepanel for update only the needed element and avoid refresh.

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
   <asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>


    ***Here put your gridview***

   <asp:gridview></asp:gridview>
   </ContentTemplate>
   </asp:UpdatePanel>
Priom Sarkar
  • 342
  • 3
  • 15
  • Dear @Priom Sarkar, Thanks for the help. I am already binding gridview on every command i.e. view, edit and update with the datatable. Further i am using master page and I have put your code on my application's master page but now the update is not working .. – Ghaffar Sep 11 '19 at 13:01
  • Are you using gridview on your Master Page? – Priom Sarkar Sep 11 '19 at 13:18
  • Dear @Priom thanks again for your reply. No I am using gridview on a content page. I dropped scriptmanager on the master page and updatepanel+contenttemplate on the another page inside the contenttemplate. – Ghaffar Sep 11 '19 at 20:51
  • put scriptmanager on the content page. Where you are using updatepanel+contenttemplate – Priom Sarkar Sep 13 '19 at 11:09