Reporting Errors in Login Control

Login control has a FailureText property that holds the text to display to the user when a login attempt fails.


What if you want to report errors before making an login attempt? For example, you may handle the LoggingIn event to check a “IsDeleted” flag on user record and, if it is set, provide an error “The user is inactive”. How can you set the error text without making an login attempt?


This code will do that:

protected void LoggingIn(object sender, System.Web.UI.WebControls.LoginCancelEventArgs e)

{

string userName = this.Login1.UserName;

// validate the user

if (IsUserDeleted(userName))

{

// deleted, cancel login

e.Cancel = true;

// notify the user

(this.Login1.FindControl(“FailureText”) as System.Web.UI.WebControls.Literal).Text = String.Format(“The user {0} is inactive”, userName);

}

}


 

Customizing web.config for different environments

This is a very common scenario.


You develop your application in environment A. The production will run in environment B. There will be also a demo site in environment C and a test site in environment D. The environments may have different connection strings, account identities, custom application settings, whatever else you may or may not think of. You clearly need to give to every environment its own web.config file. Or modify the setting in run time depending on what environment the application is running in.


On other hand, your web.config includes sections common for all environments and you wouldn’t like to maintain the same setting in four places. Another issue is that, when you publish the site, the development web.config is going to overwrite the target one that already has been customized for the target environment.


There is a simple solution with no coding involved. Make use of the configSource property for the sections you need to customize.

The configSource is basically the name of the include file in which the associated section is defined. Leave in the web.config only the sections common for all environments and take all variable sections out to separate files. For examle, the connectionString section in the web.config file will look like this: 

<connectionStrings configSource=SiteSettings\connectionStrings.config/>


Note that the configSource is set to the physical location of the file relatively to the location of the web.config.The content of the connectionStrings.config will be:

<?xml version=1.0 encoding=utf-8 ?>


<!– This file is an extension of the application web.config file. The root element must be <connectionStrings>.–>

<connectionStrings>

<add name=SiteSqlServer connectionString=Integrated Security=SSPI;database=DbDemo; />

</connectionStrings>


You can make separate config files for as many sections as you wish. Put them all in a single directory. In our case it is SiteSettings.


Now, in our example with four environments you would make four sets of section config files in four directories and deploy each one to the corresponding environment. It is important not to include any of the config section directories into your solution. This will ensure that you won’t overwrite the correct settings on the target site with your development ones on every publishing.


Now you can modify the common sections of your web.config and publish it to all targets. The targets will keep their customized config sections intact.


 

VS2008 freezes after builds

Recently my Visual Studio started freezing for 1-3 minutes after every build, no matter successful or not, debug or release. Save All operation also became very slow. This was extremely annoying. Eventually, I found the culprit. My solution .suo file grew quite big. I just deleted it and all delays gone.

Resetting ChangePassword control

If you try using the ChangePassword control for changing several passwords on the same page, you will notice that after confirming successful password change the control always shows the same success message and doesn’t offer any text entry fields any more. The reason is that the control maintains an internal state machine and doesn’t reset it properly after succes password changes. The state machine is controlled by internal property CurrentView that is not available for direct setting. But you can set it using reflection like this:

private void resetChangePasswordState(System.Web.UI.WebControls.ChangePassword cpControl)

{

System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic |

System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static;

System.Type type = cpControl.GetType();

System.Reflection.PropertyInfo p = type.GetProperty(“CurrentView”, flags);

System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(p.PropertyType);

object result = converter.ConvertFrom(“ChangePassword”); p.SetValue(cpControl, result, null);

}

Data Entry: Use Enter as Tab

A simple javascript will make your Enter key work as Tab. It is useful in large tabular forms.


function enterToTab(){
  if (event.keyCode==13)
    event.keyCode=9;
  }


<input type=”text” onkeydown=”enterToTab()”>


Note using onkeydown event. Onkeypress won’t do the trick.

Silverlight is coming: c# calls from javascript and more…

Soon you won’t need to go to the server to treat your client-site javascript with some c# calls.  Watch this technology, it looks promising:


Silverlight.


Scott Guthrie:


Silverlight opens up a ton of opportunities to build significantly richer client experiences that run cross platform and cross browser. 
For .NET developers, it means that you can now write .NET code using any development language (VB, C#, Javascript, Python, Ruby and more) in the web-browser (using Silverlight), web-server (using ASP.NET), in desktop applications, and with mobile devices.  You’ll be able to use great Visual Studio developer tools and Expression Studio designer tools to target each of these experiences.

Table height:100%

Ever tried to get a <table> to take the whole browser height? Not a trivial task if you want to keep the browser in the Strict mode. Yes, removing

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

will do, but that’s already the Quirks mode.


This css rule will work even in the Strict mode:


html, body, table#main {height: 100%}