Wednesday, June 3, 2009

Optional parameters in SQL server

There might be situations where you would like to pass optional parameters to a stored procedure. Instead of constructing dynamic queries or doing many if else there is a cleaner way to do it.

SELECT [ClubId]
FROM [Club] where
(@ClubId IS NULL OR ClubId = @ClubId)

The where clause checks each parameter to see if it is NULL and in case it is checks the fields value with that of the parameter.

Friday, May 29, 2009

Manipulating div display in javascript and c#

To hide or show a div using javascript:
myDiv.style.display = "none";
myDiv.style.display = "block";

in C#:
myDiv.Attributes["style"] = "display:none;";
myDiv.Attributes["style"] = "display:block;";

Tuesday, February 24, 2009

Scroll to top on partial postback

In one of a project that I worked on we had a datalist with paging(datapager). The datalist was in an update panel to give the ajax impact and to improve the performance. The issue was when you scroll down to the page and move to the next page the page doesnt scroll up after partial postback.

It can be done by adding the following code in code-behind file in page load for example.

if (System.Web.UI.ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
System.Web.UI.ScriptManager.RegisterStartupScript(this, typeof(MyPage), "pagingAnchor", "window.location.href = '#';", true);

Where MyPage is the name of your page or control.

This javascript code needs to be added in the page to cancel the default scroll behavior.

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(beginRequest);
function beginRequest()
{
prm._scrollPosition = null;
}