[英]GridView Edit Event firing after 2 clicks
I have a GridView which has Edit/Update functionality. 我有一个GridView,它具有编辑/更新功能。 However, when I am clicking it once, it is not firing. 但是,当我点击它一次时,它没有开火。 I have to click it again to fire. 我必须再次点击它才能开火。 Rest part is working fine. 休息部分工作正常。
Can somebody tell me whats going on? 有人能告诉我发生了什么事吗? Here is the markup for my GV: 这是我的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;
}
}
I don't expect you to choose my answer, but you should be wrapping your datacontext's in a using
block: 我不指望你选择我的答案,但你应该将datacontext包装在一个using
块中:
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();
}
}
It automatically wraps your LINQ in a try/catch block and disposes of it after. 它会自动将您的LINQ包装在try / catch块中,然后将其处理掉。
Put the code that you have written in page_load event in a function like 将您在page_load事件中编写的代码放在类似的函数中
private void Binddata()
{
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();
} }
And call this function on row_editing and row_cancellingedit events like this 并在这样的row_editing和row_cancellingedit事件上调用此函数
protected void gvShowRegistration_RowEditing(object sender, GridViewEditEventArgs e)
{
gvShowRegistration.EditIndex = e.NewEditIndex;
Binddata();
}
protected void gvShowRegistration_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvShowRegistration.EditIndex = -1;
Binddata();
}
Put this in another method 把它放在另一种方法中
private void BindData(int id)
{
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 == id select new
{
};
gvShowRegistration.DataSource = tr;
gvShowRegistration.DataBind();
}
The on Page Load do this: on Page Load执行此操作:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostback())
{
BindData(Request.QueryString["EntityId"]);
}
}
This is only half the fix, what causes the EntityId to change? 这只是修复的一半,导致EntityId改变的原因是什么? Will it change on postbacks? 它会在回发中改变吗? If so you will have to adjust for that. 如果是这样,你将不得不调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.