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

asp checkbox checking for OnCheckedChanged value

P: n/a
Hi everyone,

I am reading and displaying data rows from my database where the first
column contains the Status checkbox. I like to enable my users to change
the status of individual rows by checking and unchecking the checkbox
column. I like to update the status in the database for the column where
the status was changed.

This is what I'm doing so far

<asp:datagrid id="MyDataGrid" runat="server" CellPadding="2"
AutoGenerateColumns="false" HeaderStyle-CssClass="maintableheader"
Width="95%">
<Columns>
<asp:BoundColumn DataField="BID" ReadOnly="True"
Visible="False"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Active" Visible="True"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="Status" Runat="server" Checked='<%#
FormatStatus(DataBinder.Eval(Container.DataItem, "Status")) %>'
AutoPostBack=True OnCheckedChanged="UpdateCheckboxStatus">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
....etc.

and in my .cs page

public void UpdateCheckboxStatus(object sender, System.EventArgs e)
{
Response.Write ("You click the row...")...
}

How can I find out which row was clicked? Please let me know if there is a
better way to do this.

Thank you
Maz
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

you get to the CheckBox's parent DataGridItem by utilizing the hierarchical
idea of Controls collection, meaning that get it via your Control's Parent
property.

public void UpdateCheckboxStatus(object sender, System.EventArgs e)
{
//Written into several lines to clarify, you could go with one line

//Current checkbox
CheckBox box=(CheckBox)sender;

//The TableCell the control is in
TableCell cell=(TableCell)box.Parent;

//The DataGridItem the cell belongs to
DataGridItem dgItem=(DataGridItem)cell.Parent;
}

With that DataGridItem you get for example ItemIndex of the DataGridItem you
are into. It is the same would be e.Item in ItemCommand,ItemDataBound or
ItemCreated. And you can then again get the PK of the current row and so on
via DataKeys collection.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:27*********************@news01.bloor.is.net.c able.rogers.com...
Hi everyone,

I am reading and displaying data rows from my database where the first
column contains the Status checkbox. I like to enable my users to change
the status of individual rows by checking and unchecking the checkbox
column. I like to update the status in the database for the column where
the status was changed.

This is what I'm doing so far

<asp:datagrid id="MyDataGrid" runat="server" CellPadding="2"
AutoGenerateColumns="false" HeaderStyle-CssClass="maintableheader"
Width="95%">
<Columns>
<asp:BoundColumn DataField="BID" ReadOnly="True"
Visible="False"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Active" Visible="True"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="Status" Runat="server" Checked='<%#
FormatStatus(DataBinder.Eval(Container.DataItem, "Status")) %>'
AutoPostBack=True OnCheckedChanged="UpdateCheckboxStatus">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
...etc.

and in my .cs page

public void UpdateCheckboxStatus(object sender, System.EventArgs e)
{
Response.Write ("You click the row...")...
}

How can I find out which row was clicked? Please let me know if there is a
better way to do this.

Thank you
Maz

Nov 18 '05 #2

P: n/a
Teemu gave a great answer. For some more sample code see my article
here:

http://odetocode.com/Articles/116.aspx

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

On Sun, 05 Sep 2004 06:12:14 GMT, "Maziar Aflatoun" <ma***@rogers.com>
wrote:
Hi everyone,

I am reading and displaying data rows from my database where the first
column contains the Status checkbox. I like to enable my users to change
the status of individual rows by checking and unchecking the checkbox
column. I like to update the status in the database for the column where
the status was changed.

This is what I'm doing so far

<asp:datagrid id="MyDataGrid" runat="server" CellPadding="2"
AutoGenerateColumns="false" HeaderStyle-CssClass="maintableheader"
Width="95%">
<Columns>
<asp:BoundColumn DataField="BID" ReadOnly="True"
Visible="False"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Active" Visible="True"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="Status" Runat="server" Checked='<%#
FormatStatus(DataBinder.Eval(Container.DataItem , "Status")) %>'
AutoPostBack=True OnCheckedChanged="UpdateCheckboxStatus">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
...etc.

and in my .cs page

public void UpdateCheckboxStatus(object sender, System.EventArgs e)
{
Response.Write ("You click the row...")...
}

How can I find out which row was clicked? Please let me know if there is a
better way to do this.

Thank you
Maz


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.