c# - 取消行命令不适用于 GridView

标签 c# .net gridview

我有一个位于更新面板内的 GridView 。当我在编辑后单击“取消”时,没有任何反应。当我调试时,它确实进入了我的 gvWorkhours_RowCommand 函数并在取消 if 中,但屏幕上没有任何反应(编辑字段仍然可见)

这是我所拥有的:

<asp:GridView ID="gvWorkhours" runat="server" AutoGenerateColumns="false" CssClass="GridViewStyle" OnRowEditing="gvWorkhours_RowEditing"  
 OnRowCommand="gvWorkhours_RowCommand"  >
<EmptyDataTemplate>
no data returned
</EmptyDataTemplate>
<Columns>
<asp:commandfield buttontype="Link" showeditbutton="true" edittext="Edit" />
     <asp:TemplateField HeaderText="Organization">
        <ItemTemplate>
            <%# Eval("org.orgCode").ToString() + "- " + Eval("org.orgSubCode").ToString()%>
        </ItemTemplate>
    </asp:TemplateField>
 <asp:TemplateField HeaderText="Year-Qtr">
        <ItemTemplate>
            <%# Eval("year").ToString() + "- " + Eval("qtr").ToString()%>
        </ItemTemplate>
    </asp:TemplateField>
.......






 protected void gvWorkhours_RowEditing(Object sender, GridViewEditEventArgs e)
    {
        populateGrid();  //pulls from the Db and binds to the grid
        gvWorkhours.EditIndex = e.NewEditIndex;  //This is the selected row to edit
        gvWorkhours.DataBind();  //Make the edit Template show up

    }

    protected void gvWorkhours_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "Cancel")
        {
            gvWorkhours.EditIndex = -1;
            populateGrid(); 
        }

    }





private void populateGrid()
    {
        //getting all variables for update here.
            Workhours wh = new Workhours(selectedItem, year, qtr);
            gvWorkhours.DataSource = wh.exposures;
            gvWorkhours.DataBind();
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
            //  throw(ex);
        }
    }

我在这里错过了什么?

最佳答案

您的示例中没有显示 UpdatePanel,所以我不知道您是如何设置它的,但是如果您将 UpdateMode 设置为 Conditional,您可能需要在更新面板上手动调用 Update 方法:

    if (e.CommandName == "Cancel")
    {
        gvWorkhours.EditIndex = -1;
        populateGrid(); 
        UpdatePanel1.Update();   // whatever the name of the UpdatePanel is
    }

关于c# - 取消行命令不适用于 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17579767/

相关文章:

c# - Microsoft 身份验证自动登录帐户。用户手动选择账户时导致 "Correlation failed"错误

c# - Try-Catch 和 "Continue"- 这可能吗?

c# - ReSharper 重新格式化、数组初始化、逗号后的空格

javascript - 如何在GridView中选择和检查DropDownList的变化?

c# - 在中继器内的单个单元格中插入表格

java - 从服务发送数据到远程套接字时出现问题

jquery - 使用jquery跨域webmethod调用

c# - 使用带有 try catch 的语句。 using 语句的实例会发生什么?

c# - CSharpTest.Net.Collections.BPlusTree RecentCache 错误?

c# - DataGridView 单击事件并不总是触发