Making server side decision based on client side JavaScript

 Rate It (0)

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

<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

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

Revision number 3, Wednesday, January 07, 2009 9:34:48 PM by jsalinase

Comments

The formatting of the JavaScript code needs to be corrected.

Related Articles

Security Guidelines and Recommendations

There's a great deal of good prescriptive security guidance out there in the form of whitepapers and books. Whitepapers patterns & practices Security Guidance for .NET Framework 2.0 patterns & practices ASP.NET 2.0 Security Guidance patterns &

Enterprise Library

Enterprise Library "The patterns & practices Enterprise Library from Microsoft is a library of application blocks designed to assist developers with common enterprise development challenges. Application blocks are a type of guidance, provided as source

Security

ASP.NET security is a huge topic and we're only scratching the surface. Let's continue to categorize in the add new content around security to make thisa greatresource.This isjust the overview page, make sure to visit thesubpages fromthe Table ofContents

Interface, abstract, partial and sealed classes

InterfaceAn interface is like a class but all the methods and properties are abstract. An Interface cannot be instantiated like abstract class. All the methods and properties defined in Interface are by default public and abstract.Interface generally refers

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (14)
  2. codehard (3)
  3. Babunareshnarra (2)
  4. Dungimon (1)
  5. cabhilash (1)
Microsoft Communities