how to count number of unchecked checkboxes in a gridview?

Discussion in 'ASP .Net' started by Dan, May 25, 2009.

  1. Dan

    Dan Guest

    Hi,

    in this gridview, there is in each row a checkbox linked to a field in the
    database. Some are checked, other not. I want to know how many are (e.g.)
    unchecked when the gridview is loaded.
    This is my attempt (vb.net), but it doesn't work.
    Thanks for help.
    Dan.

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    GridView1.RowDataBound
    dim x as integer = 0
    If e.Row.RowType = DataControlRowType.DataRow Then
    if e.Row.Cells(2).Text = "false" then x=x+1
    response.write("number of unchecked checkbox:" & x)
    end if
    end Sub

    asp:GridView ID="GridView1" runat="server" />
    <Columns>
    <asp:CommandField ShowSelectButton="true" />
    <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" />
    <asp:TemplateField HeaderText="selected" >
    <ItemTemplate>
    <asp:CheckBox ID="chb" runat="server" Checked='<%# Bind("selct") %>' >
    </asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
     
    Dan, May 25, 2009
    #1
    1. Advertising

  2. On May 25, 6:47 pm, "Dan" <> wrote:
    > Hi,
    >
    > in this gridview, there is in each row a checkbox linked to a field in the
    > database. Some are checked, other not. I want to know how many are (e.g.)
    > unchecked when the gridview is loaded.
    > This is my attempt (vb.net), but it doesn't work.
    > Thanks for help.
    > Dan.
    >
    > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > GridView1.RowDataBound
    > dim x as integer = 0
    > If e.Row.RowType = DataControlRowType.DataRow Then
    > if e.Row.Cells(2).Text = "false" then x=x+1
    > response.write("number of unchecked checkbox:" & x)
    > end if
    > end Sub
    >
    > asp:GridView ID="GridView1" runat="server" />
    > <Columns>
    > <asp:CommandField  ShowSelectButton="true" />
    > <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" />
    > <asp:TemplateField HeaderText="selected" >
    > <ItemTemplate>
    > <asp:CheckBox ID="chb" runat="server" Checked='<%# Bind("selct") %>' >
    > </asp:CheckBox>
    > </ItemTemplate>
    > </asp:TemplateField>
    > </Columns>
    > </asp:GridView>


    I am not sure but it seems if you don't set the Text property to chb,
    you can't get it. Use following

    If e.Row.RowType = DataControlRowType.DataRow Then
    if e.Row.DataItem("selct") = "false" then x=x+1

    This should work
     
    Alexey Smirnov, May 25, 2009
    #2
    1. Advertising

  3. Dan

    Mr. Arnold Guest

    "Dan" <> wrote in message
    news:%...
    > Hi,
    >
    > in this gridview, there is in each row a checkbox linked to a field in the
    > database. Some are checked, other not. I want to know how many are (e.g.)
    > unchecked when the gridview is loaded.
    > This is my attempt (vb.net), but it doesn't work.
    > Thanks for help.
    > Dan.
    >
    > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > GridView1.RowDataBound
    > dim x as integer = 0
    > If e.Row.RowType = DataControlRowType.DataRow Then
    > if e.Row.Cells(2).Text = "false" then x=x+1
    > response.write("number of unchecked checkbox:" & x)
    > end if
    > end Sub



    Maybe, you need to try something different by using the example in the link
    and using the second example of searching the grid, which I don't see why
    you couldn't tweak the code to come back with a count of unchecked data data
    in a column. The routine could be executed right after the load of the grid.

    http://www.automatedqa.com/communit...leType/ArticleView/articleId/502/Default.aspx


    __________ Information from ESET NOD32 Antivirus, version of virus signature database 4101 (20090525) __________

    The message was checked by ESET NOD32 Antivirus.

    http://www.eset.com
     
    Mr. Arnold, May 25, 2009
    #3
  4. Dan

    Dan Guest

    Thanks, this works ... if i define x as Friend and not as Dim, because
    otherwise, x is still 0 or 1.

    "Alexey Smirnov" <> schreef in bericht
    news:...
    On May 25, 6:47 pm, "Dan" <> wrote:
    > Hi,
    >
    > in this gridview, there is in each row a checkbox linked to a field in the
    > database. Some are checked, other not. I want to know how many are (e.g.)
    > unchecked when the gridview is loaded.
    > This is my attempt (vb.net), but it doesn't work.
    > Thanks for help.
    > Dan.
    >
    > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > GridView1.RowDataBound
    > dim x as integer = 0
    > If e.Row.RowType = DataControlRowType.DataRow Then
    > if e.Row.Cells(2).Text = "false" then x=x+1
    > response.write("number of unchecked checkbox:" & x)
    > end if
    > end Sub
    >
    > asp:GridView ID="GridView1" runat="server" />
    > <Columns>
    > <asp:CommandField ShowSelectButton="true" />
    > <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" />
    > <asp:TemplateField HeaderText="selected" >
    > <ItemTemplate>
    > <asp:CheckBox ID="chb" runat="server" Checked='<%# Bind("selct") %>' >
    > </asp:CheckBox>
    > </ItemTemplate>
    > </asp:TemplateField>
    > </Columns>
    > </asp:GridView>


    I am not sure but it seems if you don't set the Text property to chb,
    you can't get it. Use following

    If e.Row.RowType = DataControlRowType.DataRow Then
    if e.Row.DataItem("selct") = "false" then x=x+1

    This should work
     
    Dan, May 25, 2009
    #4
  5. On May 25, 7:53 pm, "Dan" <> wrote:
    > Thanks, this works ... if i define x as Friend and not as Dim, because
    > otherwise, x is still 0 or 1.


    Yes, sure it has to be class-level variable
     
    Alexey Smirnov, May 26, 2009
    #5
  6. Dan

    shawpnendu

    Joined:
    Mar 4, 2009
    Messages:
    17
    Location:
    Dhaka, Bangladesh
    I THINK ROWDATABOUND IS NOT APPROPRIATE. WHY YOU DON'T TRY THROUGH A FOR EACH LOOP AFTER BIND?

    REGARDS
    shawpnendu.blogspot.com
     
    shawpnendu, May 26, 2009
    #6
    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. Claire
    Replies:
    3
    Views:
    4,846
    Wendy Smoak
    Mar 2, 2005
  2. runescience
    Replies:
    0
    Views:
    4,291
    runescience
    Feb 23, 2006
  3. RVic
    Replies:
    19
    Views:
    1,417
  4. Glenn
    Replies:
    2
    Views:
    126
    Glenn
    Feb 10, 2005
  5. Claire
    Replies:
    4
    Views:
    223
    Claire
    Mar 2, 2005
Loading...

Share This Page