Toars I will smile to the world ^o^

14Aug/090

Disable the history in the TextBox field / disable the AutoComplete for TextBox

When you want type in something in the TextBox field, It is very annoying that sometimes a long history is shown, besides, it is not secure at all, cause the others when using this computer that have the chance to see what you have typed in before.
Sure you want your application has such a drawback and annoys your application users? There are some ways to work around, you can either disable it using JavaScript, or disable it in the code-behind, or set the control property in the Aspx page.

If using JavaScript:

<script type="text/javascript">
    function disableTextBoxHistory(id){
        _obj = document.getElementById(id);
        if (_obj != null && _obj != undefined){
            _obj.setAttribute("autocomplete", "off");
            return true;
        }
        else
            return false;
    }
</script>



If in the code-behind:

TextBox1.Attributes.Add("autocomplete", "off");



In in the Aspx page:

<asp:TextBox ID="TextBox2" AutoCompleteType="Disabled"  runat="server" />



Why the history is shown? There are several reasons:

  • The AutoComplete Setting is turned on by default in some browsers,
  • The ID of the TextBox that shows the history is the same with the one in some pages before you have visited where you have typed in some texts.

What is the solution:
I don't have any good ways of solving it. Acutally, we can not touch the client side broswers to disable this AutoComplete setting. A not-so-smart way is to generate a random control id for the control on the fly.

Tagged as: No Comments
12Aug/090

Disable users goto previous pages

Due to security reasons or some other reasons, we don’t want the users have the ability to go back to previous pages (by clicking the Back button in the browser). It is impossible to disable the Back button directly, thus we have to do something in the code.
There are at least two ways of disabling the users to goto previous pages. One is using JavaScript in the client side, another one is to set the pages that have already expired in the server-side.

In the client side:

<script type="text/javascript">
    function noBack(){
        window.history.forward();
    }

    noBack();
    window.onload = noBack;
    window.onpageshow = function(evt) { if (evt.persisted) noBack(); }
</script>



In the server side:

protected void Page_Init(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
}



The way of using JavaScript in the client side has a drawback. This cannot really forbid the users go back to previous pages when the JavaScript function is disabled in the browser side.

Tagged as: No Comments
7Aug/090

Use JavaScript to access the control Validators in ASP.NET pages

Sometimes, we want to access the Validators such as RequiredFieldValidator and CompareValidator in the client side and do something according to the user’s behavior. This can be done only use JavaScript.

Here is what I did in my application:

<script type=”text/javascript”>¨
   function void myFunction(){
      if (typeof (Page_ClientValidate) == ‘function’){
         Page_ClientValidate();
      }
      if (Page_IsValid){
         // Do something
      }
      else{
           if (!Page_Validator[0].isvalid){
              // Do something
          }
      }
   }
</script>


Tagged as: No Comments