December 2003 - Posts - Raj Kaimal

December 2003 - Posts

Setting the default Button for a TextBox in ASP.NET

Hitting the enter key in a TextBox can sometimes have undesired effects like the wrong submit Button being “clicked“. The method described below allows you to specify a default Button to submit when the user hits the enter key in a TextBox.

When you press a key on your keyboard, the js OnKeyPress event is fired. This calls a function to which we pass the id of the button associated with the TextBox. The function gets a reference to the button and simuilates a mouse-click on the Button. We perform a browser detection because IE and Netscape have different event models. The function finally returns false so that the keypress event is cancelled (otherwise a second form submit will be raised). This works with newer versions of IE/Netscape.

function clickButton(e, buttonid){

      var evt = e ? e : window.event;

      var bt = document.getElementById(buttonid);

      if (bt){

          if (evt.keyCode == 13){

                bt.click();

                return false;

          }

      }

}


//code behind
TextBox1.Attributes.Add("onkeypress", "return clickButton(event,'" + Button1.ClientID + "')");

The code behind generates the following code:

<input name="TextBox1" type="text" id="TextBox1" onkeypress="return clickButton(event,'Button1')"  />

This causes web control Button1 to be clicked when the enter key is hit inside TextBox1.

Posted by rajbk | 80 comment(s)
Filed under:

When disabled is not really disabled

While working on a current project, I noticed that unlike a disabled winform TextBox, a disabled webcontrol TextBox can be enabled on the clientside. The following command when entered in the web browsers address field will cause a TextBox named “txtBox1“ enclosed inside a Form “Form1“ to be enabled.

javascript:void(document.Form1.txtBox1.disabled = false)

To fix this, I wrote custom controls that inherits from TextBox, DropDownList, etc.

When my custom TextBox is disabled, the control is not rendered but the text for that control is rendered in place. The same applies to my custom DropDownList...if it is disabled, only the selected item is displayed as text. 

This combined with the Viewstate MAC, SSL, Forms Auth should prevent users from messing with disabled controls. I have also added a check incase someone tried to spoof the postback.

Here are the key methods of the control (I have stripped out other things):

      protected override void Render(HtmlTextWriter writer)
      {
            if (base.Enabled)
            {
                  base.Render(writer);
            }
            else
            {
                  writer.Write(base.Text); //you can use HttpUtility.HtmlEncode if you want to
            }
      }

      //added Dec 10 2003
      //incase someone tried to spoof a postback of the control
      public bool LoadPostData(string postDataKey,
            System.Collections.Specialized.NameValueCollection postCollection)
      {
            string presentValue = base.Text;
            string postedValue = postCollection[postDataKey];
            if (base.Enabled)
            {
                  if (!presentValue.Equals(postedValue))
                  {
                        base.Text = postedValue;
                        return true;
                  }
                  return false;
            }
            else
            {
                  return false;
            }
      }

This also gives the user a clear idea of which fields are editable.
Please feel free to post feedback on this method.

Posted by rajbk | with no comments
Filed under:

Hello .NET

I hope to use this blog to post ideas and feedback about the .NET language and related utilities. Great to be a part of this community. I am learning a lot from the posts here.

Posted by rajbk | with no comments
Filed under:
More Posts