c# - GridView 编辑/更新事件在 1 次单击后未触发

标签 c# asp.net linq linq-to-sql gridview

我有一个 GridView,它有正常的编辑/更新按钮。但是,我的 GV RowUpdating、RowEditing 和 RowCancelingEdit 只需单击 2 次即可工作。如果我点击一次,它就不起作用:(

这是我填充所有内容的代码:

public partial class Testing : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         string getEntity = Request.QueryString["EntityID"];
         int getIntEntity = Int16.Parse(getEntity);
         using (OISLinq2SqlVs1DataContext dt = new OISLinq2SqlVs1DataContext())
         {
             var tr = from r in dt.Users
                      join s in dt.Entities on r.Entity_ID equals s.ID
                      where s.ID == getIntEntity
                      select new
                      {
                          s.Name,
                          r.FirstName,
                          r.LastName,
                          s.Email,
                          //r.Email,
                          r.UserID,
                          r.Pwd,
                          s.Company,
                          s.Description,
                          s.Phone,
                          s.Fax,
                          s.WebSite
                          
                      };

             gvShowRegistration.DataSource = tr;
             gvShowRegistration.DataBind();
         }
    }

    protected void gvShowRegistration_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        OISLinq2SqlVs1DataContext dt = new OISLinq2SqlVs1DataContext();
    }

    protected void gvShowRegistration_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvShowRegistration.EditIndex = e.NewEditIndex;
    }

    protected void gvShowRegistration_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvShowRegistration.EditIndex = -1;
    }

这是我在代码后面的全部代码。我在这里做错了什么?

更新:

这是我的 GridView:

<asp:GridView ID="gvShowRegistration" runat="server" 
     Height="204px" Width="678px" 
    OnRowEditing = "gvShowRegistration_RowEditing" 
    OnRowUpdating = "gvShowRegistration_RowUpdating" 
    OnRowCancelingEdit = "gvShowRegistration_RowCancelingEdit" CssClass="menu">
    <Columns>
    <asp:CommandField HeaderText="Edit" ShowEditButton="True" ShowHeader="True" ShowSelectButton="True" />
    
    </Columns>
 </asp:GridView>

最佳答案

仅在未回发时绑定(bind)您的 GV。

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack){
        //You GV Databinding code
    }
}

关于c# - GridView 编辑/更新事件在 1 次单击后未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5425440/

相关文章:

c# - 如何转换项目并在 sitecore 中获取选定的项目?

c# - 表单在运行时可见,但在设计时不可见

c# - Dotnet 应用程序作为守护进程启动,无法正常工作

c# - 将数组中的字符串项添加到对象列表

asp.net - 在 asp .net 网站中从社交网络和电子邮件提供商导入联系人和 friend

asp.net - UpdatePanel.Visible = true 没有效果

c# - 选择模型属性包含所有键值对列表的位置

c# - WPF:应用程序在 Visual Studio Debug模式下的运行速度比可执行文件慢得多

c# - 如何去除 ListBoxItem 周围的小填充?

c# - 在 ASP.NET 中从数据库动态创建 DIV