DataGrid中想實現這樣的效果
根據某一字段列的值動態改變按鈕的文本比如
點擊按鈕列自動更新某列原為的值為並將按鈕列的文本改為置
再按下自動更新某列原為的值為並將按鈕列的文本改為置
最終通過NamingContainer實現! 方法如下


<asp:DataGrid id=
DataGrid
runat=
server
AutoGenerateColumns=
False
>

<Columns>

<asp:BoundColumn DataField=
HonoreeID
HeaderText=
ID
></asp:BoundColumn>

<asp:BoundColumn DataField=
status
HeaderText=
狀態
>

<HeaderStyle Width=
px
></HeaderStyle>

</asp:BoundColumn>

<asp:TemplateColumn HeaderText=
狀態是否為
>

<HeaderStyle HorizontalAlign=
Center
Width=
%
></HeaderStyle>

<ItemStyle HorizontalAlign=
Center
></ItemStyle>

<ItemTemplate>

<asp:Label id=
lb
runat=
server
Visible=
false
Text=
<%# ((DataBinder
Eval(Container
DataItem
status
{
}
))==
)?
是
:
<font color=red>否</font>
%>
>

</asp:Label>

<asp:Button ID=
changeState
Runat=
server
Text=
<%# ((DataBinder
Eval(Container
DataItem
status
{
}
))==
)?
轉為
:
轉為
%>
>

</asp:Button>

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:DataGrid>

後台


protected System
Web
UI
WebControls
DataGrid DataGrid
;

public int KeyID

{

get

{

object o =ViewState [
KeyID
];

if(o!=null)

{

return int
Parse(ViewState [
KeyID
]
ToString());

}

else

{

return
;

}

}

set

{

ViewState [
KeyID
] = value;

}

}

public int RowState

{

get

{ return int
Parse(ViewState [
RowState
]
ToString());

}

set

{

ViewState [
RowState
] = value;

}

}

private void Page_Load(object sender
System
EventArgs e)

{

if(IsPostBack)

{return ;

}

getData();

}



private void getData()

{

//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);

DataGrid
DataSource = cmd
ExecuteReader();

DataGrid
DataBind();

con
Close();

}


private bool UpdateData(int ID
int OldState)

{

SqlConnection con = new SqlConnection(System
Configuration
ConfigurationSettings
AppSettings[
Mblog
]);

SqlCommand cmd;

con
Open();

try

{

string strSql=
Update Honoree set Status={
} where HonoreeID={
}
;

strSql=string
Format(strSql
(OldState==
?
:
)
ToString()
ID
ToString());

cmd = new SqlCommand(strSql
con);

cmd
ExecuteNonQuery();

cmd
Dispose();

return true;

}

catch

{

return false;

}

finally

{

con
Close();

con
Dispose();

}

return false;

}




Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 該調用是 ASP
NET Web 窗體設計器所必需的

//

InitializeComponent();

base
OnInit(e);

}


/**//// <summary>

/// 設計器支持所需的方法
不要使用代碼編輯器修改

/// 此方法的內容

/// </summary>

private void InitializeComponent()

{

this
DataGrid
ItemCreated += new System
Web
UI
WebControls
DataGridItemEventHandler(this
DataGrid
_ItemCreated);

this
DataGrid
ItemDataBound += new System
Web
UI
WebControls
DataGridItemEventHandler(this
DataGrid
_ItemDataBound);

this
Load += new System
EventHandler(this
Page_Load);


}

#endregion


private void DataGrid
_ItemCreated(object sender
System
Web
UI
WebControls
DataGridItemEventArgs e)

{

if(e
Item
ItemType==ListItemType
Item || e
Item
ItemType == ListItemType
AlternatingItem)

{

Button b=(Button)e
Item
FindControl(
changeState
);

if(b!=null)

{

b
Click+=new EventHandler(b_Click);

}

}

}


private void DataGrid
_ItemDataBound(object sender
System
Web
UI
WebControls
DataGridItemEventArgs e)

{

}


private void b_Click(object sender
EventArgs e)

{

Button but = (Button)sender;

DataGrid dg = (DataGrid)but
NamingContainer
NamingContainer;

//此處是關鍵!!即找到包含按鈕的命名容器的上層命名容器

if(dg == null) return;

DataGridItem di =(DataGridItem)but
NamingContainer;

TableCell key= (TableCell)di
Cells[
];

TableCell state= (TableCell)di
Cells[
];


KeyID=(key==null)?
:int
Parse(key
Text);

RowState=(state==null)?
:int
Parse(state
Text);

Response
Write(UpdateData(this
KeyID
this
RowState)
ToString());

getData();

}
From:http://tw.wingwit.com/Article/program/net/201311/13037.html