GridView点击更新按钮两次才会触发

时间:2022-05-06 19:33:00
我的GridView中一列命令列,编辑更新,点击编辑按钮后,会正常转为编辑状态,但是再点击更新按钮时,第一次点击,此行中所有输入框中的内容会清空,而且不会触发RowUpdating事件,第二次点击时才会触发RowUpdating事件.

GridView中有两列是模板列,数据绑定也能正常显示.

请问这种情况是什么原因呢

15 个解决方案

#1


后台代码

public partial class WorkSpace_FundsPlan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Databind();
        }
    }

    //数据绑定
    private void Databind()
    {
        FundsPlanManager funsPlanManager = new FundsPlanManager();
        UserInfo userInfo = (UserInfo)Session["UserInfo"];
        if (userInfo != null)
        {
            IList<FundsPlan> fundsPlan = funsPlanManager.GetListByDepartId(userInfo.Department.DepartId);
            GridView1.DataSource = fundsPlan;
            GridView1.DataBind();
        }
    }


    public string GetScript(string name)
    {
        return string.Format("return confirm(\"确定删除此资金使用计划[{0}]吗?\");", name);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        FundsPlan fundsPlan = new FundsPlan();
        fundsPlan.AddTime = DateTime.Now;
        fundsPlan.Amount = Convert.ToDecimal(txtAmount.Text.Trim());
        UserInfo user=(UserInfo)Session["UserInfo"];
        fundsPlan.DepartId = user.Department.DepartId;
        fundsPlan.Name = txtName.Text.Trim();
        fundsPlan.Paid = Convert.ToDecimal(txtPaid.Text.Trim());
        fundsPlan.Remarks = txtRemark.Text.Trim();
        fundsPlan.UnPaid = Convert.ToDecimal(txtUnPaid.Text.Trim());
        fundsPlan.UserId = user.UserId;
        FundsPlanManager fundsPlangerManager = new FundsPlanManager();
        int success=fundsPlangerManager.Add(fundsPlan);
        if (success > 0)
        {
            Databind();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Databind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        FundsPlanManager funsPlanManager = new FundsPlanManager();
        bool success=funsPlanManager.Delete(Convert.ToInt32(id));
        if (success)
        {
            GridView1.EditIndex = -1;
            Databind();
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Databind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        FundsPlan fundsPlan = new FundsPlan();
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
        decimal amount =Convert.ToDecimal(((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim());
        decimal paid = Convert.ToDecimal(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim());
        decimal unpaid = Convert.ToDecimal(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim());
        string remarks =((TextBox)GridView1.Rows[e.RowIndex].Cells[5].FindControl("txtEditRemarks")).Text.Trim();
        DateTime addTime =Convert.ToDateTime(((Label)GridView1.Rows[e.RowIndex].Cells[6].FindControl("lblEditAddTime")).Text.Trim());
        fundsPlan.PID = Int32.Parse(id);
        fundsPlan.Name = name;
        fundsPlan.Amount = amount;
        fundsPlan.Paid = paid;
        fundsPlan.UnPaid = unpaid;
        fundsPlan.Remarks = remarks;
        fundsPlan.AddTime = addTime;
        FundsPlanManager m = new FundsPlanManager();
        m.Update(fundsPlan);
        GridView1.EditIndex = -1;
        Databind();
    }


前台代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                    DataKeyNames="PID" onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
            onrowupdating="GridView1_RowUpdating">
                    <Columns>
                        <asp:BoundField DataField="PID" HeaderText="ID" />
                        <asp:BoundField DataField="Name" HeaderText="付款用途" />
                        <asp:BoundField DataField="Amount" HeaderText="暂定额度" />
                        <asp:BoundField DataField="Paid" HeaderText="已付累计" />
                        <asp:BoundField DataField="UnPaid" HeaderText="未付" />
                        <asp:TemplateField HeaderText="说明">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Remarks") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtEditRemarks" runat="server" Height="65px" 
                                    Text='<%# Bind("Remarks") %>' TextMode="MultiLine" Width="233px"></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="添加时间">
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("AddTime") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblEditAddTime" runat="server" Text='<%# Bind("AddTime") %>'></asp:Label>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowEditButton="True" />
                        <asp:CommandField ShowDeleteButton="True" />
                        <asp:CommandField SelectText="付款录入" ShowSelectButton="True" />
                    </Columns>
                </asp:GridView>

#2


检查是否回发。设置短点看下

#3


引用 2 楼 id270 的回复:
检查是否回发。设置短点看下

第一次点击更新按钮仅会触发PageLoad事件,第二次点击会触发PageLoad事件和RowUpdating事件

#4


第二次只需要在哪个事件触发

#5


奇怪了,我在另一个页面中又新建了一个不同的,还是同样的情况,是不是数据源的问题?

#6


自己顶一下

#7


引用 5 楼 idaydayup 的回复:
奇怪了,我在另一个页面中又新建了一个不同的,还是同样的情况,是不是数据源的问题?

貌似是数据没有及时更新释放

#8


引用 7 楼 lizhihe0522 的回复:
引用 5 楼 idaydayup 的回复:

奇怪了,我在另一个页面中又新建了一个不同的,还是同样的情况,是不是数据源的问题?

貌似是数据没有及时更新释放
释放?怎么释放呢?你的意思是数据连接没有关闭?

#9


LZ是用的MULTVIEW控件?

#10


Command指令?Update和Edit?

#11


刚在vs2010中试了一下没有问题

确认父控件的ID 是否明确

#12


没遇到过这种情况。。。。

#13


引用 9 楼 lubyam 的回复:
LZ是用的MULTVIEW控件?

用的是GridView啊

#14


引用 10 楼 zhaoyycit 的回复:
Command指令?Update和Edit?

是的,点击编辑按钮之后,进入编辑模式,显示更新和取消按钮,点击更新按钮就会有异常

#15


引用 11 楼 bonnibell 的回复:
刚在vs2010中试了一下没有问题

确认父控件的ID 是否明确

父控件?GridView没有父控件了

<body>
    <form id="form1" runat="server">
    <div> 
        <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False" 
            CellPadding="4" ForeColor="#333333" ShowFooter="True" DataKeyNames="ID" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
            onrowupdating="GridView1_RowUpdating">

#1


后台代码

public partial class WorkSpace_FundsPlan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Databind();
        }
    }

    //数据绑定
    private void Databind()
    {
        FundsPlanManager funsPlanManager = new FundsPlanManager();
        UserInfo userInfo = (UserInfo)Session["UserInfo"];
        if (userInfo != null)
        {
            IList<FundsPlan> fundsPlan = funsPlanManager.GetListByDepartId(userInfo.Department.DepartId);
            GridView1.DataSource = fundsPlan;
            GridView1.DataBind();
        }
    }


    public string GetScript(string name)
    {
        return string.Format("return confirm(\"确定删除此资金使用计划[{0}]吗?\");", name);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        FundsPlan fundsPlan = new FundsPlan();
        fundsPlan.AddTime = DateTime.Now;
        fundsPlan.Amount = Convert.ToDecimal(txtAmount.Text.Trim());
        UserInfo user=(UserInfo)Session["UserInfo"];
        fundsPlan.DepartId = user.Department.DepartId;
        fundsPlan.Name = txtName.Text.Trim();
        fundsPlan.Paid = Convert.ToDecimal(txtPaid.Text.Trim());
        fundsPlan.Remarks = txtRemark.Text.Trim();
        fundsPlan.UnPaid = Convert.ToDecimal(txtUnPaid.Text.Trim());
        fundsPlan.UserId = user.UserId;
        FundsPlanManager fundsPlangerManager = new FundsPlanManager();
        int success=fundsPlangerManager.Add(fundsPlan);
        if (success > 0)
        {
            Databind();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Databind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        FundsPlanManager funsPlanManager = new FundsPlanManager();
        bool success=funsPlanManager.Delete(Convert.ToInt32(id));
        if (success)
        {
            GridView1.EditIndex = -1;
            Databind();
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Databind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        FundsPlan fundsPlan = new FundsPlan();
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
        decimal amount =Convert.ToDecimal(((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim());
        decimal paid = Convert.ToDecimal(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim());
        decimal unpaid = Convert.ToDecimal(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim());
        string remarks =((TextBox)GridView1.Rows[e.RowIndex].Cells[5].FindControl("txtEditRemarks")).Text.Trim();
        DateTime addTime =Convert.ToDateTime(((Label)GridView1.Rows[e.RowIndex].Cells[6].FindControl("lblEditAddTime")).Text.Trim());
        fundsPlan.PID = Int32.Parse(id);
        fundsPlan.Name = name;
        fundsPlan.Amount = amount;
        fundsPlan.Paid = paid;
        fundsPlan.UnPaid = unpaid;
        fundsPlan.Remarks = remarks;
        fundsPlan.AddTime = addTime;
        FundsPlanManager m = new FundsPlanManager();
        m.Update(fundsPlan);
        GridView1.EditIndex = -1;
        Databind();
    }


前台代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                    DataKeyNames="PID" onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
            onrowupdating="GridView1_RowUpdating">
                    <Columns>
                        <asp:BoundField DataField="PID" HeaderText="ID" />
                        <asp:BoundField DataField="Name" HeaderText="付款用途" />
                        <asp:BoundField DataField="Amount" HeaderText="暂定额度" />
                        <asp:BoundField DataField="Paid" HeaderText="已付累计" />
                        <asp:BoundField DataField="UnPaid" HeaderText="未付" />
                        <asp:TemplateField HeaderText="说明">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Remarks") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtEditRemarks" runat="server" Height="65px" 
                                    Text='<%# Bind("Remarks") %>' TextMode="MultiLine" Width="233px"></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="添加时间">
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("AddTime") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblEditAddTime" runat="server" Text='<%# Bind("AddTime") %>'></asp:Label>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowEditButton="True" />
                        <asp:CommandField ShowDeleteButton="True" />
                        <asp:CommandField SelectText="付款录入" ShowSelectButton="True" />
                    </Columns>
                </asp:GridView>

#2


检查是否回发。设置短点看下

#3


引用 2 楼 id270 的回复:
检查是否回发。设置短点看下

第一次点击更新按钮仅会触发PageLoad事件,第二次点击会触发PageLoad事件和RowUpdating事件

#4


第二次只需要在哪个事件触发

#5


奇怪了,我在另一个页面中又新建了一个不同的,还是同样的情况,是不是数据源的问题?

#6


自己顶一下

#7


引用 5 楼 idaydayup 的回复:
奇怪了,我在另一个页面中又新建了一个不同的,还是同样的情况,是不是数据源的问题?

貌似是数据没有及时更新释放

#8


引用 7 楼 lizhihe0522 的回复:
引用 5 楼 idaydayup 的回复:

奇怪了,我在另一个页面中又新建了一个不同的,还是同样的情况,是不是数据源的问题?

貌似是数据没有及时更新释放
释放?怎么释放呢?你的意思是数据连接没有关闭?

#9


LZ是用的MULTVIEW控件?

#10


Command指令?Update和Edit?

#11


刚在vs2010中试了一下没有问题

确认父控件的ID 是否明确

#12


没遇到过这种情况。。。。

#13


引用 9 楼 lubyam 的回复:
LZ是用的MULTVIEW控件?

用的是GridView啊

#14


引用 10 楼 zhaoyycit 的回复:
Command指令?Update和Edit?

是的,点击编辑按钮之后,进入编辑模式,显示更新和取消按钮,点击更新按钮就会有异常

#15


引用 11 楼 bonnibell 的回复:
刚在vs2010中试了一下没有问题

确认父控件的ID 是否明确

父控件?GridView没有父控件了

<body>
    <form id="form1" runat="server">
    <div> 
        <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False" 
            CellPadding="4" ForeColor="#333333" ShowFooter="True" DataKeyNames="ID" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
            onrowupdating="GridView1_RowUpdating">