【问题标题】:rowcommand doesnt work in first timerowcommand 在第一次不起作用
【发布时间】:2012-06-03 22:23:09
【问题描述】:

我在 rowcommand 中有一个很大的错误。我有一个命令名为“add”的按钮字段。当我点击它时,代码不会第一次触发,但再次点击它,代码就会触发!

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!

【问题讨论】:

    标签: asp.net gridview rowcommand


    【解决方案1】:

    您应该在回发时将数据源绑定到 Gridview。

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

    【讨论】:

      【解决方案2】:

      我发现了问题,我使用的是导致问题的GridView_RowCreated,而不是使用GridView_RowDataBound,它为我解决了问题。

      或检查您是否将数据网格绑定在不回发中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-16
        • 2018-10-25
        • 2018-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多