respond to asp:RadioButtonList changes with jquery - c#

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()
})

Related

Have Validation with DropDownList

I have a DropDownList that is binded to a DataSource on the PageLoad of a page. In this DropDown List I've added a Select value as the default value. On the bottom of this drop down list and some textboxes I have a Add button. How do I add a validation on the dropdown list so that if the User leaves the value as "Select" he/she can not Click the add button unless he/she selects another value.
<asp:Label ID="lblItems" runat="server" Text="SemesterCode: "></asp:Label>
<asp:DropDownList ID="ddlItems" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text=" -- Select -- " Value="-1"></asp:ListItem>
</asp:DropDownList>
Use the "onchange" method for a drop down list. When the value of the DDL is changed call a JS function that will enable the other buttons/required fields.
function validateThingy(val) {
return (val != -1)
}
function calledOnChange(val) {
if (validateThingy(val)) {
//enable the buttons
} else {
//Keep the button disabled
}
}
Check Selected index of your dropdownlist If it is 0 so alerts a message else puts your logic likewise
if(ddlItems.SelectedIndex==0){
Response.Write("Please choose any value");
}else{
//Put your logic here
}

How to validate checkbox list in content page?

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/

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>

Categories