ActionLink passing parameter as QueryString

In MVC Razor ActionLinke, it is a common requirement to pass parameter as a query string. It creates URL like below:

/Home/Edit/1

and of course basically it is <a href="/CodeTest/EditClass/1">Edit</a>

ActionLink provide different overloads, using one of them such links can be generated:

@Html.ActionLink("Home", "Edit", new {id=1})

Instead of hard coded strign a model property can also be used:

@Html.ActionLink("Home", "Edit", new {id=Model.Id})

Important

If you are using T4MVC templates is a little different from above examples, then you will code as per below example:
@Html.ActionLink("Edit",MVC.CodeTest.EditClass()
.AddRouteValue("id",1))

and using model:
@Html.ActionLink("Edit",MVC.CodeTest.EditClass()
.AddRouteValue("id",itm.ClassID))

For passing multiple parameters use AddRouteValues().

Error on grid view paging “Failed to load viewstate.. “

I was getting “Failed to load viewstate ..” error when I was trying to load second page of grid.
I had AllowPaging=”true”, grid page size was set but even then in client’s test environment grid paging was not working at all and throwing the following error:

“Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.”.

Setting EnableViewState=false for that grid fixed this error.

Events sequence in page, master page and user controls

Event sequence became complex when master and content page have user and server controls in them. I just tried to check this sequence and my findings are as below:

Content Page_PreInt
Master Page User Control Page_Init
Master Direct Control init event
Content Direct Control init event
Content Page User Control Direct Control init event
Content Page User Control Page_Init
Master Page_Init
Content Page_Init
Content Page_InitComplete
Content Page_PreLoad
Content Page_Load
Master Page_Load
Master Page User Control Page_Load
Master Direct Control load event
Content Direct Control load event
Content Page User Control Page_Load
Content Page User Control Direct Control load event
Content Page_LoadComplete
Content Page_PreRender
Master Page_PreRender
Master Page User Control Page_PreRender
Master Page Direct Control Pre Render event
Content Page Direct Control Pre Render event
Content Page User Control Page_PreRender
Content Page User Control Page Direct Control Pre Render event
Content Page_SaveState

For more details please check this and this.

Enable or disable asp.net validation controls from Java Script

I like Asp.net validation controls because they work on client side as well as on server side. Some time we need to disable validation controls from server side and then enable them on client side depending on some condition (for example if a particular checkbox or radio button is checked). The following code can be used in Java script to enable a validation control.

 ValidatorEnable(document.getElementById("<%=MyValidationControl.ClientID%>"), true); 

Same line of code will disable a validation control if we replace true with false. ValidatorEnable is a funtion provided by validation controls client side API. A useful boolean property of this API is isvalid and it is associated with each validation control. It is different from IsValid server side property. False is returned if given input is not validated by the validation control.

For more details on asp.net validation controls please see this nice article.

Content page to Master page communication

One way of Content page to Master page communication is having properties or methods in master page and call them form content page.
Simply add @ Master Type directive in content (.aspx) page and mention virtual path to your master page

<%@ MasterType VirtualPath="~/MailBox/ThenMail.master"%> 

Above line will create a strongly typed reference to master page and by using Master property we can access methods and properties of master page in content page.

Here I am declaring a property in my master page’s cs class:

public bool MailBoxVisibility 
{ 
set 
 { 
 lbtnMailBox .Visible = value; 
 }
} 

Now see how easily we can access this property in  content page .cs class.

Master.MailBoxVisibility = false; lbtnMailBox .Visible = value; 

Selecting rows from DataTable without using Linq

DataRow(s) from DataTable can be selected without LINQ just by using DataTable.Select() method.

string searchExpression = null; 
searchExpression = "CategoryId=" + 2; 
DataRow[] SelectedDataRows = EmailAttachment.Select(searchExpression);

SelectedDataRows will have all rows where CategoryID is 2. An overload of Select also allows to select and sort results, for more details on it

please see http://msdn.microsoft.com/en-us/library/way3dy9w.aspx