开发者

Having to click twice in my edit gridview

OK So I have a web app with a dropdown field, a show button and a gridview that i can edit. Page loads, I choose my dropdown value, page loads fine. When I go to click the edit button however, I have to click it twice in order to be able to edit or cancel (having issues with that also but thats a different issue) Anyway, I want to be able to have one click on edit to bring up the update/cancel editmode. I'm new to C# Web apps so some insight would be helpful.

Thanks

My ASP

<asp:GridView ID="GridView1" runat="server" CssClass="styled" 
   OnRowEditing="TaskGridView_RowEditing"
   OnRowCancelingEdit="TaskGridView_RowCancelingEdit" 
   OnRowUpdating="TaskGridView_RowUpdating"   >
   <Columns>
        <asp:CommandField ShowEditButton="True" />
   </Columns>
</asp:GridView>

My C#

protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{

    //Set the edit index.
    GridView1.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.
   // BindData();


}

protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    //Reset the edit index.
    GridView1.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();
    Image1.Visible = true;
    Image2.Visible = false;

}

protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //Retrieve the table from 开发者_Go百科the session object.
    System.Data.DataTable dt = (System.Data.DataTable)Session["EditDataPage"];

    //Update the values.
    GridViewRow row = GridView1.Rows[e.RowIndex];
   // dt.Rows[row.DataItemIndex]["QuoteNumber"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["ItemNumber"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
   //dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
   // dt.Rows[row.DataItemIndex]["Item"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["Descp"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["Route"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["Unit"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["IG"] = ((TextBox)(row.Cells[7].Controls[0])).Text;
   // dt.Rows[row.DataItemIndex]["EXTQTY"] = ((TextBox)(row.Cells[8].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["CSTCD"] = ((TextBox)(row.Cells[9].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["PCOST"] = ((TextBox)(row.Cells[10].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["SCOST"] = ((TextBox)(row.Cells[11].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["ACOST"] = ((TextBox)(row.Cells[12].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["TCOST"] = ((TextBox)(row.Cells[13].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["ICOST"] = ((TextBox)(row.Cells[14].Controls[0])).Text;
  //  dt.Rows[row.DataItemIndex]["BIZCODE"] = ((TextBox)(row.Cells[16].Controls[0])).Text;
 //   dt.Rows[row.DataItemIndex]["DeleteItem"] = ((TextBox)(row.Cells[17].Controls[0])).Text;

    //Reset the edit index.
    GridView1.EditIndex = -1;

    //Bind data to the GridView control.
    BindData();
}

private void BindData()
{

    GridView1.DataSource = Session["Sqldatasource1"];
    GridView1.DataBind();


in the rowediting event you do need to set editindex and then databind. you need to do it in the other events too. see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting.aspx

GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();

your control may disappear if you databind unnecessarily. make sure you only databind in page_load once.

if(!Page.IsPostBack)
    GridView1.DataBind();


My fix for having to click edit twice included ViewState.

My original Load and bind:

AttribGrid.DataSource = dataset;
AttribGrid.DataBind();
ViewState["CurTable"] = dataset;

then my subsequent RowEditing.

protected void AttribGrid_RowEditing(object sender, GridViewEditEventArgs e)
	{
		AttribGrid.EditIndex = e.NewEditIndex;
		AttribGrid.DataSource = (DataSet)ViewState["CurTable"];
		AttribGrid.DataBind();
		
	}

I did have to use EditIndex = newEditIndex. When I excluded it I still had to click twice. with the above code it was click once as expected.


You do not need to set the "GridView1.EditIndex" in the Editing or Canceling callback.

protected void My_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
// Perform a custom action in here
// But you don't need to set GridView1.EditIndex in here, that would be bad.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜