DataGrid中的按钮反选事件与NamingContainer(命名容器)-downmoon

DataGrid中想实现这样的效果:
根据某一字段列的值动态改变按钮的文本,比如:
点击按钮列,自动更新某列原为0的值为1,并将按钮列的文本改为“置0”,
再按下,自动更新某列原为1的值为0,并将按钮列的文本改为“置1”,

最终通过NamingContainer,实现! 方法如下 :

<!-- .lineBorderBlue1 { BORDER-TOP: #999999 1px groove; BORDER-BOTTOM: #999999 1px groove; BORDER-LEFT: #999999 1px groove; BORDER-RIGHT: #999999 1px groove; background-color:#efefef; } -->


< asp:DataGrid id ="DataGrid1" runat ="server" AutoGenerateColumns ="False" >
< Columns >
< asp:BoundColumn DataField ="HonoreeID" HeaderText ="ID" ></ asp:BoundColumn >
< asp:BoundColumn DataField ="status" HeaderText ="状态" >
< HeaderStyle Width ="300px" ></ HeaderStyle >
</ asp:BoundColumn >
< asp:TemplateColumn HeaderText ="状态是否为0" >
< HeaderStyle HorizontalAlign ="Center" Width ="10%" ></ HeaderStyle >
< ItemStyle HorizontalAlign ="Center" ></ ItemStyle >
< ItemTemplate >
< asp:Label id ="lb" runat ="server" Visible ="false" Text ='<%# ((DataBinder.Eval(Container,"DataItem.status","{0}")) =="0")?"是":"<font color =red > </ font > "%>'>
</ asp:Label >
< asp:Button ID ="changeState" Runat ="server" Text ='<%# ((DataBinder.Eval(Container,"DataItem.status","{0}")) =="0")?"转为1":"转为0" % > '>
</ asp:Button >
</ ItemTemplate >
</ asp:TemplateColumn >
</ Columns >
</ asp:DataGrid >



后台:
<!-- .lineBorderBlue1 { BORDER-TOP: #999999 1px groove; BORDER-BOTTOM: #999999 1px groove; BORDER-LEFT: #999999 1px groove; BORDER-RIGHT: #999999 1px groove; background-color:#efefef; } -->


protected System.Web.UI.WebControls.DataGridDataGrid1;

public int KeyID
... {
get
...{
objecto=ViewState["KeyID"];
if(o!=null)
...{
returnint.Parse(ViewState["KeyID"].ToString());
}

else
...{
return0;
}

}

set
...{
ViewState[
"KeyID"]=value;
}

}

public int RowState
... {
get
...{returnint.Parse(ViewState["RowState"].ToString());

}

set
...{
ViewState[
"RowState"]=value;
}

}

private void Page_Load( object sender,System.EventArgse)
... {
if(IsPostBack)
...{return;
}

getData();
}



private void getData()
... {
//SqlConnectioncon=newSqlConnection(ConfigurationSettings.AppSettings["DsnPubs"]);

SqlConnectioncon
=newSqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Mblog"]);
SqlCommandcmd;
con.Open();
cmd
=newSqlCommand("select*fromdbo.Honoree",con);
DataGrid1.DataSource
=cmd.ExecuteReader();
DataGrid1.DataBind();
con.Close();
}


private bool UpdateData( int ID, int OldState)
... {
SqlConnectioncon
=newSqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Mblog"]);
SqlCommandcmd;
con.Open();
try
...{
stringstrSql="UpdateHonoreesetStatus={0}whereHonoreeID={1}";
strSql
=string.Format(strSql,(OldState==0?1:0).ToString(),ID.ToString());
cmd
=newSqlCommand(strSql,con);
cmd.ExecuteNonQuery();
cmd.Dispose();
returntrue;
}

catch
...{
returnfalse;
}

finally
...{
con.Close();
con.Dispose();

}

returnfalse;
}



Web窗体设计器生成的代码 #regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
...{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}


/**////<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>

privatevoidInitializeComponent()
...{
this.DataGrid1.ItemCreated+=newSystem.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
this.DataGrid1.ItemDataBound+=newSystem.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load+=newSystem.EventHandler(this.Page_Load);

}

#endregion


private void DataGrid1_ItemCreated( object sender,System.Web.UI.WebControls.DataGridItemEventArgse)
... {
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
...{
Buttonb
=(Button)e.Item.FindControl("changeState");
if(b!=null)
...{

b.Click
+=newEventHandler(b_Click);
}

}


}


private void DataGrid1_ItemDataBound( object sender,System.Web.UI.WebControls.DataGridItemEventArgse)
... {

}


private void b_Click( object sender,EventArgse)
... {
Buttonbut
=(Button)sender;
DataGriddg
=(DataGrid)but.NamingContainer.NamingContainer;
//此处是关键!!即找到包含按钮的命名容器的上层命名容器
if(dg==null)return;
DataGridItemdi
=(DataGridItem)but.NamingContainer;
TableCellkey
=(TableCell)di.Cells[0];
TableCellstate
=(TableCell)di.Cells[1];

KeyID
=(key==null)?0:int.Parse(key.Text);
RowState
=(state==null)?0:int.Parse(state.Text);
Response.Write(UpdateData(
this.KeyID,this.RowState).ToString());

getData();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值