如何使用asp.net在内容页面中设置默认按钮?

时间:2021-10-05 02:07:19

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

我需要设置content page默认按钮。我的代码是这样的:

<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.

它工作的很好,但是我的master page菜单上有一个image按钮,比如chat,我可以按下enter键来触发image按钮点击事件,但是在content page中它没有触发默认按钮。

How to handle this type of issue?

如何处理这类问题?

5 个解决方案

#1


37  

1) You simply have to do:

你只需要做:

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

OR

2) Using Javascript:

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

#2


10  

I solved a similar problem with the following code.

我用下面的代码解决了一个类似的问题。

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

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>

#3


8  

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

将所有asp.net控件和按钮封装在面板中,并设置带有按钮id的面板的默认按钮属性。

#4


2  

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

#5


2  

Try this code

试试这个代码

protected void Page_Load(object sender, EventArgs e)
    {
        this.form1.DefaultFocus = txtSearch.ClientID;
        this.Form.DefaultButton = btnSearch.UniqueID;
    }

#1


37  

1) You simply have to do:

你只需要做:

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

OR

2) Using Javascript:

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

#2


10  

I solved a similar problem with the following code.

我用下面的代码解决了一个类似的问题。

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

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>

#3


8  

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

将所有asp.net控件和按钮封装在面板中,并设置带有按钮id的面板的默认按钮属性。

#4


2  

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

#5


2  

Try this code

试试这个代码

protected void Page_Load(object sender, EventArgs e)
    {
        this.form1.DefaultFocus = txtSearch.ClientID;
        this.Form.DefaultButton = btnSearch.UniqueID;
    }