How to set the default button in content page using asp.net?

hmk picture hmk · Sep 4, 2012 · Viewed 66.5k times · Source

I need to set the content page default button. My code is like this:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent" 
defaultbutton="BtnSearch" defaultfocus="TxtSearchValue">

It is working fine, but my master page menu have one image button like chat, I can press the enter key to fire image button click event but it does not fire default button in content page.

How to handle this type of issue?

Answer

Vishal Suthar picture Vishal Suthar · Sep 4, 2012

1) You simply have to do:

this.Form.DefaultButton = this.btnId.UniqueID;

OR

2) Using Javascript:

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;
      }
  }
}

And from the code behind:

ContentPage.Attributes.Add("onkeypress", "javascript:return 
clickButton(event,'" + btnSearch.ClientID + "');");