Jun 22, 2010

Page.IsValid and Validate

ASP.net declarative programming approach, the Page.IsValid method is something easy to forget.



<
asp:TextBox ID="TextBox1" runat="server" ValidationGroup="MyValidationGroup">asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="This 
field is required!" ValidationGroup="MyValidationGroup">asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" 
ValidationGroup="MyValidationGroup" CausesValidation="true" />
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server" ValidationGroup="AnotherValidationGroup">asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
ErrorMessage="This 
field is required!" ValidationGroup="AnotherValidationGroup">asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="AnotherValidationGroup" 
OnClick="Button2_Click" /> 
 
protected void Button2_Click(object sender, EventArgs e) {
 
    Page.Validate("MyValidationGroup");
 
    if (!Page.IsValid) {
         return;
    }
 
    Response.Write("Button was clicked at " + DateTime.Now.ToShortTimeString());
} 

Jun 19, 2010

Don't redirect after setting a Session variable (or do it right)

A problem I see over and over again on the ASP.NET forums is the following:
In a login page, if the user and password have been validated, the page developer wants to redirect to the default page. To do this, he writes the following code:

Session["Login"] = true;
Response.Redirect("~/default.aspx");

Well, this doesn't work. Can you see why? Yes, it's because of the way Redirect and session variables work.

When you create a new session (that is, the first time you write to a Session variable), ASP.NET sets a volatile cookie on the client that contains the session token. On all subsequent requests, and as long as the server session and the client cookie have not expired, ASP.NET can look at this cookie and find the right session.
Now, what Redirect does is to send a special header to the client so that it asks the server for a different page than the one it was waiting for. Server-side, after sending this header, Redirect ends the response. This is a very violent thing to do. Response.End actually stops the execution of the page wherever it is using a ThreadAbortException. What happens really here is that the session token gets lost in the battle.

You can do to solve this problem.

you can do is use the overloaded version of Redirect:
Response.Redirect("~/default.aspx", false);
This does not abort the thread and thus conserve the session token. Actually, this overload is used internally by RedirectFromLoginPage. As a matter of facts, I would advise to always use this overloaded version over the other just to avoid the nasty effects of the exception. The non-overloaded version is actually here to stay syntactically compatible with classic ASP.

Jun 5, 2010

Enum bind into dropdownlist using HasTable

You can create a method to help you convert your enum into a hashtable and then bind it to the ddl:



protected void Page_Load(object sender, EventArgs e)
{
Hashtable ht = GetEnumForBind(typeof(Salutations));
DropDownList1.DataSource = ht;
DropDownList1.DataTextField = "value";
DropDownList1.DataValueField = "key";

DropDownList1.DataBind();

}

public Hashtable GetEnumForBind(Type enumeration)
{
string[] names = Enum.GetNames(enumeration);
Array values = Enum.GetValues(enumeration);
Hashtable ht = new Hashtable();
for (int i = 0; i < names.Length; i++)
{
ht.Add(Convert.ToInt32(values.GetValue(i)).ToString(), names[i]);
}
return ht;
}