problem reading the status of a check box in a gridview

Discussion in 'ASP .Net' started by ma, Sep 18, 2007.

  1. ma

    ma Guest

    Hello,

    I have a GrideView that has 4 col. 3 of them are bound to a dataset and
    the last one is not. The last one is check box. The check box is defined as
    follow:



    <asp:TemplateField HeaderText="Select">

    <ItemTemplate>

    <asp:CheckBox ID="chkSelect" runat="server" />

    </ItemTemplate>

    </asp:TemplateField>



    I am trying to read the state of these checkbox using the folllwng code:



    // Select the checkboxes from the GridView control

    for (int i = 0; i < GridView_Enterprise_name.Rows.Count; i++)

    {

    GridViewRow row = GridView_Enterprise_name.Rows;

    bool isChecked =
    ((CheckBox)row.FindControl("chkSelect")).Checked;

    if (isChecked)

    {

    // do something

    }

    }



    but the code doesn't work as isChecked is always false. What is the problem
    and how can I solve it?



    Regards
     
    ma, Sep 18, 2007
    #1
    1. Advertising

  2. When do you do that? On the first page load or on postbacks? In what event?

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "ma" <> wrote in message
    news:uFCs53j%...
    > Hello,
    >
    > I have a GrideView that has 4 col. 3 of them are bound to a dataset and
    > the last one is not. The last one is check box. The check box is defined
    > as follow:
    >
    >
    >
    > <asp:TemplateField HeaderText="Select">
    >
    > <ItemTemplate>
    >
    > <asp:CheckBox ID="chkSelect" runat="server" />
    >
    > </ItemTemplate>
    >
    > </asp:TemplateField>
    >
    >
    >
    > I am trying to read the state of these checkbox using the folllwng code:
    >
    >
    >
    > // Select the checkboxes from the GridView control
    >
    > for (int i = 0; i < GridView_Enterprise_name.Rows.Count; i++)
    >
    > {
    >
    > GridViewRow row = GridView_Enterprise_name.Rows;
    >
    > bool isChecked =
    > ((CheckBox)row.FindControl("chkSelect")).Checked;
    >
    > if (isChecked)
    >
    > {
    >
    > // do something
    >
    > }
    >
    > }
    >
    >
    >
    > but the code doesn't work as isChecked is always false. What is the
    > problem and how can I solve it?
    >
    >
    >
    > Regards
    >
    >
     
    Eliyahu Goldin, Sep 19, 2007
    #2
    1. Advertising

  3. ma

    ma Guest

    Hello,
    There is a button on the page and I am doing this on the postback event
    of this button.

    Regards

    "Eliyahu Goldin" <> wrote in
    message news:ea2CoVp%...
    > When do you do that? On the first page load or on postbacks? In what
    > event?
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "ma" <> wrote in message
    > news:uFCs53j%...
    >> Hello,
    >>
    >> I have a GrideView that has 4 col. 3 of them are bound to a dataset
    >> and the last one is not. The last one is check box. The check box is
    >> defined as follow:
    >>
    >>
    >>
    >> <asp:TemplateField HeaderText="Select">
    >>
    >> <ItemTemplate>
    >>
    >> <asp:CheckBox ID="chkSelect" runat="server" />
    >>
    >> </ItemTemplate>
    >>
    >> </asp:TemplateField>
    >>
    >>
    >>
    >> I am trying to read the state of these checkbox using the folllwng code:
    >>
    >>
    >>
    >> // Select the checkboxes from the GridView control
    >>
    >> for (int i = 0; i < GridView_Enterprise_name.Rows.Count; i++)
    >>
    >> {
    >>
    >> GridViewRow row = GridView_Enterprise_name.Rows;
    >>
    >> bool isChecked =
    >> ((CheckBox)row.FindControl("chkSelect")).Checked;
    >>
    >> if (isChecked)
    >>
    >> {
    >>
    >> // do something
    >>
    >> }
    >>
    >> }
    >>
    >>
    >>
    >> but the code doesn't work as isChecked is always false. What is the
    >> problem and how can I solve it?
    >>
    >>
    >>
    >> Regards
    >>
    >>

    >
    >
     
    ma, Sep 19, 2007
    #3
  4. Is ViewState enabled for the gridview?

    Make sure you are not re-loading the grid on every postback.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "ma" <> wrote in message
    news:%233Fskvs%...
    > Hello,
    > There is a button on the page and I am doing this on the postback event
    > of this button.
    >
    > Regards
    >
    > "Eliyahu Goldin" <> wrote in
    > message news:ea2CoVp%...
    >> When do you do that? On the first page load or on postbacks? In what
    >> event?
    >>
    >> --
    >> Eliyahu Goldin,
    >> Software Developer
    >> Microsoft MVP [ASP.NET]
    >> http://msmvps.com/blogs/egoldin
    >> http://usableasp.net
    >>
    >>
    >> "ma" <> wrote in message
    >> news:uFCs53j%...
    >>> Hello,
    >>>
    >>> I have a GrideView that has 4 col. 3 of them are bound to a dataset
    >>> and the last one is not. The last one is check box. The check box is
    >>> defined as follow:
    >>>
    >>>
    >>>
    >>> <asp:TemplateField HeaderText="Select">
    >>>
    >>> <ItemTemplate>
    >>>
    >>> <asp:CheckBox ID="chkSelect" runat="server" />
    >>>
    >>> </ItemTemplate>
    >>>
    >>> </asp:TemplateField>
    >>>
    >>>
    >>>
    >>> I am trying to read the state of these checkbox using the folllwng code:
    >>>
    >>>
    >>>
    >>> // Select the checkboxes from the GridView control
    >>>
    >>> for (int i = 0; i < GridView_Enterprise_name.Rows.Count; i++)
    >>>
    >>> {
    >>>
    >>> GridViewRow row = GridView_Enterprise_name.Rows;
    >>>
    >>> bool isChecked =
    >>> ((CheckBox)row.FindControl("chkSelect")).Checked;
    >>>
    >>> if (isChecked)
    >>>
    >>> {
    >>>
    >>> // do something
    >>>
    >>> }
    >>>
    >>> }
    >>>
    >>>
    >>>
    >>> but the code doesn't work as isChecked is always false. What is the
    >>> problem and how can I solve it?
    >>>
    >>>
    >>>
    >>> Regards
    >>>
    >>>

    >>
    >>

    >
    >
     
    Eliyahu Goldin, Sep 19, 2007
    #4
  5. ma

    ma Guest

    Thanks. The problem solved.
    I set the datagride data source on page_load event and it seems that doing
    this will reset the checkboxes. I used a ispostback and the problem solved
    now.

    Regards

    "Eliyahu Goldin" <> wrote in
    message news:eExegGt%...
    > Is ViewState enabled for the gridview?
    >
    > Make sure you are not re-loading the grid on every postback.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "ma" <> wrote in message
    > news:%233Fskvs%...
    >> Hello,
    >> There is a button on the page and I am doing this on the postback event
    >> of this button.
    >>
    >> Regards
    >>
    >> "Eliyahu Goldin" <> wrote in
    >> message news:ea2CoVp%...
    >>> When do you do that? On the first page load or on postbacks? In what
    >>> event?
    >>>
    >>> --
    >>> Eliyahu Goldin,
    >>> Software Developer
    >>> Microsoft MVP [ASP.NET]
    >>> http://msmvps.com/blogs/egoldin
    >>> http://usableasp.net
    >>>
    >>>
    >>> "ma" <> wrote in message
    >>> news:uFCs53j%...
    >>>> Hello,
    >>>>
    >>>> I have a GrideView that has 4 col. 3 of them are bound to a dataset
    >>>> and the last one is not. The last one is check box. The check box is
    >>>> defined as follow:
    >>>>
    >>>>
    >>>>
    >>>> <asp:TemplateField HeaderText="Select">
    >>>>
    >>>> <ItemTemplate>
    >>>>
    >>>> <asp:CheckBox ID="chkSelect" runat="server" />
    >>>>
    >>>> </ItemTemplate>
    >>>>
    >>>> </asp:TemplateField>
    >>>>
    >>>>
    >>>>
    >>>> I am trying to read the state of these checkbox using the folllwng
    >>>> code:
    >>>>
    >>>>
    >>>>
    >>>> // Select the checkboxes from the GridView control
    >>>>
    >>>> for (int i = 0; i < GridView_Enterprise_name.Rows.Count; i++)
    >>>>
    >>>> {
    >>>>
    >>>> GridViewRow row = GridView_Enterprise_name.Rows;
    >>>>
    >>>> bool isChecked =
    >>>> ((CheckBox)row.FindControl("chkSelect")).Checked;
    >>>>
    >>>> if (isChecked)
    >>>>
    >>>> {
    >>>>
    >>>> // do something
    >>>>
    >>>> }
    >>>>
    >>>> }
    >>>>
    >>>>
    >>>>
    >>>> but the code doesn't work as isChecked is always false. What is the
    >>>> problem and how can I solve it?
    >>>>
    >>>>
    >>>>
    >>>> Regards
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    ma, Sep 19, 2007
    #5
    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. mit
    Replies:
    1
    Views:
    845
    Ramu Pulipati
    Jan 25, 2006
  2. saha
    Replies:
    0
    Views:
    4,140
  3. Gnarlodious
    Replies:
    4
    Views:
    17,704
    elizas
    May 5, 2010
  4. Jim

    Check box and text box disable

    Jim, Jun 3, 2004, in forum: Javascript
    Replies:
    3
    Views:
    132
    Randy Webb
    Jun 4, 2004
  5. babu17
    Replies:
    1
    Views:
    148
Loading...

Share This Page