Click here to Skip to main content
13,144,679 members (35,998 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 26 Dec 2008

Making server side decision based on client side JavaScript

, 14 Jan 2014
Rate this:
Please Sign up or sign in to vote.
Many of you might have come across the situation where you need to making server side discussion based on client side JavaScript confirm message

Editorial Note

This articles was originally at but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Many of you might have come across the situation where you need to making server side discussion based on client side JavaScript confirm message value. Though this may sound very basic feature, surprisingly large developers struggle when implementing this. In this article, I will show you how to.
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">

  <table style="margin-top:100px;">
    <tr style="padding-bottom:10px">
        <asp:Label ID="lblitemName" Text="Item Value" runat="server">
        <asp:TextBox ID="txtitemval" runat="server"> </asp:TextBox>

  <div style="padding-bottom:10px">
    <asp:Label ID="lblMsg" runat="server"></asp:Label>

  <div style=" margin-bottom:100px; margin-left:120px">
    <asp:HiddenField ID="hdnField" runat="server" Value="false" />
    <asp:Button ID="btnSubmit" runat="server" Text="Check Val" OnClick="btnSubmits_Click" />


And the code behind for this page is like

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Test : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)


    protected void btnSubmits_Click(object sender, EventArgs e)
        string itemValue = Convert.ToString(txtitemval.Text);
        if (hdnField.Value == "false")
            lblMsg.Visible = false;
        else if (hdnField.Value == "true")
            lblMsg.Visible = true;
            lblMsg.Text = string.Format("You have entered {0}", itemValue);
            hdnField.Value = "false";

    private void AddJavascriptCode(string itemValue)
        string script = @"< script language=""JavaScript"" type=""text/javascript"">
var IsConfirm = 1;
objField = document.getElementById('" + hdnField.ClientID + @"');
objSubmit = document.getElementById('" + btnSubmit.ClientID + @"');
IsConfirm = newConfirm('Test','You have entered " + itemValue + @" value. Are you sure you want to go next page?',1,1,0);
if(IsConfirm == true)
objField.value = 'true';;
objField.value = 'false';
function newConfirm(title,mess,icon,defbut,mods)
if (document.all)
retVal = confirm(mess);
retVal = (retVal==1)
retVal = confirm(mess);
return retVal;
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Test", script);

See the Result below


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

ASP.NET Community
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Collaborative Group

765 members

You may also be interested in...


Comments and Discussions

GeneralBad Code! Pin
Nitij1-May-14 21:40
professionalNitij1-May-14 21:40 
GeneralMy vote of 3 Pin
WebBHTAN15-Jan-14 22:12
professionalWebBHTAN15-Jan-14 22:12 
GeneralMy vote of 1 Pin
Adam R Harris14-Jan-14 7:31
memberAdam R Harris14-Jan-14 7:31 
QuestionYUCK! Pin
Adam R Harris14-Jan-14 7:30
memberAdam R Harris14-Jan-14 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 14 Jan 2014
Article Copyright 2008 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid