RG-3
RG-3

Reputation: 6188

GridView Edit Event firing after 2 clicks

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;

    }


}

Upvotes: 1

Views: 1778

Answers (3)

Gaurav Kumar
Gaurav Kumar

Reputation: 11

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();
    }

Upvotes: 1

Ta01
Ta01

Reputation: 31610

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.

Upvotes: 1

The Muffin Man
The Muffin Man

Reputation: 20004

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.

Upvotes: 2

Related Questions