2009/03/30

The light bar for the GridView control

Add the following codes to your GridView's RowDataBound event:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    e.Row.Attributes.Add("onmouseover", "currentcolor = this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor = currentcolor");
  }
}
Move your mouse over the GridView control and you will see the result.

[C# 2.0] How to load different culture's date time?

How to load other culture's Date and Time format without changing the Region's setting in the Control Panel? Here is what I did:
//change the culture in order to read the DateTime
DateTimeFormatInfo oDateTimeFormatInfo = (new CultureInfo("zh-tw")).DateTimeFormat;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("zh-tw");
Don't forget to include the namespace:

using System.Globalization;
using System.Threading;

Reference

2009/03/28

Detect .Net Framework (1/19/2010 updated)

Sometimes you need to detect clients' environment in order to run your .net winform or webform application. I ever thought about using the System.Environment.Version, but if user doesn't have any .net framework installed on his/her machine, how can he/she run this code to get the .net version? It turns out that we have to use other technologies to detect the .net framework (like search user's registry or system folders ... etc).
Here is what I found on the Microsoft website, and I did some minor changes on it. You can copy the following code and save it as "DetectDotNet.html".
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
 <head>
  <title>Test for NET Framework</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <script type="text/javascript" language="JavaScript">
  //you can change the version that you want to detect
  var RequiredFXVersion = "2.0.50727";

  function check()
  {
   var foundVer = CheckRequiredFXVersion(RequiredFXVersion);
   if (foundVer != null)
   {
    result.innerHTML = "This computer has the correct version of the .NET Framework: " + foundVer + "." + "<br/>"
     + "This computer's userAgent string is: " + navigator.userAgent + ".";
   }
   else
   {
    result.innerHTML = "This computer does not have the correct version of the .NET Framework.<br/>"
     + "<a href='http://msdn.microsoft.com/windowsvista/default.aspx'>Click here</a> "
     + "to get .NET Framework 3.0 now.<br>"
     + "This computer's userAgent string is: " + navigator.userAgent + ".";
   }
  }

  //
  // Retrieve available versions from the user agent string
  // and check if any of them match the required version.
  //
  function CheckRequiredFXVersion(requiredVersion)
  {
   var userAgentString = navigator.userAgent.match(/\.NET CLR[ .][0-9.]+/g);
   if (userAgentString != null)
   {
    var i;
    for (i = 0; i < userAgentString.length; ++i)
    {
     var ver = userAgentString[i].slice(9);
     if (CheckVersion(requiredVersion, ver))
      return ver;
    }
   }
   return null;
  }

  //
  // Check if a specific version satisfies the version requirement.
  //
  function CheckVersion(requiredVersion, ver)
  {
   requiredVersion = requiredVersion.split(".");
   ver = ver.split(".");

   // Major versions must match exactly.
   if (requiredVersion[0] != ver[0])
    return false;

   // Minor/build numbers must be at least the required version.
   var i;
   for (i = 1; i < requiredVersion.length && i < ver.length; i++)
   {
    if (new Number(ver[i]) < new Number(requiredVersion[i]))
     return false;
   }
   return true;
  }
  </script>
 </head>
 <body onload="javascript:check();">
  <div id="result" />
 </body>
</html>
So next time you can just send this simple html file to clients and ask them to run it under IE.


Updated: 1/19/2010
A better way to check .Net Version: Here.

2009/03/25

ASP.Net: Prevent users from submitting a form more than once (disable submit button)

I found an easy way to prevent users from submitting a form more than once. Here is the code
btnSubmit.Attributes.Add("onclick", "this.disabled=true;" +
  ClientScript.GetPostBackEventReference(btnSubmit, "").ToString());
This will disable the button when the submit button is clicked, and it will automatically enable itself after the page has been postback.

Note:
1.The ClientScript.GetPostBackEventReference(btnSubmit, "") will generate the __doPostBack('btnSubmit', ''); for the btnSubmit button.
2.If you use the UpdatePanel control, make sure you update the the submit button as well.
3.It is not 100% working for all situations, so be sure to test it before applying on your web application.

2009/03/12

A simple way to show a popup window (2)

Last time I show how to popup a window if your website supports AJAX, and this time I am going to show you how to do the same thing if your website doesn't support AJAX.
public static void ShowMessage(Page page, string text)
{
  Literal oLiteral = new Literal();
  oLiteral.Text = "<script>alert('" + text + "')</script>";
  page.Controls.Add(oLiteral);
}
Here is how to use:
protected void btnSave_Click(object sender, EventArgs e)
{
ShowMessage(this, "Success!");
}

2009/03/06

A simple way to check special characters by using Regex

using System.Text.RegularExpressions;

public static string CheckString(string text)
{
  Regex oRegex = new Regex(@"[<;/>]+", RegexOptions.IgnoreCase);
  Match oMatch = oRegex.Match(text);
  oRegex = null;

  if (oMatch.Success)
    return "Please do not input any HTML tag";
  else
    return "";
}
Put any character that you want to check inside the "[]".