capturing checkbox.checked in repeater control

Discussion in 'ASP .Net' started by =?Utf-8?B?QW5kcmV3?=, Aug 22, 2005.

  1. Hi,

    I have a problem capturing the checkboxes that are checked, I get false
    irrespective of wether they are checked or not.

    I have gone thru the sample code on this forum, but they dun seem to work.
    This is the code that I used to go thru the repeater control to find my
    checkboxes.

    foreach(RepeaterItem r in MyRepeater.Items)
    {
    for (int i=1; i<=6; i++) //6 checkboxes
    {
    CheckBox chk = (CheckBox)r.FindControl("chkbox"+ i);
    if(chk.Checked)
    {
    tstdAnswer += "1";
    }
    else
    tstdAnswer += "0";
    }
    }
    lbl2.Text += " tstdAnswer is: " + tstdAnswer;

    I used the step-by-step debugger, n everytime, the program will loop
    straight to the false code, n give me a "000000" string each time.

    I databind the checkbox in the html code as below, display of the info is
    perfectly correct, so the error shouldn't be here. Below is a cut-down
    version of my code:

    <ASP:REPEATER id="MyRepeater" runat="server">
    <ItemTemplate>
    <asp:CheckBox id="chkbox1" runat="server" Checked="false" visible='<%#
    DataBinder.Eval(Container.DataItem, "choiceA").ToString() != "" %>' >
    </asp:CheckBox>
    </ItemTemplate>
    <FooterTemplate>
    </Table>
    </FooterTemplate>
    </ASP:REPEATER>

    TIA.
    Andrew.
    =?Utf-8?B?QW5kcmV3?=, Aug 22, 2005
    #1
    1. Advertising

  2. Are you binding this in the Page_Load ??

    Be sure to use the Page.IsPostBack method in your pageload.
    Otherwise, the Repeater will keep getting reset, and all your checkboxes
    will be in there default value (unchecked)

    So, if you code looks something like this:

    void Page_Load(object Sender, Eventargs e)
    {
    BindMyGrid(); //or whatever code you use to populate your repeater
    }

    Change it too:

    void Page_Load(object Sender, Eventargs e)
    {
    if(!Page.IsPostBack)
    BindMyGrid(); //or whatever code you use to populate your repeater
    }

    HTH

    "Andrew" <> wrote in message
    news:...
    > Hi,
    >
    > I have a problem capturing the checkboxes that are checked, I get false
    > irrespective of wether they are checked or not.
    >
    > I have gone thru the sample code on this forum, but they dun seem to work.
    > This is the code that I used to go thru the repeater control to find my
    > checkboxes.
    >
    > foreach(RepeaterItem r in MyRepeater.Items)
    > {
    > for (int i=1; i<=6; i++) //6 checkboxes
    > {
    > CheckBox chk = (CheckBox)r.FindControl("chkbox"+ i);
    > if(chk.Checked)
    > {
    > tstdAnswer += "1";
    > }
    > else
    > tstdAnswer += "0";
    > }
    > }
    > lbl2.Text += " tstdAnswer is: " + tstdAnswer;
    >
    > I used the step-by-step debugger, n everytime, the program will loop
    > straight to the false code, n give me a "000000" string each time.
    >
    > I databind the checkbox in the html code as below, display of the info is
    > perfectly correct, so the error shouldn't be here. Below is a cut-down
    > version of my code:
    >
    > <ASP:REPEATER id="MyRepeater" runat="server">
    > <ItemTemplate>
    > <asp:CheckBox id="chkbox1" runat="server" Checked="false" visible='<%#
    > DataBinder.Eval(Container.DataItem, "choiceA").ToString() != "" %>' >
    > </asp:CheckBox>
    > </ItemTemplate>
    > <FooterTemplate>
    > </Table>
    > </FooterTemplate>
    > </ASP:REPEATER>
    >
    > TIA.
    > Andrew.
    >
    Grant Merwitz, Aug 22, 2005
    #2
    1. Advertising

  3. U are right I am. I load the ds in the page_load, from there I call a method
    BindRepeater(), which will load the repeater with the ds.datatable.

    I already have the: if(!Page.IsPostBack) in my page_load. When I run the
    program, I used the debugger n i detect that after i click the Next button,
    the program calls the BindRepeater() method again, which will explain why i
    get checkbox.checked false values each time.

    So what I did for testing sake was to take away the BindRepeater method n
    just insert its code straight into the Page_load. I used the debugger n saw
    that when it reaches:
    foreach(RepeaterItem r in MyRepeater.Items)
    it just skips past this loop n proceed on.
    I wonder why, hmmmm ...

    TIA.
    Andrew.

    ---------------------------------------------------------------------------------
    "Grant Merwitz" wrote:

    > Are you binding this in the Page_Load ??
    >
    > Be sure to use the Page.IsPostBack method in your pageload.
    > Otherwise, the Repeater will keep getting reset, and all your checkboxes
    > will be in there default value (unchecked)
    >
    > So, if you code looks something like this:
    >
    > void Page_Load(object Sender, Eventargs e)
    > {
    > BindMyGrid(); //or whatever code you use to populate your repeater
    > }
    >
    > Change it too:
    >
    > void Page_Load(object Sender, Eventargs e)
    > {
    > if(!Page.IsPostBack)
    > BindMyGrid(); //or whatever code you use to populate your repeater
    > }
    >
    > HTH
    >
    > "Andrew" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I have a problem capturing the checkboxes that are checked, I get false
    > > irrespective of wether they are checked or not.
    > >
    > > I have gone thru the sample code on this forum, but they dun seem to work.
    > > This is the code that I used to go thru the repeater control to find my
    > > checkboxes.
    > >
    > > foreach(RepeaterItem r in MyRepeater.Items)
    > > {
    > > for (int i=1; i<=6; i++) //6 checkboxes
    > > {
    > > CheckBox chk = (CheckBox)r.FindControl("chkbox"+ i);
    > > if(chk.Checked)
    > > {
    > > tstdAnswer += "1";
    > > }
    > > else
    > > tstdAnswer += "0";
    > > }
    > > }
    > > lbl2.Text += " tstdAnswer is: " + tstdAnswer;
    > >
    > > I used the step-by-step debugger, n everytime, the program will loop
    > > straight to the false code, n give me a "000000" string each time.
    > >
    > > I databind the checkbox in the html code as below, display of the info is
    > > perfectly correct, so the error shouldn't be here. Below is a cut-down
    > > version of my code:
    > >
    > > <ASP:REPEATER id="MyRepeater" runat="server">
    > > <ItemTemplate>
    > > <asp:CheckBox id="chkbox1" runat="server" Checked="false" visible='<%#
    > > DataBinder.Eval(Container.DataItem, "choiceA").ToString() != "" %>' >
    > > </asp:CheckBox>
    > > </ItemTemplate>
    > > <FooterTemplate>
    > > </Table>
    > > </FooterTemplate>
    > > </ASP:REPEATER>
    > >
    > > TIA.
    > > Andrew.
    > >

    >
    >
    >
    =?Utf-8?B?QW5kcmV3?=, Aug 23, 2005
    #3
    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. yurps
    Replies:
    1
    Views:
    6,864
    rajeshthangarasu1983
    Feb 29, 2008
  2. Alan Silver
    Replies:
    2
    Views:
    11,029
    Alan Silver
    Jul 19, 2005
  3. c_shah
    Replies:
    5
    Views:
    1,139
    CitrusMotors.com
    Jun 15, 2007
  4. JemPower

    Capturing checked event for Template based checkbox.

    JemPower, Oct 30, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    167
    JemPower
    Oct 30, 2003
  5. randy
    Replies:
    13
    Views:
    433
    Tad McClellan
    Nov 1, 2003
Loading...

Share This Page