【问题标题】:GridView RowCommand event is not firing on footer template button clickGridView RowCommand 事件未在页脚模板按钮单击时触发
【发布时间】:2011-07-29 10:36:07
【问题描述】:

我有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++;
            }
        }
    }
}

【问题讨论】:

    标签: c# asp.net .net rowcommand


    【解决方案1】:

    第一行(例如grdMaterialPercentage.DataBind())在哪里? 如果在 Page_Load 中,是否仅在 !Page.IsPostback? 时绑定 GridView 否则 GridView 会再次绑定到数据源,这会阻止触发 RowCommand-Event。

    【讨论】:

      【解决方案2】:

      您处理过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
            }
        }
      

      这会将您的命令参数放入文本框中,或者您可以简单地将一些文本放入标签中并检查它是否被执行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-11
        • 2012-03-14
        • 1970-01-01
        相关资源
        最近更新 更多