【问题标题】:gridview id in rowdatabound行数据绑定中的gridview id
【发布时间】:2012-09-02 19:39:32
【问题描述】:

我有一个 rowdatabound 方法,这对于两个 gridviews 来说很常见。此方法的部分任务是将值分配给 gridview 的最后一列。

gridviews 是相同的,但是两个gridviews 的值不同。所以我需要检查我是否将 vaules 分配给第一个 gridview 或另一个。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Here i need to check which gridview (Gridview1 or Gridview2)
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            {
                int CellValue = Convert.ToInt32(e.Row.Cells[0].Text);

                if (CellValue == 1)
                    e.Row.Cells[7].Text = "" + patchWeekTwo[0] + "t";
                else if (CellValue == 2)
                    e.Row.Cells[7].Text = "" + patchWeekTwo[1] + "t";
                else if (CellValue == 3)
                    e.Row.Cells[7].Text = "" + patchWeekTwo[2] + "t";
                else if (CellValue == 4)
                    e.Row.Cells[7].Text = "" + patchWeekTwo[3] + "t";
                else
                    e.Row.Cells[7].Text = "" + patchWeekTwo[4] + "t";
            }
        }
     }

【问题讨论】:

    标签: c# asp.net gridview rowdatabound


    【解决方案1】:

    你可以检查senderGridView1还是GridView2

    if( sender == GridView1 ){}
    else{}
    

    请注意,这仅在 GridView1 声明在页面顶部而不是在其中一个子 NamingContainers 中时才有效。然后你可以检查id:

    var grid = (GridView)sender;
    if( grid.Id == "GridView1" ){}
    else{}
    

    【讨论】:

      【解决方案2】:

      我认为应该这样做

      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
           GridView gv = (GridView)sender;
           if(gv.ID == "gv1")
              //do this
           else
              //do that
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多