Click here to Skip to main content
15,887,989 members
Articles / Web Development / ASP.NET

Making server side decision based on client side JavaScript

Rate me:
Please Sign up or sign in to vote.
4.08/5 (5 votes)
14 Jan 2014CPOL 13.3K   9   4
Many of you might have come across the situation where you need to making server side discussion based on client side JavaScript confirm message

This articles was originally at wiki.asp.net 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
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">

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

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

  <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" />
  </div>

</asp:Content>

And the code behind for this page is like

C#
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;
            AddJavascriptCode(itemValue);
        }
        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"">
window.onload=function()
{
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';
objSubmit.click();
}
else
{
objField.value = 'false';
}
}
function newConfirm(title,mess,icon,defbut,mods)
{
if (document.all)
{
retVal = confirm(mess);
retVal = (retVal==1)
}
else
{
retVal = confirm(mess);
}
return retVal;
}
</script>";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Test", script);
    }
}

See the Result below

License

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


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
GeneralBad Code! Pin
Nitij1-May-14 21:40
professionalNitij1-May-14 21:40 
GeneralMy vote of 3 Pin
BH TAN15-Jan-14 22:12
professionalBH TAN15-Jan-14 22:12 
GeneralMy vote of 1 Pin
Adam R Harris14-Jan-14 7:31
Adam R Harris14-Jan-14 7:31 
QuestionYUCK! Pin
Adam R Harris14-Jan-14 7:30
Adam R Harris14-Jan-14 7:30 
I absolutely hate, HATE, seeing server side generated JavaScript because you need to add a few values from the server. This is just ugly code and becomes a nightmare to maintain.

Instead you can use the <%= %> tags to inject your server side variables into your javascript, like this:
objField = document.getElementById('<%= hdnField.ClientID %>');

This way you end up with javascript where it should be, in an HTML or JS file and not in your code behind having the strings escaped and just making an all around mess. Or if your javascript is included in an external file you can just set a variable that will be consumed by that file using the same method as above.

On a side note there is nothing in this article about making decisions on the server side based on client side javascript OMG | :OMG: WTF | :WTF: like the title implies. This is making decisions on the client side based on server side code, the complete opposite of your title.
Don't comment your code - it was hard to write, it should be hard to read!

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.