熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

GridView根據條件修改命令按鈕的顯示的文字

2013-11-13 10:32:20  來源: .NET編程 

  在進行GridView綁定的時候有時候使用CommandField 進行綁定但要根據不同的條件動態改變顯示的文 字則是比較麻煩的當然什麼模板列則是非常簡單的下面就提供一種使用綁定列修改命令按鈕顯示文字的方法

  CommandField 有個ButtonType屬性個枚舉值ButtonImageLink根據不同的設置顯示不同的類型那麼就可以在綁定事件裡進行修改顯示的文字

  ASPX 代碼
<%@ Page Language=C# %>

  <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>

  <script runat=server>
  // 計算數據完全可以從數據看取得
  SystemDataDataView CreateDataSourceByXianhuiMeng()
  {
    SystemDataDataTable dt = new SystemDataDataTable();
    SystemDataDataRow dr;
    dtColumnsAdd(new SystemDataDataColumn(id typeof(SystemInt)));
    dtColumnsAdd(new SystemDataDataColumn(學生姓名 typeof(SystemString)));
    dtColumnsAdd(new SystemDataDataColumn(語文 typeof(SystemDecimal)));
    dtColumnsAdd(new SystemDataDataColumn(數學 typeof(SystemDecimal)));
    dtColumnsAdd(new SystemDataDataColumn(英語 typeof(SystemDecimal)));
    dtColumnsAdd(new SystemDataDataColumn(計算機 typeof(SystemDecimal)));

  for (int i = ; i < ; i++)
    {
      SystemRandom rd = new SystemRandom(EnvironmentTickCount * i); ;
      dr = dtNewRow();
      dr[] = i * i;
      dr[] = 【孟子E章】 + iToString();
      dr[] = SystemMathRound(rdNextDouble() * );
      dr[] = SystemMathRound(rdNextDouble() * );
      dr[] = SystemMathRound(rdNextDouble() * );
      dr[] = SystemMathRound(rdNextDouble() * );
      dtRowsAdd(dr);
    }
    SystemDataDataView dv = new SystemDataDataView(dt);
    return dv;
  }
  protected void Page_Load(object sender EventArgs e)
  {
    if (!PageIsPostBack)
    {
      GridViewDataSource = CreateDataSourceByXianhuiMeng();
      GridViewDataBind();
    }
  }

  protected void GridView_RowDataBound(object sender GridViewRowEventArgs e)
  {
    if (eRowRowType == DataControlRowTypeDataRow)
    {
      SystemDataDataRowView dv = eRowDataItem as SystemDataDataRowView;
      if (DecimalParse(dv[語文]ToString()) > )
      {
        LinkButton b = eRowCells[]Controls[] as LinkButton;
        bText = 凍結;
      }
    }
  }

  protected void GridView_RowDeleting(object sender GridViewDeleteEventArgs e)
  {
    GridView gv = sender as GridView;
    ResponseWrite(要進行操作的主鍵的值 + gvDataKeys[eRowIndex]Value);
  }
</script>

  <html xmlns=>
<head id=Head runat=server>
  <title></title>
</head>
<body>
  <form id=form runat=server>
  <asp:GridView ID=GridView runat=server AutoGenerateColumns=False DataKeyNames=id
    OnRowDataBound=GridView_RowDataBound OnRowDeleting=GridView_RowDeleting>
    <Columns>
      <asp:BoundField DataField=id HeaderText=id />
      <asp:CommandField HeaderText=狀態 ShowDeleteButton=True DeleteText=state />
      <asp:BoundField DataField=語文 HeaderText=語文成績 />
    </Columns>
  </asp:GridView>
  </form>
</body>
</html>


From:http://tw.wingwit.com/Article/program/net/201311/13942.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.