"Steven Cheng[MSFT]" <v-schang@online.microsoft.com> wrote in message
news:9Zs5yWvyEHA.3984@cpmsftngxa10.phx.gbl...
> Hi Scott,
>
> Thanks for your posting. I've also noticed your another thread in this
> group. As I've mentioned, if you want to attache event handler for the sub
> controls nested in the datagrid's TemplateColumn. We have following means:
>
> Add the "OnXXXX" = ... in the aspx page template or prgramly add the
> event handler in code behind, to make it more clearly, here is a simple
> demo page , you can have a look to see whether it helps. If you have any
> further questions, please feel free to pos there. Thanks.
>
> ===================aspx =============
> <HTML>
> <HEAD>
> <title>codedemo</title>
> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
> <meta content="C#" name="CODE_LANGUAGE">
> <meta content="JavaScript" name="vs_defaultClientScript">
> <meta content="
http://schemas.microsoft.com/intellisense/ie5" > name="vs_targetSchema">
> </HEAD>
> <body>
> <form id="Form1" method="post" runat="server">
> <table width="100%" align="center">
> <tr>
> <td><asp:datagrid id="dgMain" runat="server"
> AutoGenerateColumns="False">
> <Columns>
> <asp:TemplateColumn HeaderText="Column Header">
> <ItemTemplate>
> <asp:CheckBox id="chkStatic" Runat="server" AutoPostBack="True"
> Text="Static EventHandler"
> OnCheckedChanged="chkStatic_CheckedChanged"></asp:CheckBox>
> <asp:CheckBox id="chkDynamic" Runat="server" AutoPostBack="True"
> Text="Dynamic EventHandler" ></asp:CheckBox>
> </ItemTemplate>
> </asp:TemplateColumn>
> </Columns>
> </asp:datagrid></td>
> </tr>
> </table>
> </form>
> </body>
> </HTML>
>
> =========code behind==================
>
> public class codedemo : System.Web.UI.Page
> {
> protected System.Web.UI.WebControls.Label lblTitle;
> protected System.Web.UI.WebControls.Button Button1;
> protected System.Web.UI.WebControls.DataGrid dgMain;
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> if(!IsPostBack)
> {
> string[] items = {"one","two","three","four","five","six"};
>
> dgMain.DataSource= items;
> dgMain.DataBind();
> }
> }
>
> #region Web Form Designer generated code
> override protected void OnInit(EventArgs e)
> {
>
> InitializeComponent();
> base.OnInit(e);
> }
>
> private void InitializeComponent()
> {
> this.dgMain.ItemCreated += new
> System.Web.UI.WebControls.DataGridItemEventHandler(this.dgMain_ItemCreated);
> this.Load += new System.EventHandler(this.Page_Load);
>
> }
> #endregion
>
>
>
>
>
> protected void chkStatic_CheckedChanged(object sender, System.EventArgs e)
> {
> CheckBox chk = sender as CheckBox;
> DataGridItem item = chk.NamingContainer as DataGridItem;
>
> Response.Write("<br>chkStatic in GridItem " + item.ItemIndex + " is
> changed!");
> }
>
> protected void chkDynamic_CheckedChanged(object sender, System.EventArgs
> e)
> {
> CheckBox chk = sender as CheckBox;
> DataGridItem item = chk.NamingContainer as DataGridItem;
>
> Response.Write("<br>chkDynamic in GridItem " + item.ItemIndex + " is
> changed!");
> }
>
> private void dgMain_ItemCreated(object sender,
> System.Web.UI.WebControls.DataGridItemEventArgs e)
> {
> if(e.Item.ItemType== ListItemType.Item || e.Item.ItemType ==
> ListItemType.AlternatingItem)
> {
> CheckBox chk = e.Item.Cells[0].FindControl("chkDynamic") as CheckBox;
> chk.CheckedChanged +=new EventHandler(chkDynamic_CheckedChanged);
> }
> }
> }
> =========================
>
> Regards,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure!
www.microsoft.com/security > (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>