GridView RowCommand事件未在页脚模板按钮单击时触发

时间:2011-07-29 10:36:08

标签: c# asp.net .net rowcommand

我有GridView.AutoGenerateColumn=true

我在rowdatabound上创建了按钮,当我点击按钮行命令事件未被触发时

这是我的代码:

dt = ESalesUnityContainer.Container.Resolve<IAgentService>().GetAgentMaterialPercentage();
grdMaterialPercentage.DataSource = dt;
grdMaterialPercentage.DataBind();

protected void grdMaterialPercentage_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (grdMaterialPercentage.AutoGenerateColumns == true)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Visible = false;
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Visible = false;
            if (DataBinder.Eval(e.Row.DataItem, "AgentName").ToString() != string.Empty)
            {
                int i = 0;
                foreach (TableCell c in e.Row.Cells)
                {
                    if (i >= 3)
                    {
                        TextBox tb = new TextBox();
                        tb.Text = c.Text;
                        tb.Style.Add("Width", "25px");
                        tb.Style.Add("Height", "15px");
                        c.Controls.Clear();
                        c.Controls.Add(tb);

                    }
                    i++;
                }
            }
            else
            {
                e.Row.Visible = false;
            }
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Visible = false;
            int j = 0;
            foreach (TableCell c in e.Row.Cells)
            {

                if (j >= 3)
                {
                    DataRow dr = dt.Rows[dt.Rows.Count - 1];
                    LinkButton btn = new LinkButton();

                    btn.ID = j.ToString();

                    btn.CommandName ="fghfh"+j.ToString();
                    btn.Text = "Save" + dr[j - 1].ToString();
                    btn.CssClass = "button";
                    btn.Style.Add("align", "center");
                    btn.CommandArgument = dr[j - 1].ToString();
                  //  btn.OnClientClick = "return ValidateTotalPercentage(this)";
                    c.Controls.Clear();
                    c.Controls.Add(btn);

                } j++;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

第一行(f.e。grdMaterialPercentage.DataBind())位于何处? 如果在Page_Load中,仅在!Page.IsPostback?时绑定GridView,否则GridView再次绑定到数据源会阻止RowCommand事件被触发。

答案 1 :(得分:1)

您是否处理了Row_Command事件?您必须检查相应的命令然后测试它:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
      if (e.CommandName == "Select")
      {
          int num = Convert.ToInt32(e.CommandArgument);

          instTextBox.Text = GridView1.Rows[num].Cells[1].Text;

          //Or you can also do dis
          //Set Label lblTest.text = "It Executes"; 
          //just to check if your code reaches here
      }
  }

这会将您的Command Argument放入文本框,或者您只需将一些文本放入标签中并检查它是否已执行。