Checkbox postback in DataGrid

T

TPS

I have a checkbox column in my datagrid. This checkbox triggers an
autopostback.

When I postback, I need to tell what row the user is on so I can tell what
check box was checked or unchecked.

Can't seem to find that property.

Thanks.

TPS.
 
S

Scott Allen

Hi TPS:

In the event handler, the sender parameter will represent the checkbox
that initiated the event. By inspecting the Parent property you can
eventually get to the DataGridItem that represents the row you are in.
DataGridItem has an ItemIndex property if you need to index into a
DataSource of some sort.

My article might shed some light on the hierarchy:
http://odetocode.com/Articles/116.aspx
 
T

TPS

Hey Scott,

Thanks for your reply.

I will inspect the sender. And check out your article.

Thanks.

TPS.
 
S

Steven Cheng[MSFT]

Hi TPS,

Yes, as Scott has mentioned, you can get the reference to the checkbox in
its postback event and use the Parent property to upper referece its parent
and parent control which is DataGridItem. Here is a simple test page, you
can have a look:

===============aspx================
<HTML>
<HEAD>
<title>CheckGrid</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:DataGrid id="dgCheck" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="index"
HeaderText="Index"></asp:BoundColumn>
<asp:BoundColumn DataField="name"
HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Selected">
<ItemTemplate>
<asp:CheckBox ID="chkSelected" Runat="server" AutoPostBack="True"
OnCheckedChanged="chkSelected_CheckedChanged" Checked='<%#
((System.Data.DataRowView)Container.DataItem)["selected"] %>' >
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
==============code behind========
public class CheckGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgCheck;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind_Data();
}
}

protected void Bind_Data()
{
DataTable tb = new DataTable();
tb.Columns.Add("index",typeof(int));
tb.Columns.Add("name",typeof(string));
tb.Columns.Add("selected",typeof(bool));

DataRow row = null;
for(int i=1;i<=15;i++)
{
row = tb.NewRow();
row["index"] = i;
row["name"] = "Name_" + i;

if(i%3==0)
{
row["selected"] = true;
}
else
{
row["selected"] = false;
}

tb.Rows.Add(row);

}

dgCheck.DataSource = tb;
dgCheck.DataBind();


}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

protected void chkSelected_CheckedChanged(object sender, System.EventArgs
e)
{
CheckBox chk = (CheckBox)sender;

DataGridItem dgi = (DataGridItem)chk.Parent.Parent;
Response.Write("<br>" + dgi.ItemIndex);

}
}
=============================

Thanks.

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.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top