【问题标题】:gridview RowUpdating event using asp:Command Field type button使用asp的gridview RowUpdating事件:命令字段类型按钮
【发布时间】:2012-11-16 20:48:27
【问题描述】:

我正在使用 asp 命令字段类型按钮来编辑 gridview 行。 每当用户更新记录时,假设网格中不会显示一行。 ShowAllClasses() 方法假设从 sql db 中获取记录,不包括最近更新的行。

“更新”命令字段按钮的现在行为。

本地主机: 当我单击按钮时,它会隐藏该行(意味着再次完成绑定)。在这种情况下,记录会更新一次。 (根据需要)

在已部署的应用程序上: 用户单击后,该行不会隐藏,并且用户可以在按钮上单击多次(好像功能不起作用)。但是当用户停止单击按钮时,在稍有延迟后,gridview 的编辑模式就会消失,并且会在触发更新后绑定。

不好的是每次单击按钮时 sql 表都会更新。 帮我解决这个问题。

这是我正在使用的代码

GridView1 内的命令字段标记

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

GridView RowUpdating 事件

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

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

【问题讨论】:

    标签: c# asp.net ajax gridview commandfield


    【解决方案1】:

    在行更新事件结束时禁用更新链接按钮。然后在行编辑事件中将控件设置为活动。这将阻止用户单击按钮两次。听起来与生产服务器的连接不好。

      this.mybutton.enabled = false:
    

    【讨论】:

    • 感谢您的回复。我已经尝试过这个解决方案,但在这种情况下不起作用。
    • 不断点击 CommandField 按钮会导致数据库表中的多次更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多