The below code runs a confirm messagebox with yes no on Asp.Net.
I need to detect the value if its confirmed or not.
How can I do this ?
Aspx :
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/>
</form>
</body>
</html>
Code behind :
protected void OnConfirm(object sender, EventArgs e)
{
// This method runs even though the user clicks no.
}
Update :
With this code, both yes or no selections runs the same method named OnConfirm.
So I try to run the OnConfirm method only if the user clicks yes.
Update:
you can use a hidden field with runat=server and save yes/no. then send it to server.
<input type="hidden" runat="server" value="" id="hidden1" />
function Confirm() {
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
document.getElementById("hidden1").value = "yes";
} else {
confirm_value.value = "No";
document.getElementById("hidden1").value = "no";
}
document.forms[0].appendChild(confirm_value);
}
If you use a master page, remember that client id of hidden is different with its server id.
protected void OnConfirm(object sender, EventArgs e)
{
string confirmValue = hidden1.Value;
}
i think what you want to do is only execute the server side button when the user confirms OK right? if yes, then just do this, basically when the javascript function returns false, the server side button will not fire ie: (OnClientClick = "return Confirm()")
<script type = "text/javascript">
function Confirm() {
return confirm("Do you want to save data?");
}
</script>
<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "return Confirm()"/>
I have ASP.NET WEB FORMS apps
<asp:Button ID="DeleteDealsButton" runat="server" Text="Delete" />
function btnDeleteClick() {
var result = confirm("Want to delete all Deals?");
if (result) {
return true;
} else {
return false;
}
}
$("#MainContent_DeleteDealsButton").on("click", function() {
if (btnDeleteClick()) {})
On this button I have sample javascript alert to ask OK or Cancel.I want if choise OK to fire CODE BEHIND method call DeleteDealsButton_Click, if it Cancel to do nothing.
Just change button defination to,
<asp:Button ID="DeleteDealsButton" runat="server" Text="Delete" OnClientClick="return confirm('Want to delete all Deals?')" />
No need for any javascript function.
try like this:
function btnDeleteClick() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
confirm_value.value = "No";
var result = confirm("Want to delete all Deals?");
if (result) {
confirm_value.value = "Yes";
__doPostBack('DeleteDealsButton', '')
return true;
} else {
confirm_value.value = "No";
return false;
}
}
In your .Aspx file Change your button code to like this:
<asp:Button ID="DeleteDealsButton" OnClientClick=" return btnDeleteClick()" runat="server" Text="Delete" OnClick="DeleteDealsButton_Click" />
In your .Aspx.cs file
protected void DeleteDealsButton_Click(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (!String.IsNullOrWhiteSpace(confirmValue) && confirmValue.Equals("Yes"))
{
}
}
How to validate user control textbox value on Parent page? I have the following code but it couldn't find the control. I have checked IE View Source(HTML) and this control renders as
ctl00_Management_Initiation_txtTitle_txtInput
How to get the value using JQuery? Please let me know.
User Control
<telerik:RadTextBox id="txtInput" runat="server" TextMode="MultiLine" LabelCssClass=""></telerik:RadTextBox>
<asp:image id="ImgRequired1" ImageUrl="../../images/requiredfield.gif" AlternateText="Required Field" Runat="server" Visible=false></asp:image><br />
.aspx page
<script type="text/javascript">
$(document).ready(function () {
$("#<%=btnSave.ClientID %>").click(function () {
var valid = true;
var msg = "Please fill form:\n";
if($("input[id*=txtTitle]").val().length == 0{
msg += "Title is Required!\n";
valid = false;
}
});
});
</script>
<uc1:ControlTextBox ID="txtTitle" runat="server" MaxLength="200" ></uc1:ControlTextBox>
<asp:Button ID="btnSave" name="btnSave" runat="server" Text="Save" />
if($("#<%= txtTitle.TextInputClientID %>").val().length == 0) {
There is serious lack of closing brackets, both "}" and ")" in your javascript.
Add property to your user control:
public string TextInputClientID { get { return txtInput.ClientID; } }
I looked many examples here of enabling and disabling a button in javascript with jquery and any of them worked for me.
Here my desperate situation.
<asp:TextBox ID="mytext" runat="server" onkeyup="enableButton(this, 3)"/>
<asp:Button ID="myButton" runat="server" Text="Search" disabled="disabled" />
and my javascript
<script type="text/javascript">
function enableButton(control, chars) {
if (control.value.length >= chars) {
$('#myButton').removeAttr("disabled");
} else {
$('#myButton').attr("disabled", true);
}
}
</script>
EDIT
Hey fellows, finally I got it!
I registered the call of my javascript function in LoadPage event in the codebehind
mytext.Attributes.Add("onkeypress", "enableButton('" + mytext.ClientID + "',3,'" + mybutton.ClientID + "');"
Thank you all you guys for the time!
Disabled is a property, not an attribute.
Use:
$('#myButton').prop("disabled", "disabled");
Javascript:
<script language="javascript" type="text/javascript">
function SetButtonStatus(sender, target)
{
if ( sender.value.length >= 12 )
document.getElementById(target).disabled = false;
else
document.getElementById(target).disabled = true;
}
</script>
HTML:
<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this, 'btnButton')"></asp:TextBox>
<asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false" />
Use this.
$('#<%= myButton.ClientID%>').attr('disabled', 'disabled');
Did you add jquery library in the head tag?
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
Here is what I've done before.
//On document load
$(function(){
//Set button disabled
$("input[type=submit]").attr("disabled", "disabled");
//Append a change event listener to you inputs
$('input').change(function(){
//Validate your form here, example:
var validated = true;
if($('#nome').val().length === 0) validated = false;
//If form is validated enable form
if(validated) $("input[type=submit]").removeAttr("disabled");
});
//Trigger change function once to check if the form is validated on page load
$('input:first').trigger('change');
})
finally I got it!
I registered the call of my javascript function in LoadPage event in the codebehind
mytext.Attributes.Add("onkeypress", "enableButton('" + txtTRReference.ClientID + "',3,'" + btnGetTR.ClientID + "');"
Thank you all you guys for the time!
function enableButton(control, chars) {
if (control.value.length >= chars) {
$('#<%= myButton.ClientID%>').prop("disabled", false);
$('#<%= myButton.ClientID%>').css("pointer-events", "auto");
$('#<%= myButton.ClientID%>').css("opacity", "1");
} else {
$('#<%= myButton.ClientID%>').prop("disabled", true);
$('#<%= myButton.ClientID%>').css("pointer-events", "none");
$('#<%= myButton.ClientID%>').css("opacity", ".5");
}
}
disabled works only in IE, it doesn't work in chrome, so add the css pointer-events none which changes the mouse pointer and doesn't allow the link to be clicked.
Edit: Also use myButton.ClientID when you don't use ClientID = static
I have this much
<asp:Button ID="btnSaveContest" runat="server" Text="Save & Publish Contest" OnClientClick="javascript:changeInputTexts(); return disableAfterClick();"
ValidationGroup="ContestAdd" OnClick="btnSaveContest_Click" />
Now I want to call disableAfterClick() after all validations are completed. It should be in Client Side. So that I can ensure that the user can click the button only one time.
function ValidateCreateContest() {
Page_ClientValidate();
if (Page_IsValid) {
$('#<%=btnSaveContest.ClientID%>').attr('disabled', 'disabled');
return true;
}
else {
return false;
}
}`