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
 
Joined
Feb 10, 2010
Messages
12
Reaction score
0
Checkbox Column In Datagrid

When we add a customized checkbox column to a datagrid in .net (windows application) , the default property allows to check or uncheck the column using a double click. On the first click it selects the column and on the second click the column is either checked or unchecked.

To change this default property, we need to handle the click event on grid and modify the selected cell value. Here is the sample code to achieve this.

[C#.NET VS 2003 , Code to add checkbox column to grid using table style property]

dgItemDetails.TableStyles.Clear(); // clears the tablestyle (dgItemDetails is the name of grid)
DataGridTableStyle dgt = new DataGridTableStyle();
dgt.MappingName = "ItemDetails";

DataGridBoolColumn dgbCol = new DataGridBoolColumn(); // creates the checkbox column
dgbCol.MappingName = "Select";
dgbCol.HeaderText = "";
dgbCol.Width = 40;
dgbCol.AllowNull = false;
dgbCol.Alignment = HorizontalAlignment.Left;
dgt.GridColumnStyles.Add(dgbCol);

dgItemDetails.TableStyles.Add(dgt); // add
 
Joined
Jun 2, 2011
Messages
2
Reaction score
0
Trying to find a Checkbox within a Datagrid ....

I have read through this post and it seems very helpful, the problem is that I am still doing something wrong because it is not working.

I have a template with a checkbox within a datagrid, so each row of data has 1 checkbox. I need to find the checkbox that is checked/unchecked on the postback.

I have this in my .aspx page:
<asp:TemplateColumn HeaderText="Archived" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkArchived" runat="server" CommandName="Archive"
CommandArgument='<%# Eval("QuestionId") %>' AutoPostBack="true" OnCheckedChanged="chkArchived_CheckedChanged" />
</ItemTemplate>
</asp:TemplateColumn>

Which I believe is working alright, but the VB page is not working for me. I created a CheckedChanged routine as in the example:
Protected Sub chkArchived_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
'This is where I am not able to find what row I am on to save the changes to the db.
End Sub

The article posted states about finding the control through the datagrid selection change, but I only got compile errors when I tried to rewrite the code into VB.

Any ideas?
 
Joined
Jun 2, 2011
Messages
2
Reaction score
0
This is what worked for me ....

This is the VB code that ended up making the checkboxes in my datagrid saveable on a postback, one at a time.

Protected Sub chkArchived_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim c As Control = CType(sender, Control)
Dim item As DataGridItem = CType(c.Parent.Parent, DataGridItem)
Dim id As Integer = CType(dgQuestions.DataKeys.Item(item.ItemIndex), Integer)
Dim question As BOBJ.SurveyQuestionObj = CType(BLL.SessionData.SurveyQuestions.ItemById(id), BOBJ.SurveyQuestionObj)

question.Archived = True
BLL.Processes.Administrative.UpdateSurveyQuestion(question)
End Sub
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top