0

I am using asp command field type button for editing gridview row. A row is suppose to be not showing in the grid whenever user update a record. The ShowAllClasses() method is suppose to fetch records from sql db excluding recently updated row.

Now behavior of "Update" Command Field Button.

Localhost: As I click on the button it hides the row (means binding is done again). In this case record gets updated once. (as required)

On deployed application: Upon user click the row doesn't hide and user is able to click many times on the button (as if function is not working). But as user stops clicking button, after a minor delay, editing mode of gridview goes away and binding after update is fired.

Bad thing is that the sql table updates each time button is clicked. Help me how I can fix this.

Here is code I am using

Markup of command field inside GridView1:

<asp:CommandField ButtonType="Button" HeaderText="Edit/Update" ShowEditButton="True"
                            ShowHeader="True" UpdateText="Set Class" ItemStyle-HorizontalAlign="Center" />

GridView RowUpdating Event:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

        lblError.Text = string.Empty;

        int iTutorID = Convert.ToInt32(Session["EID"]);

        DropDownList ddlTime = GridView1.Rows[e.RowIndex].FindControl("ddlClassTime") as DropDownList;
        string sClassTime = ddlTime.SelectedItem.Text;

        HiddenField hf = GridView1.Rows[e.RowIndex].FindControl("sIdHidden") as HiddenField;
        int iSID = Convert.ToInt32(hf.Value);

        hf = GridView1.Rows[e.RowIndex].FindControl("cIdHidden") as HiddenField;
        int iCID = Convert.ToInt32(hf.Value);

        hf = GridView1.Rows[e.RowIndex].FindControl("hfClassTime") as HiddenField;
        string sOriginalClassTime = hf.Value.ToString();

        if (sOriginalClassTime.Length < 8)
            sOriginalClassTime = "0" + sOriginalClassTime;

        DropDownList ddlDate = GridView1.Rows[e.RowIndex].FindControl("ddlClassDate") as DropDownList;
        DateTime dNewDate = Convert.ToDateTime(ddlDate.SelectedValue);

        Label lblLesson = GridView1.Rows[e.RowIndex].FindControl("lblLesson") as Label;
        string sLesson = lblLesson.Text;

        DateTime dClassDate = Convert.ToDateTime(ddlAdvDay.SelectedValue);

        //cond: same date and same time set
        if (sOriginalClassTime == sClassTime && dNewDate == dClassDate.Date)
        {
            //show error
            lblError.Text = "Same class date and time cannot be set as Advanced class";
            return;
        }

        lblError.Text = string.Empty;

        BLClass blClass = new BLClass();
        //1. insert in makeup table today's class
        //2. insert in classdetails table

        //1. insert in makeup table with today's date
        blClass.InsertAdvancedClass(iTutorID, iSID, iCID, dClassDate, dNewDate);

        //2. insert in classdetails table
        blClass.InsertClassDetails(iTutorID, iSID, iCID, dNewDate, sClassTime, "Advanced", sLesson);           


        GridView1.EditIndex = -1;
        ShowAllClasses();
}

Method for Binding Grid with DataSource:

private void ShowAllClasses()
{
        lblError.Text = string.Empty;

        int iTutorID = Convert.ToInt32(Session["EID"]);    

        BLClass blClass = new BLClass();

        DateTime dClassDate = Convert.ToDateTime(ddlAdvDay.SelectedValue);


        DataTable dtClass = blClass.GetAdvancedClasses(iTutorID, dClassDate.Date);            

        //temp method for date display format
        FixDateFormat(dtClass);
        dtClass.AcceptChanges();

        GridView1.DataSource = dtClass;
        GridView1.DataBind();
 }

1 Answer 1

0

Disable the update link button at the end of the row updating event. Then in the row editing event set the control to active. This would keep the user from clicking the button twice. Sounds like a bad connection to production server.

  this.mybutton.enabled = false:
2
  • Thank you for reply. I have tried this solution but not working in this case.
    – Hassan
    Nov 17, 2012 at 16:44
  • Constantly clicking on CommandField button leads to multiple updates in the database table.
    – Hassan
    Nov 21, 2012 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.