Skip to main content
 首页 » 编程设计

c#之单击 2 次后触发 GridView 编辑事件

2024年02月23日18shanyou

我有一个具有编辑/更新功能的 GridView。但是,当我单击它一次时,它没有触发。我必须再次单击它才能开火。其余部分工作正常。

谁能告诉我这是怎么回事?这是我的 GV 的标记:

<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> 
 
 
 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); 
 
            TestLinq2SqlVs1DataContext dt = new TestLinq2SqlVs1DataContext(); 
            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 
                     { 
 
 
                     }; 
 
            gvShowRegistration.DataSource = tr; 
            gvShowRegistration.DataBind(); 
 
 
    } 
 
 
 
    protected void gvShowRegistration_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
        //Update code goes here!!! 
 
    } 
 
 
    protected void gvShowRegistration_RowEditing(object sender, GridViewEditEventArgs e) 
    { 
        gvShowRegistration.EditIndex = e.NewEditIndex; 
 
    } 
 
 
    protected void gvShowRegistration_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
    { 
        gvShowRegistration.EditIndex = -1; 
 
    } 
 
 
} 

请您参考如下方法:

我不希望您选择我的答案,但您应该将数据上下文包装在 using block 中:

protected void Page_Load(object sender, EventArgs e) 
{ 
        string getEntity = Request.QueryString["EntityID"]; 
        int getIntEntity = Int16.Parse(getEntity); 
        using (TestLinq2SqlVs1DataContext dt = new TestLinq2SqlVs1DataContext()) 
        { 
          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 
                   { 
 
 
                   }; 
 
          gvShowRegistration.DataSource = tr; 
          gvShowRegistration.DataBind(); 
        } 
} 

它会自动将您的 LINQ 包装在 try/catch block 中并在之后处理它。