My application has a checkbox control as part of a template column. I need this checkbox to respond to a user click by getting the key field of the row it is in, inserting it into a sql statement and executing it. How can I get the value of
the key field associated with the row the user clicked on? And how can I get the index of that row?
// The CheckBox event handler.
// Note the protected declaration.
protected void gridCheckBox_OnCheckedChanged(object sender, System.EventArgs e)
{
string textValue = ((CheckBox)sender).Text;
string yourKeyField = ((CheckBox)sender).Attributes["YourKeyField"];
int rowNumber = Convert.ToInt32(((CheckBox)sender).Attributes["RowNumber"]);
}
// The ItemDataBound event of the DataGrid.
// NOTE: Make sure that this event handler is attached to the DataGrid.
private void yourDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ( e.Item.ItemIndex >= 0 )
{
CheckBox gridCheckBox = (CheckBox)e.Item.FindControl("gridCheckBox");
if ( gridCheckBox != null )
{
// Set the CheckBox's Text property to column #1's value...
// Remember that column indexes are 0 based.
gridCheckBox.Text = e.Item.Cells[1].Text;
// Add the key value as an attribute on the CheckBox for later retrieval...
string yourKeyField = // set to some value
gridCheckBox.Attributes.Add("YourKeyField", yourKeyField);
// Add the row number as an attribute on the CheckBox for later retrieval...
gridCheckBox.Attributes.Add("RowNumber", e.Item.ItemIndex.ToString());
}
}
}
Thank you for your response. I guess I should have mentioned that I'm a vb programmer, and although I get the gist of what your C# code is doing, I have not been able to find a way in vb to reference that checkbox inside the template. I have
tried:
Dim chkDone
As CheckBox = e.Row.Cells(1).Controls(0)
This gives me an error: "Implicit Conversion from 'System.Web.UI.Control' to 'System.Web.UI.WebControls.Checkbox'"
If you or some vb programmer can advise me on this, I'd be much obliged.
Jajajaja, i didn't realized this, jajajaja, i just saw the post cuz i was looking for info about the use of checkboxes on a gridview and saw that the guy got some problems so i just replied without realizing this, jajajaja, sorry for bringing up the death
ones, xD
Sheldon
Member
155 Points
31 Posts
Capturing Key Field of Current Row in Gridview
Oct 06, 2005 04:30 AM|LINK
Thanks in advance for any help.
Sheldon
NC01
All-Star
82559 Points
15430 Posts
MVP
Re: Capturing Key Field of Current Row in Gridview
Oct 06, 2005 01:23 PM|LINK
In the aspx file:
The CheckBox DataGrid Template column
<asp:templatecolumn headertext="Nbr" itemstyle-width="50px" itemstyle-wrap="False">
<itemtemplate>
<asp:checkbox id="gridCheckBox" autopostback="True" enableviewstate="True"
runat="server" text=""
oncheckedchanged="gridCheckBox_OnCheckedChanged"></asp:checkbox>
</itemtemplate>
</asp:templatecolumn>
In the CodeBehind file:
// The CheckBox event handler.
// Note the protected declaration.
protected void gridCheckBox_OnCheckedChanged(object sender, System.EventArgs e)
{
string textValue = ((CheckBox)sender).Text;
string yourKeyField = ((CheckBox)sender).Attributes["YourKeyField"];
int rowNumber = Convert.ToInt32(((CheckBox)sender).Attributes["RowNumber"]);
}
// The ItemDataBound event of the DataGrid.
// NOTE: Make sure that this event handler is attached to the DataGrid.
private void yourDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ( e.Item.ItemIndex >= 0 )
{
CheckBox gridCheckBox = (CheckBox)e.Item.FindControl("gridCheckBox");
if ( gridCheckBox != null )
{
// Set the CheckBox's Text property to column #1's value...
// Remember that column indexes are 0 based.
gridCheckBox.Text = e.Item.Cells[1].Text;
// Add the key value as an attribute on the CheckBox for later retrieval...
string yourKeyField = // set to some value
gridCheckBox.Attributes.Add("YourKeyField", yourKeyField);
// Add the row number as an attribute on the CheckBox for later retrieval...
gridCheckBox.Attributes.Add("RowNumber", e.Item.ItemIndex.ToString());
}
}
}
NC...
Sheldon
Member
155 Points
31 Posts
Re: Capturing Key Field of Current Row in Gridview
Oct 07, 2005 12:06 AM|LINK
Dim chkDone As CheckBox = e.Row.Cells(1).Controls(0)
This gives me an error: "Implicit Conversion from 'System.Web.UI.Control' to 'System.Web.UI.WebControls.Checkbox'"
If you or some vb programmer can advise me on this, I'd be much obliged.
Thanks again.
Sheldon
cjLopez
Member
17 Points
34 Posts
Re: Capturing Key Field of Current Row in Gridview
Aug 16, 2007 08:52 PM|LINK
The way to reference the checkbox on the template field is using the ID established for the checkboxes.
I'm sure you have something like this
<asp:CheckBox runat="server" ID="chkBox" AutoPostBack="true" />
There is a ID field which 'll use to reference to the row checkbox and you can look for it like this
If you know on which row the checkbox is you can do this
Dim chekBox As CheckBox = e.Rows("indexNumber As Integer").FindCrontol("chkBox")
This way you say that the chekBox will have the proprieties found on the Row you indicates and has the id of chkBox
But if you don't know in which row is the checkbox you need, just do a little search using a for each
for each row As gridviewrow In e.Rows Dim checkBox As CheckBox = row.FindControl("chkBox") NextHope this helpsdcolemanturner
Member
149 Points
68 Posts
Re: Capturing Key Field of Current Row in Gridview
Aug 16, 2007 09:05 PM|LINK
http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx
use this url when u need to convert any C# code to VB or vice versa.
-Derrick
NC01
All-Star
82559 Points
15430 Posts
MVP
Re: Capturing Key Field of Current Row in Gridview
Aug 17, 2007 03:50 PM|LINK
cjLopez,
You are responding to posts from almost 2 years ago?
NC...
dcolemanturner
Member
149 Points
68 Posts
Re: Capturing Key Field of Current Row in Gridview
Aug 17, 2007 05:19 PM|LINK
hahaha...in Sovet Russia, 2 year post responds to you
cjLopez
Member
17 Points
34 Posts
Re: Capturing Key Field of Current Row in Gridview
Aug 17, 2007 05:38 PM|LINK
Jajajaja, i didn't realized this, jajajaja, i just saw the post cuz i was looking for info about the use of checkboxes on a gridview and saw that the guy got some problems so i just replied without realizing this, jajajaja, sorry for bringing up the death ones, xD