How to display "yes" or "no" message box in asp.net? - javascript

I want to display a message to the user to select yes or no. According to the clicked button(Yes or no), the value should be passed to a query as a parameter. How can I do this. This is what I have done so far.
<script type="text/javascript">
function confirm()
{
var confirmValue = document.createElement("INPUT");
confirmValue.type = "hidden";
confirmValue.name = "confirmValue";
if (confirm("Do you want to finalize? If you click yes, you will not be able to update and application will be passed to the MBD Coordinator")) {
confirmValue.value = "YES";
}
else
{
confirmValue.value = "NO";
}
document.forms[0].appendChild(confirmValue);
}
</script>
<asp:Button ID="btnFinish" runat="server" Text="Finish" BorderStyle="Solid" BorderColor="#9B0423"
Font-Bold="true" Width="100px" Height="30px" ForeColor="#9B0423" Style="margin-right:20px;" OnClick="btnFinish_Click" OnClientClick = "confirm()"/>
This is the C# code
string confirmValue = Request.Form["confirmValue"];
if (confirmValue == "Yes")
{
string value = "YES";
}
else
{
string value = "NO";
}
But in this I don't get a message to select any option.

Change the name of the function from "confirm" to something else. You are overriding the default confirm function in JavaScript.

Related

Confirm messagebox not working correctly

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()"/>

Deleting data using confirmation window popup in ASP.NET and with RequiredFieldValidator

I am using the following code to delete records from database using confirmation.
ASP.NET
<asp:TextBox ID="text_delete" Width="70%" Height="60px" ValidationGroup="del" CssClass="textbox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="required_desc" CssClass="validator" ValidationGroup="del" ControlToValidate="text_delete" ErrorMessage="Reason Required" runat="server">Reason Required</asp:RequiredFieldValidator>
<asp:Button ID="button_delete" CssClass="button" ValidationGroup="del" Text="Delete" runat="server" OnClientClick="Confirm()" OnClick="button_delete_Click" />
JavaScript
<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 delete this data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
C#
protected void button_delete_Click(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
// Delete the data
}
if (confirmValue == "No")
{
// Do not delete data
}
}
But here i want the user to enter the reason to delete in textbox first and only after that the confirm popup should appear. Even though i am using a required field validator the validator is fired only after the popup window. How can i fire the validator first and only if the reason is entered the popup window for delete confirmation should appear. How can i do this?
You can invoke the required field validation within a script using Page_ClientValidate(), or Page_ClientValidate("group name") if you want to be more specific.
if(Page_ClientValidate()){
....popup confirmation code
....
}
Working RequiredFieldValidator along with javascript

ASP Web Forms fire code behind event on condition

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"))
{
}
}

RegisterStartupScript shows message after function ends

i want to show a javascript confirm box on the client side under OnChange event "chkIsActive_CheckedChanged". i am using registerStartupscript int the code behind for this purpose. this confirm message is displaying fine but it is displaying after end of the event "chkIsActive_CheckedChanged". i want to display the confirm message while executing in the function. Please help me out.
My HTML
<asp:CheckBox ID="lbl_IsActive" runat="server" OnCheckedChanged="chkIsActive_CheckedChanged" AutoPostBack="true" Checked='<%# Eval("IsActive") %>' ></asp:CheckBox>
My javascript
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.getElementById("<%=IsChecked.ClientID%>");
if (confirm("Do you want to save data?")) {
confirm_value.value = "1";
} else {
confirm_value.value = "0";
}
}
</script>
my C# code behind
protected void chkIsActive_CheckedChanged(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyFun1", "Confirm();", true);
string confirmValue = IsChecked.Value;
if (confirmValue == "1")
{
string CarEstimateID = "";
Entities.CarEstimateFirms ObjEst = new Entities.CarEstimateFirms();
CheckBox chk = (CheckBox)sender;
GridViewRow gr = (GridViewRow)chk.Parent.Parent;
CarEstimateID = (GridView1.Rows[gr.RowIndex].FindControl("lbl_CarEstimateFirmID") as Label).Text; // GridView1.DataKeys[gr.RowIndex].Value.ToString();
ObjEst.CarEstimateFirmID = Convert.ToInt32(CarEstimateID);
ObjEst.IsActive = chk.Checked;
BLL.Common.UpdateCarEstimateFirms(ObjEst);
BindGridView();
}
}
Add onchange javascript event in your checkbox:
<asp:CheckBox ID="lbl_IsActive" runat="server" OnCheckedChanged="chkIsActive_CheckedChanged" AutoPostBack="true"
Checked='<%# Eval("IsActive") %>' onchange="javascript:return Confirm();" ></asp:CheckBox>
Your javascript confirm method should be like below:
<script type = "text/javascript">
function Confirm() {
if (confirm("Do you want to save data?")) {
return true;
} else {
return false;
}
}
</script>
And remove below line from chkIsActive_CheckedChanged event from code behind:
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyFun1", "Confirm();", true);

validation of radiobuttonlist in asp.net using javascript

i have to validate a radio button using javascript. it should show a message if none of the option is selected.
my radio button code is:
<asp:RadioButtonList ID="welldata" runat="server" RepeatDirection="Horizontal" Width="100px">
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
and the submit button is:
<asp:Button ID="next2" runat="server" Text=">>" Width="46px"
OnClientClick="return next2()"/>
the corresponding javascript function is :
function next2() {
var listItemArray = document.getElementById('<%=welldata.ClientID %>');
var isItemChecked = false;
var length1 = listItemArray.length;
for (var i=0; i<length1; i++)
{
var listItem = listItemArray[i];
document.write(listItem);
if ( listItem.checked )
{
alert(listItem.value);
isItemChecked = true;
}
}
if ( isItemChecked == false )
{
alert('Nothing is checked for welldata!');
return false;
}
return true;
}
while debugging i noticed thaat function is executed but doesn't enter the for loop.
also i tried
document.write(isItemChecked);
document.write(listItemArray);
document.write(length1);
and the output was :
false [object HTMLTableElement] undefined
The wellData RadioButtonList ASP.NET server control will render in the browser as a table with a number of input type="radio" controls under it, each with the same name.
So, you need to get the input tags inside the table tag first:
var wellData = document.getElementById('<%=welldata.ClientID %>');
var listItemArray = wellData.getElementsByTagName('input');
This is, of course, if you are doing this manually for some odd reason. You can do it automatically with a RequiredFieldValidator control.
<asp:RadioButtonList ID="welldata" runat="server" RepeatDirection="Horizontal" Width="100px">
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="rfvWellData" runat="server" ControlToValidate="wellData" Display="Dynamic" ErrorMessage="Pick yourself some well data" />
You are using a RadiobuttonList which is rendered as table per default. If you select the dom element by id document.getElementById('<%=welldata.ClientID %>') then you are selecting the table and not the RadioButtonList. If you want to select the radio buttons then you have to loop through the childNodes of listItemArray to get the radio buttons. Alternatively you could use jquery to select the radio buttons, as their ids will start with the id of your radio button list (they will look like welldata_0 or welldata_1).
This line of jquery code will get the your radio buttons
var listItemArray = $("input[id^='welldata']")
Try this:
function next2() {
if(document.getElementById('<%=welldata.ClientID %>').checked==false)
{
alert('Nothing is checked for welldata!');
return false;
}
return true;
}
or
Use RequiredFieldValidator
<asp:RequiredFieldValidator
ID="ReqiredFieldValidator1"
runat="server"
ControlToValidate="welldata"
ErrorMessage="Select your welldata!"
>
Check these below links
http://asp-net-example.blogspot.in/2009/02/aspnet-requiredfieldvalidator-example.html
http://www.itechies.net/tutorials/jscript/jsexample.php-pid-jform.htm#rb
http://www.codeproject.com/Questions/499602/RequiredplusFieldplusValidatorplusForplusRadioplus
var rblItems = document.getElementById('<%=radioBtnListId.ClientID %>').getElementsByTagName('input');
var checkedItems = true;
for (var i = 0; i < rblItems.length; i++) {
if (rblItems[i].checked) {
checkedItems = true;
break;
}
else {
checkedItems = false;
}
}
if (!checkedItems) {//Hence No items checked
alert("Nothing is checked");
return false;
}

Categories