0

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!

pb2q
  • 54,061
  • 17
  • 135
  • 139
WEB DEV
  • 9
  • 3

2 Answers2

1

You should bound the Datasource to Gridview on postback.

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    GridView1.Datasource = DataTable1;
    GridView1.DataBind();
  }
}
Ashwini Verma
  • 7,288
  • 4
  • 33
  • 56
0

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 .

Botz3000
  • 37,236
  • 8
  • 100
  • 125