DataGrid中想实现这样的效果:
根据某一字段列的值动态改变按钮的文本,比如:
点击按钮列,自动更新某列原为0的值为1,并将按钮列的文本改为“置0”,
再按下,自动更新某列原为1的值为0,并将按钮列的文本改为“置1”,
最终通过NamingContainer,实现! 方法如下 :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<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>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
|
后台:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public int KeyID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) ...{
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
object o =ViewState ["KeyID"];
if(o!=null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
return int.Parse(ViewState ["KeyID"].ToString());
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
return 0;
}
}
set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
ViewState ["KeyID"] = value;
}
}
public int RowState
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) ...{
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{ return int.Parse(ViewState ["RowState"].ToString());
}
set
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
ViewState ["RowState"] = value;
}
}
private void Page_Load(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) ...{
if(IsPostBack)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{return ;
}
getData();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void getData()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) ...{
//SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["DsnPubs"]);
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Mblog"]);
SqlCommand cmd;
con.Open();
cmd = new SqlCommand("select * from dbo.Honoree", con);
DataGrid1.DataSource = cmd.ExecuteReader();
DataGrid1.DataBind();
con.Close();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private bool UpdateData(int ID,int OldState)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) ...{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Mblog"]);
SqlCommand cmd;
con.Open();
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
string strSql="Update Honoree set Status={0} where HonoreeID={1}";
strSql=string.Format(strSql,(OldState==0?1:0).ToString(),ID.ToString());
cmd = new SqlCommand(strSql, con);
cmd.ExecuteNonQuery();
cmd.Dispose();
return true;
}
catch
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
return false;
}
finally
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
con.Close();
con.Dispose();
}
return false;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) ...{
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
Button b=(Button)e.Item.FindControl("changeState");
if(b!=null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
b.Click+=new EventHandler(b_Click);
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) ...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void b_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) ...{
Button but = (Button)sender;
DataGrid dg = (DataGrid)but.NamingContainer.NamingContainer;
//此处是关键!!即找到包含按钮的命名容器的上层命名容器
if(dg == null) return;
DataGridItem di =(DataGridItem)but.NamingContainer;
TableCell key= (TableCell)di.Cells[0];
TableCell state= (TableCell)di.Cells[1];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
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();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
|
|