Thursday, November 15, 2007

Some tips

Some tips:

Tip #1:

sometimes browsers dont support Javascript or the users purposely or accidently enable/disable javascript
in a website.
If your website uses many javascripts, then it is a good idea to inform a friendly message if Javascripts
is turned off or not supported.

There are easy ways to do this. one way to check is use a hidden field in the page, set its initial
value to true,then call the javascript from onload. change the hidden field value to false. check again
for the hiddenfield value. if it is true, then javascript is either not enabled or supported

Another easy way to do this is to add the html tag below: Please add the < and > i.e opening and closing tags for html

noscript
meta http-equiv='refresh' content='0;url=http://localhost/JavascriptError.aspx'
noscript

This tag will check if JS is enabled or not and redirect to a Custom JSError page.


To check if JS is supported by a browser, use Request.Browser.Javascript in c#

Tip #2:

If you want to check browser version using C# code,

if (!IsPostBack)
{
if (Request.Browser.Browser == "IE")
{
if (Request.Browser.MajorVersion < 6)
{
//Redirect to custom page to download IE 6 or more
}
}
else if (Request.Browser.Browser == "Firefox")
{
if (Request.Browser.MajorVersion < 2)
{
Response.Redirect("http://www.mozilla.com/en-US/firefox/");
}
}
}


Tip #3:

When you develop a site, you generally use tables/divs and spans...if you use .NET's ajax capabilities, you need to wrap everything that should be ajax enabled inside a updatepanel's contentpanel

What happens when you do this is by default is, all html tags gets capitalized and there is the annoying red squiggly lines..

The cause for this error is, in your web.config look for this tag...and change the tagprefix from
default asp to anything else... (Please the opening and closing tags for the following)

(opening tag) controls (closing tag)
(opening tag) add tagPrefix="ajax" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/ (closing tag)

(opening tag)controls / (closing tag)

and now change the to . All your errors should be gone.


Tip #4:

The required field Validators and other .NET validators didnt work for us due to a missing update or hotfix on the server. and we decided to extend the validators by using Validators.dll file we got
from a Microsoft blogger Matt Gibbs.

Link to the page is:
http://blogs.msdn.com/mattgi/archive/2007/01/23/asp-net-ajax-validators.aspx

hot fix:

http://blogs.msdn.com/mattgi/archive/2007/05/12/validators-update-available.aspx


Tip #5:

This piece of code can be used to trap enter key press from any page.

public void SetDefaultButton (TextBox control)
{
control.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}} else {return true}; ");
}


public void FindTextBoxesForEnterKeyPress(Control ParentControl)
{
// Init the name of the control
foreach (Control childControl in ParentControl.Controls)
{
if (childControl is TextBox)
{
SetDefaultButton((TextBox)childControl);
}
else
{
if (childControl.HasControls())
{
// Make a recursive call to loop through the child's children protected void
FindTextBoxesForEnterKeyPress(childControl);
}
}
}
}


Tip #6:
//Used to get the current instance of the scriptmanager and set the focus

//setting focus doesn't work if tried to access it directly due to ajax capabilities

ScriptManager scrMgr = ScriptManager.GetCurrent(this.Page);

scrMgr.SetFocus(this.textbox1.ClientID);

Tip#7:
private void CustomValidationSummary()
{
foreach (Control currentControl in this.Page.Validators)
{
IValidator currentValidator = currentControl as IValidator;
if (currentValidator != null)
{
if (!currentValidator.IsValid)
{
Label currentValidationMessage = new Label();
currentValidationMessage.Text = currentValidator.ErrorMessage;
//Do something with the label here
lblLoginError.Text = lblLoginError.Text + "
" + currentValidationMessage.Text +
";
LabelError.Text = LabelError.Text + "
" + currentValidationMessage.Text+"
";
}
}
}
}


Tip #8

A potentially dangerous Request.Form value was detected
from the client user Page attribute validaterequest = false
and handle it explicitly


A potentially dangerous Request.Form value was detected
from the client (ResultInput="...="1.0"? ResultData
xmlns:xs...").
Description: Request Validation has detected a
potentially dangerous client input value, and processing
of the request has been aborted. This value may indicate
an attempt to compromise the security of your
application, such as a cross-site scripting attack. You
can disable request validation by setting
validateRequest=false in the Page directive or in the
configuration section. However, it is strongly
recommended that your application explicitly check all
inputs in this case.