July 16, 2009

In windows applications we can set access key property of a control and the relevant character gets underlined in the control if it exists. Unfortunately, this does not automatically happen in asp.net web controls. Also, I tried to set a access key for a Button control and tried to underline the relevant character. The Button control does not allow inserting of html.

To set the access key and underline the required character - I use the html button element. To produce the same behavior as an asp.net Button control, I set the runat attribute to "server" and use the onclick event to call javascript and onServerClick event to call a server side method.

Here is the code for the page,

<form id="form1" runat="server">

<button id="hbutton1" runat="server" type="submit" accesskey="A" onserverclick="hbutton1_Click" onclick="alert('test');"><u>A</u>dd</button>

</form>


Code-behind,

  protected void hbutton1_Click(object sender, EventArgs e)
        {
            Response.Write("hbutton clicked");
        }


When, the page is run, a button is generated with text "Add". When clicked, at first an alert message is shown and then the page postbacks.

0 comments:

Reference: Shahed Kazi at AspNetify.com