在进行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> |