2011-07-29 145 views
0

我有GridView.AutoGenerateColumn=trueGridView RowCommand事件不会触发页脚模板按钮点击

我创建上的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++; 
      } 
     } 
    } 
} 

回答

1

在哪里(f.e grdMaterialPercentage.DataBind())位于第一线? 如果在Page_Load中,只有在!Page.IsPostback?的情况下,您是否绑定GridView否则,GridView会再次绑定到数据源,从而防止RowCommand事件被触发。

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 
     } 
    } 

这将会把你的命令参数到一个文本框,或者你可以简单地把一些文字一个标签和检查,是否执行。

相关问题