In this article I will
show you how you to use JavaScript confirm message from code behind in asp.net using c#.net. This article will
also help you to get the JavaScript conform message yes/No value on code behind
in c#.
Some of my
previous articles are as follows. How
to call javascript function from code behind Using C# in Asp.Net, Display
Alert Message on Page Load in MVC Application Using Javasscript, Bind
DataGridView In Windows Application Using C#, Digital
Clock in Asp.Net in C#.
Now here we go. First we will create a new asp.net application. Now
we will add the below code in you .aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ConfirmmessageInCsharp.aspx.cs" Inherits="ProjectDemo_Asp.et.ConfirmmessageInCsharp"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JavaScript
Confirm Message From Code Behind in Asp.Net Using C#</title>
<script language="javascript">
function
ConfirmMessage() {
var
selectedvalue = confirm("Do you want to save
data?");
if
(selectedvalue) {
document.getElementById('<%=txtconformmessageValue.ClientID
%>').value
= "Yes";
} else
{
document.getElementById('<%=txtconformmessageValue.ClientID
%>').value
= "No";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>JavaScript Confirm
Message From Code<br
/> Behind in Asp.Net Using C#</h3>
<asp:HiddenField ID="txtconformmessageValue" runat="server"
/>
<asp:Button ID="btnsubmit" runat="server" Text="Submit"
OnClientClick="javascript:ConfirmMessage();" onclick="btnsubmit_Click"/>
</div>
</form>
</body>
</html>
|
Here in above code please check the JavaScript method. In this method we have assign the value to a textbox on the bases of confirmation box button. We are assigning the value in a hidden field. Now check the .cs file code.
C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class ConfirmmessageInCsharp : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
}
/// <summary>
/// When user click on confirm message this button code
/// will confirm that user have click on yes or no
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
protected
void btnsubmit_Click(object
sender, EventArgs e)
{
if
(txtconformmessageValue.Value == "Yes")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Confirm
value", "alert('You have clicked
on YES.')", true);
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"Confirm value", "alert('You have clicked on CANCEL.')",
true);
}
}
}
}
|
VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace ProjectDemo_Asp.et
Partial Public Class ConfirmmessageInCsharp
Inherits
System.Web.UI.Page
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
EventArgs)
End Sub
'''
<summary>
''' When
user click on confirm message this button code
''' will
confirm that user have click on yes or no
'''
</summary>
'''
<param name="sender"></param>
'''
<param name="e"></param>
Protected
Sub btnsubmit_Click(ByVal
sender As Object,
ByVal e As EventArgs)
If
txtconformmessageValue.Value = "Yes"
Then
Page.ClientScript.RegisterStartupScript(Me.[GetType](),
"Confirm value", "alert('You have clicked on YES.')", True)
Else
Page.ClientScript.RegisterStartupScript(Me.[GetType](), "Confirm
value", "alert('You have clicked
on CANCEL.')", True)
End
If
End Sub
End Class
End Namespace
|
Here in above
code we are checking the hidden field value and the new are displaying the
message box that user has clicked on which button.
This is output when we click on ok button
Awesome tutorial, thanks.
ReplyDeletethanks for your valuable comment.
DeleteI am getting error
ReplyDeleteTypeError: document.getElementById(...) is null
when I try to set the value in hidden field.
Please check the control id which you are using in document.getElementById(...) is present in your form or not.
DeleteAwesome!!! thanks~
DeleteThanks for your valuable comment
Delete