rowcommand doesnt work in first time

1.1k views Asked by At

I have a big mistake in rowcommand. I have a button field with commandname "add". When I click it the code doesn't fire the first time but click it again and the code fires!

if (e.CommandName == "add")
{
 DataClassesDataContext db = new DataClassesDataContext();
 int ii = int.Parse(e.CommandArgument.ToString());
 int num = int.Parse(((TextBox)GridView1.Rows[ii].FindControl("TextBox2")).Text);
       string id = GridView1.Rows[ii].Cells[0].Text;
                    temp t = new temp();
                    t.tedad = num;
                    t.username = Session["username"].ToString();
                    db.temps.InsertOnSubmit(t);
                    db.SubmitChanges();
}

rowcommand dose not fire when clicking the first time!

2

There are 2 answers

0
Ashwini Verma On

You should bound the Datasource to Gridview on postback.

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    GridView1.Datasource = DataTable1;
    GridView1.DataBind();
  }
}
0
user1434146 On

I figured out the issue, I was using GridView_RowCreated which was causing the problem, instead I used GridView_RowDataBound which solved the problem for me.

or check that if you are binding the datagrid in not post back .