asp checkbox checking for OnCheckedChanged value

Discussion in 'ASP .Net' started by Maziar Aflatoun, Sep 5, 2004.

  1. 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
     
    Maziar Aflatoun, Sep 5, 2004
    #1
    1. Advertising

  2. Maziar Aflatoun

    Teemu Keiski Guest

    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" <> wrote in message
    news:27y_c.144864$...
    > 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
    >
    >
     
    Teemu Keiski, Sep 5, 2004
    #2
    1. Advertising

  3. Maziar Aflatoun

    Scott Allen Guest

    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" <>
    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
    >
     
    Scott Allen, Sep 5, 2004
    #3
  4. Maziar Aflatoun

    radu_m

    Joined:
    Dec 29, 2008
    Messages:
    2
    checkbox control

    I had a similar problem where I was trying to find out the row where the check box was clicked, then extract the "ID" -primary key value. What I did was the following. Inside the GridView I inserted a hyperlink template that used the <%# Eval("ID")%> function to assign its value to Text property.
    After that in the handler I searched for this using FindControl() method.

    So this is what I had inside the .aspx among the gridview columns:

    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:HyperLink ID="LinkButtonID" runat="server" Text='<%# Eval("ID") %>' Visible="false" />
    </ItemTemplate>
    </asp:TemplateField>
    ...........
    <asp:TemplateField HeaderText="Activated">
    <ItemTemplate>
    <asp:CheckBox ID="activatedcheckbox" runat="server" Checked='<%#Bind("Activated") %>'
    OnCheckedChanged="UpdateActivationStatus" AutoPostBack="True" />
    </ItemTemplate>
    </asp:TemplateField>
    ...........
    </Columns>

    Now remember that I was using Linq as a data source to provide the table columns to the gridview, one of them being the column "ID" in on of the db table.
    You can use any other method of providing the SQL to the Gridview.

    Here is the code behind:

    Public Sub UpdateActivationStatus(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim db As New CarGalleryDataContext
    Dim chkbox As CheckBox = CType(sender, CheckBox)

    Dim tblcell As TableCell = CType(chkbox.Parent, TableCell)

    Dim dgRow As GridViewRow = CType(tblcell.Parent, GridViewRow)

    'find the control that holds the ID value and cast it 'back to HyperLink type
    Dim lnkHyperLink = CType(dgRow.FindControl("LinkButtonID"), HyperLink)

    Dim listingID = lnkHyperLink.Text 'retreive listing ID

    Dim listingauto As UserVehicle = (From l In db.UserVehicles _
    Where l.ID.ToString = listingID _
    Select l).Single

    listingauto.Activated = chkbox.Checked

    db.SubmitChanges() 'submit changes to db


    End If

    End Sub

    This worked fine for me ,:captain:
    Happy Coding !!!:stupido:
    Radu_M
     
    Last edited: Dec 29, 2008
    radu_m, Dec 29, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Homa
    Replies:
    3
    Views:
    14,911
    nchavan
    Jul 23, 2009
  2. Danielle
    Replies:
    1
    Views:
    2,800
    Munna
    Jun 8, 2008
  3. arby

    Checkbox in DataGrid Not Firing OnCheckedChanged events.

    arby, Jan 30, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    527
    Alvin Bruney [MVP]
    Jan 31, 2004
  4. Chris Heinrich

    OnCheckedChanged won't fire when turning CheckBox OFF

    Chris Heinrich, Jul 2, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    265
    Chris Heinrich
    Jul 2, 2003
  5. Hitesh Jain

    oncheckedchanged event is not firing everytime on checkbox

    Hitesh Jain, Oct 14, 2004, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    1,530
    Robert Koritnik
    Oct 15, 2004
Loading...

Share This Page