Galina Besselyanova
asked on
C#, ASP How to check how many check boxes are checked
Hello,
We have a web page with dynamically loaded check boxes (depends on how many records in database).
What i need is to make sure the user checks only 5 check boxes. And if he checks the 6th one, give him a message that he can only check 5.
ASP:
Below is a C# code:
Thanks.
We have a web page with dynamically loaded check boxes (depends on how many records in database).
What i need is to make sure the user checks only 5 check boxes. And if he checks the 6th one, give him a message that he can only check 5.
ASP:
Please select up to five:
<asp:Repeater runat="server" ID="rptCandidats" >
<ItemTemplate>
<div>
<asp:checkbox id="chResponse" runat="server" AutoPostBack="True" OnCheckedChanged="Check_Clicked"></asp:checkbox>
<%# Eval("ResText")%> <a href="<%#Eval("ResLink")%>" class="moreinfo"> More Info</a>
<asp:HiddenField ID="ResId" runat="server" Value ='<%#Eval("ResponId")%>' /> </div>
</ItemTemplate>
</asp:Repeater>
I tried to use OnCheckedChanged event, but it doesn't work.Below is a C# code:
protected void Check_Clicked(Object sender, EventArgs e)
{
int checkedCount = 0;
foreach (RepeaterItem r in rptCandidats.Items)
{
CheckBox cb = (CheckBox)r.FindControl("chResponse");
if (cb.Checked)
{
checkedCount += (sender as CheckBox).Checked ? 1 : -1;
}
}
if (checkedCount > 5)
{
System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Only Up to five candidates can be selected.\")</SCRIPT>");
}
}
Any ideas how can i achieve it?Thanks.
ASKER
Thanks!
Here is my new code
Here is my new code
protected void Check_Clicked(Object sender, EventArgs e)
{
string sysAlert="Only up to five selections.";
int checkedCount = 0;
foreach (RepeaterItem r in rptCandidats.Items)
{
CheckBox cb = (CheckBox)r.FindControl("chResponse");
if (cb.Checked)
{
checkedCount += (sender as CheckBox).Checked ? 1 : -1;
if (checkedCount > 5)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "This Alert", "alert('" + sysAlert + "');", true);
cb.Checked = false;
}
}
}
}
It is working, but now, when you click on the check box, the page blinking like it reloads .
take it out of the for each. and do you have your repeater ajaxified in an updatepanel?
protected void Check_Clicked(Object sender, EventArgs e)
{
string sysAlert="Only up to five selections.";
int checkedCount = 0;
foreach (RepeaterItem r in rptCandidats.Items)
{
CheckBox cb = (CheckBox)r.FindControl("chResponse");
if (cb.Checked)
{
checkedCount += cb.Checked ? 1 : 0;
}
}
if (checkedCount > 5)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "This Alert", "alert('" + sysAlert + "');", true);
cb.Checked = false;
}
}
ASKER
I'm sorry, i'm new to it.
There is no updatepanel.
And why remove For each if i need to count how many check boxes are already checked?
Am i doing something wrong?
There is no updatepanel.
And why remove For each if i need to count how many check boxes are already checked?
Am i doing something wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@kqureshi321
If you see the code I modified I still use the for each, I just moved the register script out of it.
https://www.experts-exchange.com/questions/28391393/C-ASP-How-to-check-how-many-check-boxes-are-checked.html?anchorAnswerId=39937803#a39937803
If you see the code I modified I still use the for each, I just moved the register script out of it.
https://www.experts-exchange.com/questions/28391393/C-ASP-How-to-check-how-many-check-boxes-are-checked.html?anchorAnswerId=39937803#a39937803
ASKER
Even with this change, the page still blinks like it reloads.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
use ScriptManager.RegisterStar
http://msdn.microsoft.com/en-us/library/bb359558%28v=vs.110%29.aspx
string javaScript = "<script language=JavaScript>\n" + "alert('Only Up to five candidates can be selected');\n" + "</script>";
ScriptManager.RegisterStar
, "alert", javaScript, true);