c# - Verify CheckBox Checked in foreach -
i needed verify if exists checkbox unchecked in form. have verification in btnsend_onclick event. if exists 1 checkbox unchecked, enable label message error. if have other rows, , it's checked, how it's foreach, don't show mesage error! how build verification, in c#, verifies if exists checkbox unchecked , shows message user , stops foreach? i'm clear?
my cs:
protected void btnsend_onclick(object sender, eventargs e) { foreach (gridviewrow row in gridview.rows) { checkbox check = (checkbox)row.findcontrol("checkbox1"); checkbox check2 = (checkbox)row.findcontrol("checkbox2"); if (check.checked == false && check2.checked == false) { lblerrorcheck.visible = true; } else { } } }
my page:
<body> <form id="form1" runat="server"> <div> <div class="control-group error"> <asp:label id="lblerrorcheck" runat="server" class="control-label" for="inputerror" visible="false">please, check checkbox each folder!</asp:label> </div> <asp:gridview id="gridview" runat="server" autogeneratecolumns="false" gridlines="none" cssclass="table table-bordered table-striped"> <columns> <asp:boundfield datafield="accessgroup" headertext="access group" /> <asp:templatefield headertext="access type"> <itemtemplate> <asp:checkbox id="checkbox1" runat="server" text="access read" oncheckedchanged="checkbox1_changecheck" autopostback="true" /> <asp:checkbox id="checkbox2" runat="server" text="access modify" oncheckedchanged="checkbox2_changecheck" autopostback="true" /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> <asp:button id="btnsend" class="btn btn-large" runat="server" text="send request access" onclick="btnsend_onclick" align="left" /> </div> <div align="center" style="width: auto; height: auto;"> <asp:hyperlink id="hyperlink2" runat="server" imageurl="~/image/home_back_48.png" navigateurl="~/home.aspx">homepage</asp:hyperlink> </div> </form>
try this
protected void btnsend_onclick(object sender, eventargs e) { foreach (gridviewrow row in gridview.rows) { checkbox check = (checkbox)row.findcontrol("checkbox1"); checkbox check2 = (checkbox)row.findcontrol("checkbox2"); if (!check.checked && !check2.checked) { lblerrorcheck.visible = true; break; } } }
hope helps you.
the break;
clause here tells loop stop when reaches line. take took @ this msdn article more details.
Comments
Post a Comment