Stop a PostBack using JavaScript and the OnClientClick event

If you need to be able to update the text displayed in an ASP.net control such as a LinkButton through code-behind before the page is rendered but will end up using that control to invoke client-side javascript and don’t want a PostBack to happen you can simply add “return false;” after your custom JavaScript calls in the OnClientClick event handler of the control. “return false;” actually stops the page from posting back to the server and allows your custom JavaScript to be run client-side.

Example:
<asp:LinkButton ID="lbtnVisit" runat="server" Text="» Default Text" OnClientClick="CustomScriptMethod(); return false;">

Full Page Processed on Partial PostBack

One odd thing you need to keep in mind when using AJAX.net and the AjaxControlToolkit is that when the UpdatePanel is used to do an asynchronous call back to the server the entire page state is sent back to the server and the entire page is reprocessed. This means that everything in your Page_Load and other initialization methods will be called again.

Dynamically Find Control in Page

Need to find a control in your ASP.net page without knowing where and how deep within containing controls it is buried. Using .Net 3.5 features such as LINQ and Extension Methods its cake! Here is the method:


public static IEnumerable All(this ControlCollection controls)
{
foreach(Control control in controls)
{
foreach(Control grandchild in control.Controls.All())
yield return grandchild;
yield return control;
}
}

ASP.net: Validator Controls Hidden but still taking up space

If you are using the built-in ASP.net Validation Controls such as the RequiredFieldValidator or the RegularExpressionValidator to validate user input, as you should be, you may have notices that in your UI even though they are not active (hidden) they are still taking up space in your layout and may be throwing things off.
It turns out that the Validation Controls in ASP.net  have an attribute you can add to their markup called ‘Display’. By default if you do not add and set this attribute in your control’s markup it will default to ‘Static’. There are 2 other options available to you ‘None’ and ‘Dynamic’. It is due to the ‘Static’ option that you are seeing it take up space even when it is not active.
To fix the problem set the ‘Display’ attribute in the control’s markup to the ‘Dynamic’ value and poof, the space is no longer taken up when inactive and is dynamically added when active.
Here is what each option actually does to the Validator’s HTML markup when set.
Static (Default): Causes the required space for the error message to be taken up in the UI’s HTML no matter if it is inactive or active.

<span style="color: Red; visibility: hidden;">Invalid</span>

Dynamic: Causing the element to not display at all, but when activated the space needed to display the error is reclaimed and UI elements are shifted to make room.

<span style="color: Red; display: none;">Invalid</span>

None: Causing the element to not display at all even when activated, the error message still displays in a Validation Summary if provided but nothing will appear in the UI where the Validator was originally positioned.

<span style="color: Red; display: none;"></span>