I have a GridView which has Edit/Update functionality. 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:
<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:
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.
Put the code that you have written in page_load event in a function like
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
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:
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? Will it change on postbacks? If so you will have to adjust for that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.