Hassan
Hassan

Reputation: 5430

gridview RowUpdating event using asp:Command Field type button

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();
 }

Upvotes: 0

Views: 3418

Answers (1)

briskovich
briskovich

Reputation: 690

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:

Upvotes: 0

Related Questions