Thursday, September 24, 2009

Set active tab on failed validation with ajax tabcontainer

Ajax Tab Container is one of the coolest control in ASP.NET but it has one problem when you're using mulitple validation controls on different tabs and on click of your submit button you need to set focus on the tab on which validation failed. Following javascript is useful to achive this:

function RunValidationsAndSetActiveTab()
{
if (typeof (Page_Validators) == "undefined") return;
try
{
var noOfValidators = Page_Validators.length;
for (var validatorIndex = 0; validatorIndex < noOfValidators; validatorIndex++)
{
var validator = Page_Validators[validatorIndex];
if(validator.validationGroup == 'CategoryGroup')
{
ValidatorValidate(validator);
if (!validator.isvalid)
{
var tabPanel = validator.parentNode.control;
if(typeof (tabPanel) != "undefined")
{
var tabContainer = tabPanel.get_owner();
tabContainer.set_activeTabIndex(tabPanel.get_tabIndex());
break;
}
}
}
}
}
catch (Error)
{
}
}


Please notice that we're using parentNode instead of parentElment because firefox doesn't recognize parentElement.

In the button that is used to submit all the tabs at once where each tab has set of validations you can call the above method on click of button on client side such as:

OnClientClick="RunValidationsAndSetActiveTab()"

Happy Programming!!!

Wednesday, July 15, 2009

Add nodes dynamically to the SiteMap For Your ASP.NET Site

The Web.sitemap file stores only static URLs, so if you have a page like this:
http://yoursite.com/news.aspx?view=4

Your sitemap breadcrumb will not reflect the fact that you are viewing a an actual news item. If you're like me, you also reflect the breadcrumb in the title of your page. So instead of seeing:
Breadcrumb: Home > News > A headline! Title: A headline! : News : My Site
You see:
Home > News News : My Site
Read on for how to solve it.

Solution
How do you fix it? I found a
great post on how to nest the sitemap in a master-detail style and then adjust the parent node's URL, but I wanted to actually add a new node because I was viewing and listing news on the same page.
Here's how you do it:

private string _mHeadline = string.Empty;

protected void Page_Load(object sender, EventArgs e)
{

// Here you set the page title to whatever dynamic thing
// you're loading.
_mHeadline = "A test";


SiteMap.SiteMapResolve += SiteMapResolve;
}

protected void Page_Unload(object sender, System.EventArgs e)
{
// Remove this specific handler once the page is done.
// Otherwise it will get called on other pages.
SiteMap.SiteMapResolve -= SiteMapResolve;
}

protected SiteMapNode SiteMapResolve(object sender, SiteMapResolveEventArgs e)
{

SiteMapNode cn = SiteMap.CurrentNode.Clone(true);
SiteMapNode newNode = default(SiteMapNode);

// "viewnews" can be changed to whatever you want. It's just a key.
if (_mViewTitle != string.Empty) {
newNode = new SiteMapNode(SiteMap.Provider, "viewnews", Request.Url.PathAndQuery, _mHeadline);
newNode.ParentNode = cn;
}
else {
newNode = cn;
}

return newNode;
}

So I left out how I was getting the title of the news headline because I assume you know how to do that. The place that is interesting is the SiteMapResolve function. I create a new node and set its parent to the current node, which successfully has the SiteMapPath show the correct breadcrumb:
Home > News > A headline!

A couple notes:

First, I remove the handler once the page is complete because otherwise the function will get called every time the SiteMap resolves itself, which is not what we want!
Second, you may need to touch the web.config (just remove a line and undo it) and save it to have your application recycle itself, otherwise your changes might not show up.
Hope this helps somebody. Happy Programming!!!

Reference:

http://blog.lib.umn.edu/ayubx003/dividebyzero/2009/01/10/how_to_programmatically_add_no.html#more


Thursday, July 9, 2009

VS Debug Problem with IE8

How VS debugger could be crashed with IE8?

If you opened multiple instances of IE8 and you attempt to debug your project, you mostly will have the issue where VS debugger just stops and ignores your break points!

Why was that?

Well, IE 8 has a feature called Loosely-Coupled Internet Explorer (LCIE) which results in IE running across multiple processes.
http://www.microsoft.com/windows/internet-explorer/beta/readiness/developers-existing.aspx#lcie

Older versions of the Visual Studio Debugger get confused by this and cannot figure out how to attach to the correct process.

To overcome this issue, you need to disable the process growth feature of LCIE by follow the below steps:

1) Open RegEdit
2) Browse to HKEY_LOCALMACHINE -> SOFTWARE -> Microsoft -> Internet Explorer -> Main
3) Add a dword under this key called TabProcGrowth
4) Set TabProcGrowth to 0

If you run into the same problem on Vista or newer, you will also need to turn off protected mode.

And then go a head and start debugging your code :)

Reference:
http://weblogs.asp.net/abdullaabdelhaq/archive/2009/06/01/VS-Debug-Problem-with-IE8.aspx

Thursday, April 30, 2009

Tip/Trick: Handling Errors with the UpdatePanel control using ASP.NET AJAX

Luis Abreu is an ASP.NET MVP who has a great blog on the http://msmvps.com blog site. Earlier today he posted a great tutorial post that describes how to use some of the new features in the ASP.NET AJAX Beta1 release to add more robust error handling into your application. I highly recommend reading and bookmarking it for future use.

Error handling in an AJAX world can often be tricky -- especially when AJAX call-backs are taking place and a mixture of client and server code is running within an application. In its most recent release, the control that comes with ASP.NET AJAX now has much more robust error handling and reporting features for you to use.

Specifically:
1) You can now handle the "OnAsyncPostBackError" event on the control to catch and/or modify an error message that occurs during the processing of an AJAX postback callback on the server.
2) You can now set the "AllowCustomErrors" property on the control to enable the standard ASP.NET custom error support to be enabled to redirect a user automatically to an error page when an error occurs (even if it is happening during an AJAX postback).
3) You can now optionally handle client-side JavaScript events on the page to intercept any error message sent back from the server, and perform custom client-side actions as a result (for example: to output the error message to a nicely formatted section instead of performing a pop-up message).

Read all about how to take advantage of the above new features from Luis' great tutorial here.

Hope this helps,

Viresh Shah