DataGrid中想实现这样的效果 根据某一字段列的值动态改变按钮的文本比如 点击按钮列自动更新某列原为的值为并将按钮列的文本改为置 再按下自动更新某列原为的值为并将按钮列的文本改为置 最终通过NamingContainer实现! 方法如下 <asp:DataGridid=DataGridrunat=serverAutoGenerateColumns=False> <Columns> <asp:BoundColumnDataField=HonoreeIDHeaderText=ID></asp:BoundColumn> <asp:BoundColumnDataField=statusHeaderText=状态> <HeaderStyleWidth=px></HeaderStyle> </asp:BoundColumn> <asp:TemplateColumnHeaderText=状态是否为> <HeaderStyleHorizontalAlign=CenterWidth=%></HeaderStyle> <ItemStyleHorizontalAlign=Center></ItemStyle> <ItemTemplate> <asp:Labelid=lbrunat=serverVisible=falseText=<%#((DataBinderEval(ContainerDataItemstatus{}))==)?是:<fontcolor=red>否</font>%>> </asp:Label> <asp:ButtonID=changeStateRunat=serverText=<%#((DataBinderEval(ContainerDataItemstatus{}))==)?转为:转为%>> </asp:Button> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> 后台 protectedSystemWebUIWebControlsDataGridDataGrid; publicintKeyID { get { objecto=ViewState[KeyID]; if(o!=null) { returnintParse(ViewState[KeyID]ToString()); } else { return; } } set { ViewState[KeyID]=value; } } publicintRowState { get {returnintParse(ViewState[RowState]ToString()); } set { ViewState[RowState]=value; } } privatevoidPage_Load(objectsenderSystemEventArgse) { if(IsPostBack) {return; } getData(); } privatevoidgetData() { //SqlConnectioncon=newSqlConnection(ConfigurationSettingsAppSettings[DsnPubs]); SqlConnectioncon=newSqlConnection(SystemConfigurationConfigurationSettingsAppSettings[Mblog]); SqlCommandcmd; conOpen(); cmd=newSqlCommand(select*fromdboHonoreecon); DataGridDataSource=cmdExecuteReader(); DataGridDataBind(); conClose(); } privateboolUpdateData(intIDintOldState) { SqlConnectioncon=newSqlConnection(SystemConfigurationConfigurationSettingsAppSettings[Mblog]); SqlCommandcmd; conOpen(); try { stringstrSql=UpdateHonoreesetStatus={}whereHonoreeID={}; strSql=stringFormat(strSql(OldState==?:)ToString()IDToString()); cmd=newSqlCommand(strSqlcon); cmdExecuteNonQuery(); cmdDispose(); returntrue; } catch { returnfalse; } finally { conClose(); conDispose(); } returnfalse; } Web窗体设计器生成的代码#regionWeb窗体设计器生成的代码 overrideprotectedvoidOnInit(EventArgse) { // //CODEGEN:该调用是ASPNETWeb窗体设计器所必需的 // InitializeComponent(); baseOnInit(e); } /**////<summary> ///设计器支持所需的方法不要使用代码编辑器修改 ///此方法的内容 ///</summary> privatevoidInitializeComponent() { thisDataGridItemCreated+=newSystemWebUIWebControlsDataGridItemEventHandler(thisDataGrid_ItemCreated); thisDataGridItemDataBound+=newSystemWebUIWebControlsDataGridItemEventHandler(thisDataGrid_ItemDataBound); thisLoad+=newSystemEventHandler(thisPage_Load); } #endregion privatevoidDataGrid_ItemCreated(objectsenderSystemWebUIWebControlsDataGridItemEventArgse) { if(eItemItemType==ListItemTypeItem||eItemItemType==ListItemTypeAlternatingItem) { Buttonb=(Button)eItemFindControl(changeState); if(b!=null) { bClick+=newEventHandler(b_Click); } } } privatevoidDataGrid_ItemDataBound(objectsenderSystemWebUIWebControlsDataGridItemEventArgse) { } privatevoidb_Click(objectsenderEventArgse) { Buttonbut=(Button)sender; DataGriddg=(DataGrid)butNamingContainerNamingContainer; //此处是关键!!即找到包含按钮的命名容器的上层命名容器 if(dg==null)return; DataGridItemdi=(DataGridItem)butNamingContainer; TableCellkey=(TableCell)diCells[]; TableCellstate=(TableCell)diCells[]; KeyID=(key==null)?:intParse(keyText); RowState=(state==null)?:intParse(stateText); ResponseWrite(UpdateData(thisKeyIDthisRowState)ToString()); getData(); } |