1

In my case i have asp:gridview with OnRowCommand calls server method but when i try to refresh my page manually then it also calls OnRowCommand method, i don't know where is the actual problem.

My Code:.aspx

<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="CUG_OnSelectedIndexChanged" OnRowCommand="CugSubmit_onserverclick1"  EnableViewState="true"BorderStyle="Solid" BorderColor="Gray" BorderWidth="1px" EditRowStyle-BorderStyle="Solid" EditRowStyle-BorderWidth="1px" EditRowStyle-BorderColor="Gray"  HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderWidth="1px" HeaderStyle-BorderColor="Gray" RowStyle-BorderStyle="Solid" RowStyle-BorderColor="Gray"  RowStyle-BorderWidth="1px" EmptyDataRowStyle-BorderStyle="Solid" EmptyDataRowStyle-BorderColor="Black" EmptyDataRowStyle-BorderWidth="1px"  SortedAscendingHeaderStyle-BorderStyle="Solid" SortedAscendingHeaderStyle-BorderColor="Black" SortedAscendingHeaderStyle-BorderWidth="1px">
    <Columns>
    <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
    <asp:TemplateField HeaderText="Driver ID">
        <ItemTemplate>
        <asp:Label ID="lbl_Dri_Name" runat="server" Text='<%# Eval("VehicleNo") %>' Visible="false"></asp:Label>
            <asp:TextBox ID="Dri_Name" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Dedicated">
                <ItemTemplate>
        <asp:Label ID="lbl_Vehitype" runat="server" Text='<%# Eval("VehicleType") %>' Visible="false"></asp:Label>
                    <asp:CheckBox ID="Vehitype" runat="server" AutoPostBack="true" />
                </ItemTemplate>
        </asp:TemplateField>

    <asp:ButtonField CommandName="ProcessThis" Text="Submit" />
    <asp:ButtonField CommandName="Select" ItemStyle-Width="30" Text="delete" HeaderText="Delete" />
    <asp:TemplateField HeaderText="">
        <ItemTemplate>

       </ItemTemplate>
        <FooterStyle HorizontalAlign="left" />
        <FooterTemplate>
         <asp:Button ID="ButtonAdd" runat="server" Text="Add Row" onclick="ButtonAdd_Click" />
        </FooterTemplate>
    </asp:TemplateField>
    </Columns></asp:gridview>

My code:.cs

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetCUGList();
        }
    }
protected void CugSubmit_onserverclick1(object sender, GridViewCommandEventArgs e)
    {
        DL.CustomerProfile ObjInput = new CustomerProfile();
        BL.CustomerInputBL ObjCustomerInputBL = new CustomerInputBL();

        if (e.CommandName == "ProcessThis")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = Gridview1.Rows[index];

            if (row.RowType == DataControlRowType.DataRow)
            {
                TextBox textBox = row.FindControl("Dri_Name") as TextBox;
                CheckBox chk = row.FindControl("Vehitype") as CheckBox;

                ObjInput.CustId = HttpContext.Current.Session["VidStr"].ToString();
                ObjInput.CustName = HttpContext.Current.Session["FirstName"].ToString();

                ObjInput.VehiNo = textBox.Text;
                ObjInput.Update = "Insert";
                if (chk.Checked == true)
                {
                    ObjInput.VehiChk = 1;
                }

                else
                {
                    ObjInput.VehiChk = 0;
                }

                string URL = "http://*****/CustomerServices.svc/CUGList/";
                string OutStatus = ObjCustomerInputBL.GetConnection(ObjInput, URL);
                var serializer = new JavaScriptSerializer();
                //AllTypeDetails.Value = OutStatus;
                DataSet data = JsonConvert.DeserializeObject<DataSet>(OutStatus);
                string ds = data.Tables[0].Rows[0]["ErrorMessage"].ToString();
                if (ds == "already exist")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Vehicle Number has already registered')", true);
                    GetCUGList();
                }

                else if (ds == "Not Use")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Vehicle Number is not in freightX')", true);
                    GetCUGList();
                }

                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Insert Request Sent to Admin')", true);



                    GetCUGList();
                }
            }
            AddNewRowToGrid();
        }


        else if (e.CommandName == "ProcessThisUpdate")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = Gridview1.Rows[index];

            if (row.RowType == DataControlRowType.DataRow)
            {
                TextBox textBox = row.FindControl("Dri_Name") as TextBox;
                CheckBox chk = row.FindControl("Vehitype") as CheckBox;

                ObjInput.CustId = HttpContext.Current.Session["VidStr"].ToString();
                ObjInput.CustName = HttpContext.Current.Session["FirstName"].ToString();
                ObjInput.Update = "Update";
                ObjInput.VehiNo = textBox.Text;

                if (chk.Checked == true)
                {
                    ObjInput.VehiChk = 1;
                }

                else
                {
                    ObjInput.VehiChk = 0;
                }

                string URL = "http://*******/CustomerServices.svc/CUGList/";
                string OutStatus = ObjCustomerInputBL.GetConnection(ObjInput, URL);
                var serializer = new JavaScriptSerializer();
                //AllTypeDetails.Value = OutStatus;
                DataSet data = JsonConvert.DeserializeObject<DataSet>(OutStatus);
                string ds = data.Tables[0].Rows[0]["ErrorMessage"].ToString();
                if (ds == "already exist")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Vehicle Number has already registered')", true);
                    GetCUGList();
                }

                else if (ds == "Not Use")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Vehicle Number is not in freightX')", true);
                    GetCUGList();
                }

                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Update Request Sent to Admin')", true);
                    GetCUGList();
                }
            }
        }

    }
DInesh AG
  • 316
  • 4
  • 20
  • You can take a look at these posts: [Why in ASP.NET is a button click event executes when page is refreshed?](http://stackoverflow.com/questions/2379720/why-in-asp-net-is-a-button-click-event-executes-when-page-is-refreshed), [How to avoid duplicate entry from asp.net on Postback?](http://stackoverflow.com/questions/11511481/how-to-avoid-duplicate-entry-from-asp-net-on-postback), [how to prevent data from sending if user press F5 or refresh in asp.net?](http://stackoverflow.com/questions/12353461/how-to-prevent-data-from-sending-if-user-press-f5-or-refresh-in-asp-net). – ConnorsFan Jul 30 '16 at 15:20
  • Here is another article about that same problem: [Detect Browser Refresh to avoid events fired again in ASP.NET](http://www.aspdotnet-suresh.com/2010/04/detect-browser-refresh-to-avoid-events.html). – ConnorsFan Jul 30 '16 at 15:21
  • One more: [Detecting Page Refresh in ASP.NET WebForms](http://stevenbey.com/detecting-page-refresh-in-aspnet-webforms-redux); – ConnorsFan Jul 30 '16 at 15:39

0 Answers0