`
netxdiy
  • 浏览: 683251 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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:DataGridid="DataGrid1"runat="server"AutoGenerateColumns="False">
<Columns>
<asp:BoundColumnDataField="HonoreeID"HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumnDataField="status"HeaderText="状态">
<HeaderStyleWidth="300px"></HeaderStyle>
</asp:BoundColumn>
<asp:TemplateColumnHeaderText="状态是否为0">
<HeaderStyleHorizontalAlign="Center"Width="10%"></HeaderStyle>
<ItemStyleHorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Labelid="lb"runat="server"Visible="false"Text='<%#((DataBinder.Eval(Container,"DataItem.status","{0}"))=="0")?"是":"<fontcolor=red></font>"%>'>
</asp:Label>
<asp:ButtonID="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; } -->


protectedSystem.Web.UI.WebControls.DataGridDataGrid1;

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

else
...{
return0;
}

}

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

}

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

}

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

}

privatevoidPage_Load(objectsender,System.EventArgse)
...{
if(IsPostBack)
...{return;
}

getData();
}



privatevoidgetData()
...{
//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();
}


privateboolUpdateData(intID,intOldState)
...{
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


privatevoidDataGrid1_ItemCreated(objectsender,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);
}

}


}


privatevoidDataGrid1_ItemDataBound(objectsender,System.Web.UI.WebControls.DataGridItemEventArgse)
...{

}


privatevoidb_Click(objectsender,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();
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics