By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

Checkbox postback in DataGrid

P: n/a
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.
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

--
Scott
http://www.OdeToCode.com

On Mon, 9 Aug 2004 09:53:49 -0500, "TPS" <tp*@tps.com> wrote:
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.


Nov 18 '05 #2

P: n/a
TPS
Hey Scott,

Thanks for your reply.

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

Thanks.

TPS.
Nov 18 '05 #3

P: n/a
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
Nov 18 '05 #4

P: n/a
TPS
Hey Scott, thanks for that tip. It helped.

TPS.

--
Nov 18 '05 #5

P: n/a
TPS
Stephen,

Thank you for your comments and your CHECKBOX example was very helpful.

TPS
Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.