Checking if radiobuttonlist has been selected - javascript

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.

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;
}
}

How to disable delete button when all the checkboxes are disabled of a column in grid asp.net by JavaScript?

I have a grid in which I have 7 columns where all the checkboxes are
disabled. How do I disable the delete button?
In the diagram below, if all the checkboxes in the Cancel SO Line Item
column are disabled then the DEL button should be disabled.
.Aspx File
<asp:TemplateField HeaderText="Cancel SO Line Item">
<ItemTemplate>
<asp:checkbox ID="cbSOCan" runat="server" ViewStateMode="Enabled" EnableViewState="true"></asp:checkbox>
</ItemTemplate>
<asp:LinkButton CssClass="btn btn-primary" ID="btnCancelItem" runat="server" CausesValidation="False"OnClientClick="return Confirmationbox();"> Cancel Item</asp:LinkButton>
<asp:HiddenField id="hdnval" value=0 runat="server"/>
C# code
protected void btnCancelItem_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in gvPOItems.Rows) {
CheckBox chkdelte = (CheckBox)gvrow.FindControl("cbSOCan");
HiddenField hdnval = (HiddenField)gvrow.FindControl("hdnval");
if (chkdelte.Checked) {
// gvAdditionalArea.Rows(rowIndex).Cells(0).Text()
Int32 ItemNumber = Convert.ToInt32(gvrow.Cells(0).Text());
Queries.CancelSOlineItem(ItemNumber, txtPONumber.Text);
gvrow.Cells(7).Text() = "Cancelled";
chkdelte.Checked = false;
chkdelte.Enabled = false;
hdnval.Value = 1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
{if (!IsPostBack)
int rowcount = 0;
foreach (GridViewRow gvrow in gvPOItems.Rows) {
HiddenField hdnval = (HiddenField)gvrow.FindControl("hdnval");
if ((hdnval.Value == 1)) {
rowcount = rowcount + 1;
}
}
if ((gvPOItems.Rows.Count == rowcount)) {
btnCancelItem.Visible = false;
}
}
}
You can check the state of the CheckBoxes in the RowDataBound event. If even one checkbox is enabled, set the global boolean to true and then in the footer row set the Delete Button state.
bool oneCheckBoxIsEnabled = false;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow or the footer row
if (e.Row.RowType == DataControlRowType.DataRow)
{
//check the state of the checkbox
//either by checking the source data for the value that would trigger the disable
DataRowView row = e.Row.DataItem as DataRowView;
if (Convert.ToBoolean(row["isEnabledInDB"]) == true)
{
oneCheckBoxIsEnabled = true;
}
//or the state of the checkbox with findcontrol
CheckBox cb = e.Row.FindControl("CheckBox1") as CheckBox;
if (cb.Enabled == true)
{
oneCheckBoxIsEnabled = true;
}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
//use the footer row to enable or disable the button
DeleteButton.Enabled = oneCheckBoxIsEnabled;
}
}
by Jquery
disableCheckbox = function () {
//checked check-boxes length
checkedCount = $('#CP_Main_gvPOItems input[type=checkbox]:checked').length;
//check-boxes length
checkboxCount = $('#CP_Main_gvPOItems input[type=checkbox]').length;
//if no check-box is selected then alert
// if (checkedCount == 0) {
// alert('No check-box is selected');
// }
//check for all disabled check-boxes
//var disableCheckBox = 0;
$('#CP_Main_gvPOItems input[type=checkbox]').each(function () {
if ($(this).is(':disabled')) {
disableCheckBox++;
}
});
//if all check-boxes are disabled then disable button
if (checkboxCount == disableCheckBox) {
$('#CP_Main_btnCancelItem').attr("disabled", "disabled"); //# is missing
}
}

Passing textBox to java script function

My question might be little old but I tried all the possibilities and could not find any solution.
I want to do one function for verification the input field:
This function works fine:
function myFunction()
{
var x;
x = document.getElementById('<%=textBoxCustomerCode.ClientID%>').value;
if(isNaN(x) || x == "")
{
alert('Problem input');
return false;
}
else
{
return true;
}
}
and here is the code:
<asp:ImageButton ID="imageButtonView" runat="server" Height="60px" ToolTip="بحث" Width="60px" BorderStyle="Solid" ImageUrl="~/Images/searchButton.jpg" OnClick="imageButtonView_Click" OnClientClick="return myFunction();"/>
This function works fine but once I need to make the function works with any input field. I want way to pass any field to the function so it will verify it. Any idea to do so.
Something like this could work (making the function work for any textbox):
function myFunction(textboxid)
{
var x;
x = document.getElementById(textboxid).value;
if(isNaN(x) || x == "")
{
alert('Problem input');
return false;
}
else
{
return true;
}
}
Here you give the function the id of the textbox you want to check. We do not check if a textbox with the given id exists though.
Change x = document.getElementById('<%=textBoxCustomerCode.ClientID%>').value; to x = document.getElementById('imageButtonView').value;
Try ..........this
function myFunction(id)
{
var x;
x = document.getElementById(id).value;
if(isNaN(x) || x == "")
{
alert('Problem input');
return false;
}
else
{
return true;
}
}
and pass that id through onclientclick event. i.e.
<asp:ImageButton ID="imageButtonView" runat="server" Height="60px" ToolTip="بحث" Width="60px" BorderStyle="Solid" ImageUrl="~/Images/searchButton.jpg" OnClick="imageButtonView_Click" OnClientClick="return myFunction(here is that id to pass);"/>
You can simply use class for finding the element. Or you can pass id to the function in OnClientClick()..
function myFunction(element)
{
var x;
x = document.getElementById('element').value;
if(isNaN(x) || x == "")
{
alert('Problem input');
return false;
}
else
{
return true;
}
}
and you can call this function by OnClientClick="return myFunction(this);"

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>

Returning Values in javascript function

I have a scenario where there are 4 text boxes and one Radio Button list. If i click the Submit button by leaving all fields it should alert me . I used 4 Required Validator s for text boxes and a java script function for the radio button list.
My mark up language and function is:
function Validate() {
var radio = document.getElementsByName("rdbGender");
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked) {
return true;
}
else
{
alert("Choose a Gender");
return false;
}
}
}
Aspx page is:
<asp:Button ID="btnSave" Text="Save" runat="server" Width="50px" OnClientClick= "Validate()"
OnClick="btnSave_Click" />
When ever i click the button by leaving fiealds empty, and leaving radio button list with out selecting a item, it is alerting but if i press ok it is executing further. I read in one website that if we write " return true " after alert has been showed and clicking ok it will execute further,,but i wrote "return false" after alert it means untill i check one radio button it should not execute...Where am i wrong??
Javascript:
<script type="text/javascript">
function Validate() {
var checkedVal = false;
var radio = document.getElementsByName("rdbGender");
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked) {
checkedVal = true;
return checkedVal;
}
else {
alert('Chhooose gender');
return checkedVal;
}
}
}
</script>
Mark up design:
<tr>
<td>
Gender
</td>
<td>
<asp:RadioButtonList ID="rdbGender" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Value="MALE">
</asp:ListItem>
<asp:ListItem Value="FEMALE">
Female
</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
.
.
.
.![enter image description here][2]
<td>
<asp:Button ID="btnSave" Text="Save" runat="server" Width="50px" ValidationGroup="Star" OnClientClick="return Validate()"
OnClick="btnSave_Click" />
</td>
Try using return when calling client function.
<asp:Button ID="btnSave" Text="Save" runat="server" Width="50px"
OnClientClick= "return Validate();"
OnClick="btnSave_Click" />
For first iteration of for, If first radio button is not checked it would go to else part which is showing alert and returning false. Please implement below code.
function Validate() {
var checkedval=false;
var radio = document.getElementsByName("rdbGender");
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked) {
checkedval=true;
return checkedval;
}
}
return checkedval;
}
A function can only have one return value. When return true is executed, the function immediately return true and stop going further. Therefore, comment out the return true and try.
function Validate() {
var radio = document.getElementsByName("rdbGender");
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked) {
//return true;
//this return true causes the problem.
}
else
{
alert("Choose a Gender");
return false;
}
}
}

Categories

Resources