How to validate checkbox list in content page? - c#

I am taking a checkbox list in content page of a master page by which i can select multiple values and save in the database. How to confirm that minimum one of the checkboxes r selected at the time of clicking the submit button.

You can do like below.
It does not matter in JavaScript that where control are either or content page or master page
<asp:CheckBoxList ID="chkModuleList"runat="server" />
<asp:CustomValidator runat="server" ID="cvmodulelist"
ClientValidationFunction="ValidateModuleList"
ErrorMessage="Please Select Atleast one Module" />
// javascript to add to your aspx page
function ValidateModuleList(source, args)
{
var chkListModules= document.getElementById ('<%= chkModuleList.ClientID %>');
var chkListinputs = chkListModules.getElementsByTagName("input");
for (var i=0;i<chkListinputs .length;i++)
{
if (chkListinputs [i].checked)
{
args.IsValid = true;
return;
}
}
args.IsValid = false;
}
Update:
Or you can go through this approach: Put any css-class say company. You can go through below.
There is a :checked ( http://api.jquery.com/checked-selector/ ) selector you can use :
<script>
$(document).ready(function() {
$(".company input").click(function() {
var cnt = $(".company input:checked").length;
if (cnt == 0)
{
// none checked
}
else
{
// any checked
}
});
});
</script>
You may want to add (or use) an id on the container of these checkbox, to optimize selector speed.
As for asp.net messing up client ids on controls, you can use
$('<%= MyControl.ClientID %>')
References:
Check if a checkbox is checked in a group of checkboxes in clientside
How to validate a user chose at least one checkbox?

Dado.Validators, a library on GitHub, handles this very nicely too.
<asp:CheckBoxList ID="cblCheckBoxList" runat="server">
<asp:ListItem Text="Check Box (empty)" Value="" />
<asp:ListItem Text="Check Box 1" Value="1" />
<asp:ListItem Text="Check Box 2" Value="2" />
<asp:ListItem Text="Check Box 3" Value="3" />
</asp:CheckBoxList>
<Dado:RequiredFieldValidator runat="server" ControlToValidate="cblCheckBoxList" ValidationGroup="vlgSubmit" />
Example codebehind.aspx.cs
btnSubmit.Click += (a, b) =>
{
Page.Validate("vlgSubmit");
if (Page.IsValid) {
// Validation Successful
}
};
https://www.nuget.org/packages/Dado.Validators/

Related

Disable a validation control in Jquery?

I have asp.net web form with lot of .net controls. where few of the controls are disabled depending on the input of another controls. All of the controls are required so i have to check for required validation as well.
I can disable the control using jquery but i am not able to disable the validation control.
Here is my code.. any better way of doing this will be very appreciable.
<script>
$('#<%=rdb_one.ClientID%> input:radio').click(function () {
var currentIdone = 'Humans';
var currentId = $(this).val();
if (currentId != currentIdone) {
$('#<%=rdb_two.ClientID%> input').attr('checked', false);
$('#<%=rdb_two.ClientID%> input').attr('disabled', 'disabled');
}
else {
$('#<%=lstExposureValue.ClientID%> input').removeAttr('disabled');
}
});
</script>
<asp:RadioButtonList CssClass="Required" ID="rdb_one" runat="server" OnSelectedIndexChanged="rdb_studysubj_SelectedIndexChanged">
<asp:ListItem Value="Humans">Humans</asp:ListItem>
<asp:ListItem Value="Non-Human primates">Non-Human primates</asp:ListItem>
<asp:ListItem Value="Rodents">Rodents</asp:ListItem>
<asp:ListItem Value="Others">Others</asp:ListItem>
</asp:RadioButtonList>
<br/>
<br/>
<asp:RadioButtonList CssClass="Required" ID="rdb_two" runat="server" OnSelectedIndexChanged="rdb_study_popul_SelectedIndexChanged">
<asp:ListItem>Individuals</asp:ListItem>
<asp:ListItem>Population</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="vald_one" Display="None" ErrorMessage="Study Population is Required" runat="server" ControlToValidate="rdb_two"></asp:RequiredFieldValidator>
Here i disable the control "rdb_two" depending on the value from "rdb_one" but i am not able to disable the validation controls for "rdb_two" any ideas. i have this function repeatedly used over all the controls with in the same page.
You can use ValidatorEnable function
for example
ValidatorEnable(document.getElementById($(this).attr('id')), false);

Radio button selection should not be changed by any one

I have made a radio button list in an aspx page in C#
<asp:RadioButtonList ID="RblMentor_teacher_student" class="case" runat="server"
RepeatDirection="Horizontal" AppendDataBoundItems="False" >
<asp:ListItem Text="Docent" Value="0"></asp:ListItem>
<asp:ListItem Text="Mentor" Value="1"></asp:ListItem>
<asp:ListItem Text="Student" Value="2"></asp:ListItem>
</asp:RadioButtonList>
It works perfectly as I want that it shows the selected person is either teacher or student or mentor.
But I want that no one can change the selection of this.
Can anyone help.
You could put this in your Page_load() and it would disable the whole list.
RblMentor_teacher_student.Enabled = false;
Or you could disable a specific list item
<asp:ListItem Text="Docent" Value="0" Disabled="Disabled"></asp:ListItem>
You can set Enabled property to false
On selectedindexchanged event of radiobutton..then put enable property false for selected item
Using jQuery:
Benefit of using this is that the checked value will be always post on submit.
$(function () {
$("input[type=radio]", $("#<%= RblMentor_teacher_student.ClientID%>")).each(function (index) {
if ($(this).attr("checked") != "checked") {
$(this).attr("disabled", "disabled");
}
});
});

Multiple Selection in Dropdown and update query string

I generate a report according to selection of company and their customer. I use one dropdown for company and according to company the customer is displayed. However, I want to make it possible to select multiple customers, and when the user clicks on the button to the view report, I want to update the query string with that selection. If the selection of customer is three, I want to pass their customer code using query string. In sort querystring is send according to the selection of customer.
Please help me.
Thanks and regards.
Mitesh
In your selection page (aspx):
...
<script type="text/javascript">
function submitSelection() {
var customerList = document.getElementById('<%= lbCustomers.ClientID %>');
var companyList = document.getElementById('<%= lbCompany.ClientID %>');
var selectedCustomerQuery = [];
for (var i = 0; i < customerList.options.length; i++) {
if (customerList.options[i].selected)
selectedCustomerQuery.push('customer_id=' + customerList.options[i].value);
}
location.href = 'Report.aspx?company_id=' + companyList.value + '&' + selectedCustomerQuery.join('&');
}
</script>
<asp:DropDownList ID="lbCompany" runat="server" SelectionMode="Single" >
<asp:ListItem Value="1">Company 1</asp:ListItem>
<asp:ListItem Value="2">Company 2</asp:ListItem>
</asp:DropDownList><br />
<asp:ListBox ID="lbCustomers" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="John" Value="1"></asp:ListItem>
<asp:ListItem Text="Paul" Value="2"></asp:ListItem>
<asp:ListItem Text="Peter" Value="3"></asp:ListItem>
</asp:ListBox><br />
<input id="Button1" type="button" value="View Report" onclick="submitSelection()" />
...
Then in your Report.aspx.cs:
...
protected void Page_Load(object sender, EventArgs e)
{
var selectedCompany = Request.QueryString["company_id"];
//get passed selected customers, will be stored in an array
var selectedCustomers = Request.QueryString.GetValues("customer_id");
Response.Write(string.Format("Company ID: {0}, Customers: {1}", selectedCompany.ToString(), string.Join(",", selectedCustomers)));
}
...
This illustrates an example without posting back to the page. If you need to post back to handle other logic in your code behind, you'll have to change the <input> button to an ASP.NET button control and handle its OnClick event.

How can i make a disabled control back to enable using Javascript

I have done a code to disable a control when user clicks on a control. On my form i am having a TextBox and a DropDown. When user clicks on a TextBox i will disable the DropDown like that when click on DropDown i will disable the TextBox which works fine.
But when the user clicks on Disabled control i would like to enable that control. Means if i click on TextBox which was disabled i would like to Enable it like that for dropdown too..
My sample Script is as follows
<script type="text/javascript">
function toggleDropDownList1()
{
var d=document.getElementById("<%= DropDownList4.ClientID %>");
if(d.disabled)
{
d.disabled=false;
}
else
{
document.getElementById("<%= TextBox1.ClientID %>").disabled = true;
}
}
function toggleDropDownList2()
{
document.getElementById("<%= DropDownList4.ClientID %>").disabled = true;
}
</script>
Design
<asp:TextBox ID="TextBox1" runat="server" onclick="toggleDropDownList2();"></asp:TextBox>
<asp:DropDownList ID="DropDownList4" runat="server" onclick="toggleDropDownList1();">
<asp:ListItem Text="One" Value="1"></asp:ListItem>
<asp:ListItem Text="One" Value="1"></asp:ListItem>
</asp:DropDownList>
Apparently Firefox, and perhaps other browsers, disable DOM events on form fields that are disabled, and any event that starts at the disabled form field is completely canceled and does not propagate up the DOM tree.
So, if you want to achieve something like that, you will probably have to do it with a workaround.
Instead of actually disabling the field try to style the fields to look as if they are disabled, that way you can still capture the click event.
The idea is to place a div in front of dropdown list, and this div accept the onclick event.
The issue here is that the div can not place so easy in front of dropdown list. To place it you need to do that dynamically and use the absolut position.
I make here a small code, and test it and working. I have left the background color RED on div to see where it is. Some details I left it to you, eg to find the width and height of your control list, I place the onclick, you can place back the double click, And just remove the red background.
<script type="text/javascript">
function toggleDropDownList1()
{
var MyDiv = document.getElementById("DivForClick");
MyDiv.style.display = "none";
var d=document.getElementById("<%= DropDownList4.ClientID %>");
if(d.disabled)
{
d.disabled=false;
}
else
{
document.getElementById("<%= TextBox1.ClientID %>").disabled = true;
}
}
function toggleDropDownList2()
{
document.getElementById("<%= DropDownList4.ClientID %>").disabled = true;
var MyDdl = document.getElementById("<%= DropDownList4.ClientID %>");
var MyDiv = document.getElementById("DivForClick");
MyDiv.style.display = "block";
MyDiv.style.left = MyDdl.style.left;
MyDiv.style.top = MyDdl.style.top;
// need to find the height/width
// MyDiv.style.height = MyDdl.style.height;
// MyDiv.style.width = MyDdl.style.width;
}
</script>
and the asp code.
<asp:TextBox ID="TextBox1" runat="server" onclick="toggleDropDownList2();"></asp:TextBox>
<br /><br />
<div id="DivForClick" onclick="toggleDropDownList1();" style="z-index:999;position:absolute;left:0;top:0;height:20px;width:40px;background-color:Red;display:none;">
</div>
<asp:DropDownList ID="DropDownList4" runat="server" onclick="toggleDropDownList1();" style="z-index:2;">
<asp:ListItem Text="One" Value="1"></asp:ListItem>
<asp:ListItem Text="Two" Value="2"></asp:ListItem>
</asp:DropDownList>
Do you mean
function toggleIt() {
var d=document.getElementById("<%= DropDownList4.ClientID %>");
var t =document.getElementById("<%= TextBox1.ClientID %>");
d.disabled=!d.disabled
t.disabled=!t.disabled
}
<asp:TextBox ID="TextBox1" runat="server" onclick="toggleIt();"></asp:TextBox>
<asp:DropDownList ID="DropDownList4" runat="server" disabled="disabled" onclick="toggleIt();">
<asp:ListItem Text="One" Value="1"></asp:ListItem>
<asp:ListItem Text="One" Value="1"></asp:ListItem>
</asp:DropDownList>

respond to asp:RadioButtonList changes with jquery

I have the code below with sets the focus on a textbox when the page loads and when the user selects a radio button.
How can I change this so that if they choose the last radio option "Mail" the focus is not set to
txtPayerName but instead to the btnSubmit control?
$(function () {
SetDefaultPaymentType();
$("#txtPayerName").focus();
$("#rdLstPaymentOptions").change(function () {
$("#txtPayerName").focus();
});
});
<asp:RadioButtonList ID="rdLstPaymentOptions" runat="server" RepeatColumns="3" RepeatDirection="vertical"
RepeatLayout="Flow" OnPreRender="rdBtnLst_PreRender" TabIndex="1"
ClientIDMode="Static">
<asp:ListItem Text="Credit Card " Value="CreditCard" ></asp:ListItem>
<asp:ListItem Text="Electronics Fund Transfer " Value="EFT"></asp:ListItem>
<asp:ListItem Text="Check By Mail " Value="Mail"></asp:ListItem>
</asp:RadioButtonList>
What if you set the change event on each radio button individually?
Something like:
$("#rdLstPaymentOptions").each(function (index) {
if (index == $("#rdLstPaymentOptions").length - 1) {
// insert code to set change event for last radio button
}
else {
($this).change = function () {
$("#txtPayerName").focus();
}
}
});
Give the radio button and the submit button a unique CSS class, then use:
$('.radioClass').click(function() {
$('.submitClass').focus()
})

Categories