ASP.NET – How to call a server-side method from client-side JavaScript

Decoding the Web

Introduction

This is a tutorial on how to call a server-side ASP.NET method from a client-side JavaScript method.

If you are wondering when that could become useful, imagine the following scenario:

A Web Application having an implemented authentication system.
Users log in with their username and password.
At any point the user is able to log out by clicking on the respective “Log Out” button.
On the server-side, the log out action would trigger a cleaning up process of user’s temp data.

However, the user instead of clicking on the “Log Out” button, may simply close the browser window. Now since HTTP is a stateless protocol, the server-side cannot directly detect the user’s action. Therefore the client-side (browser) would have to notify the server that the user is closing the window.

A solution to this problem would be to call a JavaScript function when the client-side “onUnload” event…

View original post 397 more words

Converting Linq Expressions to T-SQL

devioblog

In my post about a Batch Update command for Linq To SQL, the code I presented uses a method ToMSSqlString() which has not yet been defined.

The method converts a Linq Expression to a T-SQL expression by evaluating the current node of the expression tree and evaluation the node’s children:

public static class ExpressionExtensions { public static string ToMSSqlString(this Expression expression) { switch (expression.NodeType) { case ExpressionType.Add: var add = expression as BinaryExpression; return add.Left.ToMSSqlString() + " + " + add.Right.ToMSSqlString(); case ExpressionType.Constant: var constant = expression as ConstantExpression; if (constant.Type == typeof(string)) return "N'" + constant.Value.ToString().Replace("'", "''") + "'"; return constant.Value.ToString(); case ExpressionType.Equal: var equal = expression as BinaryExpression; return equal.Left.ToMSSqlString() + " = " + equal.Right.ToMSSqlString(); case ExpressionType.Lambda: var l = expression as LambdaExpression; return l.Body.ToMSSqlString(); case ExpressionType.MemberAccess: var memberaccess = expression as MemberExpression; // todo: if column aliases are used, look up ColumnAttribute.Name return "[" +…

View original post 74 more words