Javascript not working on asp:Datalist checkbox onchange - javascript

This is a Datalist in my code.
<asp:DataList runat="server" ID="dlstate" RepeatColumns="6">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chk" Text='<%#Eval("area_state") %>' OnCheckedChanged="chk_state_SelectedIndexChanged" onchange="return checkconfirm(this);" AutoPostBack="true" />
</ItemTemplate>
</asp:DataList>
This is the Javascript code.
<script type="text/javascript">
function checkconfirm(a) {
if (a.checked == true) {
return true;
}
else {
debugger;
var box = confirm("Are you sure you want to unselect this state as areas alloted under this state will be lost?");
if (box == true)
return true;
else {
a.checked = true;
return false;
}
}
}
</script>
The problem is that when this apsx page is rendered in html the onchange="return checkconfirm(this);" appears on a span rather than the checkbox. I have tried several ways for confirmation and yet I am not able to put the onchange event on the checkbox.

You can use a jQuery listener for this.
<script type="text/javascript">
$(document).ready(function () {
$("#<%= dlstate.ClientID %> input[type='checkbox']").change(function () {
if (!$(this).prop("checked")) {
var box = confirm("Are you sure you want to unselect this state as areas alloted under this state will be lost?");
if (box == true)
return true;
else {
$(this).prop("checked", "checked");
return false;
}
}
});
});
</script>

Related

How can I replace asp listbox selection with textbox value?

I have an asp.net listbox that has an option of "Other". If a user selects "Other" I want to enable a textbox. When the form is submitted I want the text inside of of the textbox to replace "Other". So far I have gotten the textbox to enable if the "Other" value is selected but not sure how to replace "Other" with the text from the textbox. Any ideas?
<script type="text/javascript">
$(document).ready(function () {
$("#ListBox1").click(function () {
var arr = [];
$.each($("#ListBox1 option:selected"), function () {
arr.push($(this).val());
});
if ($.inArray("Other", arr) >= 0) {
$("#TextBox12").prop("disabled", false);
}
else {
$("#TextBox12").prop("disabled", true);
}
});
});
</script>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="Other" Value="other"></asp:ListItem>
<asp:ListItem Text="Blue" Value="blue"></asp:ListItem>
<asp:ListItem Text="Red" Value="red"></asp:ListItem>
<asp:ListItem Text="Green" Value="green"></asp:ListItem>
</asp:ListBox>
<asp:TextBox ID="TextBox12" runat="server" Enabled="false"></asp:TextBox>
You should be careful with case sensitivity. The listbox item value is other but you are trying to find value in the array as Other.
if ($.inArray("other", arr) >= 0) {
$("#TextBox12").prop("disabled", false);
}
else {
$("#TextBox12").prop("disabled", true);
}
The simplest way may be to work through the code behind:
protected void TextBox12_TextChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedItem != null && ListBox1.SelectedItem.Text == "Other")
{
ListBox1.SelectedItem.Text = TextBox12.Text;
}
}

Disable submit button if two textboxes have no value not working properly

I have two text-boxes and one button. I want that the button be disabled if one of the textboxes have no value. With this code the button is enabled if just one textbox has a value.
HTML
<asp:TextBox ID="TextBox2" runat="server" onblur = "Toggle()"></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server" onblur = "Toggle()"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" Enabled = "false" />
JavaScript
function Toggle() {
var txt1 = document.getElementById("<%=TextBox1.ClientID %>");
var txt2 = document.getElementById("<%=TextBox2.ClientID %>");
var btn = document.getElementById("<%=Button1.ClientID %>");
if (txt1.value == "" && txt2.value == "") {
btn.disabled = true;
} else {
btn.disabled = false;
}
}
Try
if (txt1.value && txt2.value) btn.disabled = false;
else btn.disabled = true;

Disabling RequiredFieldValidator for hidden fields

I have a dropdownlist that should be mandatory when not hidden, so i have a RequiredFieldValidator for this.
<table runat="server" id="tblLocations" class="ts1">
<tr>
<td class="tr0">
Location:
</td>
<td>
<asp:DropDownList ID="ddlLocations" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvDdlLocations" runat="server" ControlToValidate="ddlLocations" InitialValue="0" validationgroup="LocationValidation" ErrorMessage="Please select a Location" />
</td>
</tr>
</table>
When a radio button is selected I set the visibility of this table to false.
I want to disable validation of this field when not visible so I set this on the radio button changed event:
rfvDdlLocations.Enabled = tblLocations.Visible;
This doesn't disable the verification however.
I have tried using JQuery as suggested elsewhere but this has no effect either:
<script type="text/javascript">
$("[id$='btnDelegate']").click(function () {
if (!$("[id$='tblLocations']").is(':visible')) {
ValidatorEnable($("[id$='rfvDdlLocations']")[0], false);
}
//ValidationSummaryOnSubmit("LoginUserValidationGroup");
if (Page_ClientValidate("LocationValidation")) {
alert('it is valid');
return true;
}
else {
alert('Not valid');
return false;
}
});
you should use a customvalidator in this case as below
<asp:CustomValidator ID="cvDdlLocations" runat="server" ControlToValidate="ddlLocations" ValidationGroup="LocationValidation" ErrorMessage="Please select a Location" OnServerValidate="cvDdlLocations_ServerValidate" ClientValidationFunction="validateLocation" />
and for client validation
function validateLocation(s, args) {
if($('#'<%= Radio.ClientID %>).is(':checked')){
args.IsValid = true;
}
else {
args.IsValid = $('#<%= ddlLocations.ClientID%>').val() != "0";
}
}
and for server validation
protected void cvPrice2Edit_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = true;
if(!Radio.Checked) {
args.IsValid = ddlLocations.SelectedValue != "0";
}
}
I hope this helps

header checkbox not working first click

hey everyone i have two view
1- Index
2- Edit
i have a grid its header have one checkbox i want to click checkbox all rows checkbox is checkbox. in my header checkbox its not working first time when i click uncheck then check its work fine. i dont know why and what is the problem in my code please help
here is my index view its have Script
function Check_Uncheck() {
$('#SelectAll').click(function () {
if (this.checked) {
$('.checkbox:enabled').each(function () {
this.checked = true;
});
}
else {
$('.checkbox:enabled').each(function () {
this.checked = false;
});
}
});
}
and here is my Edit its have checkbox
<input type="checkbox" id="SelectAll" onclick="Check_Uncheck();" tabindex="3" />
If you want to select and unselect all checkboxes of the gridview row, then you can do something like below:-
<script type="text/javascript">
function SelectAll(obj) {
var valid = false;
var chkselectcount = 0;
var gridview = document.getElementById('<%= GridView1.ClientID %>');
for (var i = 0; i < gridview.getElementsByTagName("input").length; i++) {
var node = gridview.getElementsByTagName("input")[i];
if (node != null && node.type == "checkbox") {
node.checked = obj;
}
}
return false;
}
</script>
Gridview aspx:-
<asp:GridView ID="GridView1"
runat="server" AutoGenerateColumns="false"></asp:GridView>
And onButton click
Select :
<asp:LinkButton ID="lnkdelete" runat="server" CausesValidation="false" Text="All" OnClientClick="SelectAll(true); return false"></asp:LinkButton>
|
<asp:LinkButton ID="lnkundelete" runat="server" CausesValidation="false" Text="None" OnClientClick="SelectAll(false); return false"></asp:LinkButton>

Checking if radiobuttonlist has been selected

I'm using this code to check if a radiobuttonlist has been selected, however it doesn't return false if nothing has been checked, and continues through to the function in the class behind. The radiobutton list is generated from databinding in codebehind.
<asp:LinkButton class="btn" id="linkDelete" runat="server" onclick="link_Delete" OnClientClick="checkform()">Delete Template</asp:LinkButton>
<asp:radiobuttonlist id="optMessageType" runat="server" onselectedindexchanged="optMessageType_SelectedIndexChanged" RepeatDirection="Vertical" CssClass="none-table">
</asp:radiobuttonlist>
function checkform() {
var radiolist = document.getElementById('<%= optMessageType.ClientID %>');
var radio = radiolist.getElementsByTagName("input");
if (radio.length > 0) {
for (var x = 0; x < radio.length; x++) {
if (radio[x].type === "radio" && radio[x].checked) {
//alert("Checking...");
// alert("Selected item Value " + radio[x].value);
var r = confirm("Are you sure you want to delete this template?")
if (r == true) {
//alert("You pressed OK!")
return true;
}
else {
//alert("You pressed Cancel!");
return;
}
}
else {
alert("Select a template to delete");
return;
}
}
}
else {
alert("nope");
return;
}
}
Code Behind:
protected void link_Delete(object sender, System.EventArgs e)
{
Label3.Text = "Deleted!"; //For Testing
}
You should write
<asp:LinkButton class="btn" id="linkDelete" runat="server" onclick="link_Delete" OnClientClick="return checkform();">Delete Template</asp:LinkButton>
i have added return in OnClientClick atribute funcion call
hope this may solve our problem.

Categories

Resources