DataGrid中想實現這樣的效果
根據某一字段列的值動態改變按鈕的文本
點擊按鈕列
再按下
最終通過NamingContainer
<asp:DataGrid id=
<Columns>
<asp:BoundColumn DataField=
<asp:BoundColumn DataField=
<HeaderStyle Width=
</asp:BoundColumn>
<asp:TemplateColumn HeaderText=
<HeaderStyle HorizontalAlign=
<ItemStyle HorizontalAlign=
<ItemTemplate>
<asp:Label id=
</asp:Label>
<asp:Button ID=
</asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
後台
protected System
public int KeyID
get
object o =ViewState [
if(o!=null)
return int
}
else
return
}
}
set
ViewState [
}
}
public int RowState
get
}
set
ViewState [
}
}
private void Page_Load(object sender
if(IsPostBack)
}
getData();
}
private void getData()
//SqlConnection con = new SqlConnection(ConfigurationSettings
SqlConnection con = new SqlConnection(System
SqlCommand cmd;
con
cmd = new SqlCommand(
DataGrid
DataGrid
con
}
private bool UpdateData(int ID
SqlConnection con = new SqlConnection(System
SqlCommand cmd;
con
try
string strSql=
strSql=string
cmd = new SqlCommand(strSql
cmd
cmd
return true;
}
catch
return false;
}
finally
con
con
}
return false;
}
Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
//
// CODEGEN: 該調用是 ASP
//
InitializeComponent();
base
}
/**//// <summary>
/// 設計器支持所需的方法
/// 此方法的內容
/// </summary>
private void InitializeComponent()
this
this
this
}
#endregion
private void DataGrid
if(e
Button b=(Button)e
if(b!=null)
b
}
}
}
private void DataGrid
}
private void b_Click(object sender
Button but = (Button)sender;
DataGrid dg = (DataGrid)but
//此處是關鍵!!即找到包含按鈕的命名容器的上層命名容器
if(dg == null) return;
DataGridItem di =(DataGridItem)but
TableCell key= (TableCell)di
TableCell state= (TableCell)di
KeyID=(key==null)?
RowState=(state==null)?
Response
getData();
}
From:http://tw.wingwit.com/Article/program/net/201311/13037.html