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

只愿长相守 提交于 2019-11-29 09:21:57

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 + "');");

I solved a similar problem with the following code.

Thx: http://www.w3schools.com/aspnet/prop_webcontrol_panel_defaultbutton.asp

<form runat="server">
    <asp:Panel runat="server" DefaultButton="bt1">

        <asp:TextBox runat="server" />
        <asp:Button id="bt1" Text="Default" runat="server" />

    </asp:Panel>
</form>

Wrap all the asp.net controls and buttons inside the Panel and set default button property of panel with the id of a button.

this.Page.Form.DefaultButton = btnSave.ID;

Try this code

protected void Page_Load(object sender, EventArgs e)
    {
        this.form1.DefaultFocus = txtSearch.ClientID;
        this.Form.DefaultButton = btnSearch.UniqueID;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!