How to set the default button in content page usin

2020-02-11 18:36发布

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?

5条回答
神经病院院长
2楼-- · 2020-02-11 19:04

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 + "');");
查看更多
闹够了就滚
3楼-- · 2020-02-11 19:08

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>
查看更多
在下西门庆
4楼-- · 2020-02-11 19:08

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

查看更多
欢心
5楼-- · 2020-02-11 19:17

Try this code

protected void Page_Load(object sender, EventArgs e)
    {
        this.form1.DefaultFocus = txtSearch.ClientID;
        this.Form.DefaultButton = btnSearch.UniqueID;
    }
查看更多
趁早两清
6楼-- · 2020-02-11 19:23
this.Page.Form.DefaultButton = btnSave.ID;
查看更多
登录 后发表回答