2014年7月13日 星期日

[RESOLVED] How to Count Checked CheckBoxes in the page ?


Hi all,


i have an page and masterpage for it i want to count checked Checboxes i try this code


void get_chk()
{
foreach (CheckBox cb in Page.Controls)
{
if (cb.Checked)
{
count++;
}
}
}

and i that err ?


Unable to cast object of type 'ASP.master_page_site1_master' to type 'System.Web.UI.WebControls.CheckBox'.

i try and change my code to:


            foreach (Control cb in Page.Controls)
{
int count = 0;
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}

now no cast err - but it is not count any thing ( count still = 0 ) and i ( checked 3 CheckBoxes ) ........ !


and the if condetion is false ...... ?


if (cb is CheckBox && ((CheckBox)cb).Checked)

so how can i resolve that ..... !?



Use


ContentPlaceHolder p = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");


where "ContentPlaceHolder1" is name of your place holder and then access its Controls collection.



Are you able to use a checkboxlist control instead? This will allow you to easily iterate through the cbl Items collection to determine which items are checked/selected. 





smirnov



Use


ContentPlaceHolder p = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");


where "ContentPlaceHolder1" is name of your place holder and then access its Controls collection.






do you mean the code will be 



ContentPlaceHolder cp1 = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

foreach (CheckBox cb in cp1.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}

i try that not working .... !?







MetalAsp.Net



Are you able to use a checkboxlist control instead? This will allow you to easily iterate through the cbl Items collection to determine which items are checked/selected. 





no i can't use it ..... :(



Fair enough. Ok well if you can put all the checkboxes inside of a panel control you'll be able to iterate through the panel Controls collection. If not then you'll need to use recursion to drill down into all the controls, sub-controls, sun-sub-controls
etc. 





red_scorpion1


i try that not working .... !?


Where do you call that code?


I tried this


ContentPlaceHolder p = (ContentPlaceHolder)this.FindControl("ContentPlaceHolder1");
int count = 0;
foreach (Control cb in p.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}

and it counts checkboxes.


I also noticed that in your original code you put count = 0 in the loop which was wrong.


P.S.


If you call it from master page, you should use this.FindControl.





smirnov



red_scorpion1
i try that not working .... !?

Where do you call that code?
I tried this
ContentPlaceHolder p = (ContentPlaceHolder)this.FindControl("ContentPlaceHolder1");
int count = 0;
foreach (Control cb in p.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}

and it counts checkboxes.


I also noticed that in your original code you put count = 0 in the loop which was wrong.


P.S.


If you call it from master page, you should use this.FindControl.




i have tryed a new page with same master and the code works fine


in my first page i have add an updatepanal (id="UpdatePanel1") and the checkboxes on it  ?


i try this code


            ContentPlaceHolder p = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
UpdatePanel up = (UpdatePanel)this.FindControl("UpdatePanel1");
int count = 0;
foreach (Control cb in p.Controls)
{
foreach (Control item in up.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}
}

but here when i run code the UP = null .... !


if checkboxes out of the updatepanal it works 


note: updatepanal in page not in masterpage




Master HTML:












Page HTML:
















so what i must do please ..............









red_scorpion1


in button in the page not in masterpage


Then you can use your original code, but int count = 0; must be outside of foreach loop, because you reset counter to 0 on every item.


foreach (Control cb in Page.Controls)
{
int count = 0;
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}




smirnov



red_scorpion1
in button in the page not in masterpage

Then you can use your original code, but int count = 0; must be outside of foreach loop, because you reset counter to 0 on every item.
foreach (Control cb in Page.Controls)
{
int count = 0;
if (cb is CheckBox && ((CheckBox)cb).Checked)
{
count++;
}
}




i have edit my prv reply i hope you see it ?



Why do you do this?


1) ContentPlaceHolder p = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
2) UpdatePanel up = (UpdatePanel)this.FindControl("UpdatePanel1");

1) if you call the code from content page and looking for controls in content page, you don't need to search for placeholders. This is required only if you call the code from master page and looking for the controls inside content page. See my example in one of my posts before.
2) If you call the code from content page you don't need to use FindControl, simply use id - UpdatePanel1


So, if I understand it correct you need


int count = 0;
foreach (Control cb in UpdatePanel1.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
count++;
}




smirnov



Why do you do this?
1) ContentPlaceHolder p = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
2) UpdatePanel up = (UpdatePanel)this.FindControl("UpdatePanel1");

1) if you call the code from content page and looking for controls in content page, you don't need to search for placeholders. This is required only if you call the code from master page and looking for the controls inside content page. See my example in one of my posts before.
2) If you call the code from content page you don't need to use FindControl, simply use id - UpdatePanel1


So, if I understand it correct you need


int count = 0; foreach (Control cb in UpdatePanel1.Controls) { if (cb is CheckBox && ((CheckBox)cb).Checked) count++; }


i try this code but nothing ..... !!!!! - it is dont count any checkbox


        protected void Button1_Click(object sender, EventArgs e)
{
int count = 0;
foreach (Control cb in UpdatePanel1.Controls)
{
if (cb is CheckBox && ((CheckBox)cb).Checked)
count++;
}
}

and tel that is false 


if (cb is CheckBox && ((CheckBox)cb).Checked)

MY HTML of page that has the updatepanal:


<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="HR.WebForm2" %>














and thank you so much for trying help ............ ???









try


Control p = (Control)UpdatePanel1.Controls[0];

foreach (Control cb in p.Controls)

... 


or


foreach (Control cb in UpdatePanel1.ContentTemplateContainer.Controls)

{

    if (cb is CheckBox && ((CheckBox)cb).Checked)

         count++;

}





smirnov



try


Control p = (Control)UpdatePanel1.Controls[0];

foreach (Control cb in p.Controls)

... 


or


foreach (Control cb in UpdatePanel1.ContentTemplateContainer.Controls)

{

    if (cb is CheckBox && ((CheckBox)cb).Checked)

         count++;

}






Thank you so much ... finaly worked ..... Smile


沒有留言:

張貼留言