Sunday, 8 June 2014

JavaScript Confirm Message From Code Behind in Asp.Net Using C#

6/08/2014 - By Pranav Singh 6

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#.

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.

Now here is the output when we click on submit and after message box we click on cancel button




This is output when we click on ok button

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

6 comments:

  1. Awesome tutorial, thanks.

    ReplyDelete
  2. I am getting error
    TypeError: document.getElementById(...) is null
    when I try to set the value in hidden field.

    ReplyDelete
    Replies
    1. Please check the control id which you are using in document.getElementById(...) is present in your form or not.

      Delete
    2. Awesome!!! thanks~

      Delete
    3. Thanks for your valuable comment

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top