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

后端 未结 5 684
悲哀的现实
悲哀的现实 2020-12-18 20:29

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



        
相关标签:
5条回答
  • 2020-12-18 21:01

    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 + "');");
    
    0 讨论(0)
  • 2020-12-18 21:07

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

    0 讨论(0)
  • 2020-12-18 21:18
    this.Page.Form.DefaultButton = btnSave.ID;
    
    0 讨论(0)
  • 2020-12-18 21:19

    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>
    
    0 讨论(0)
  • 2020-12-18 21:21

    Try this code

    protected void Page_Load(object sender, EventArgs e)
        {
            this.form1.DefaultFocus = txtSearch.ClientID;
            this.Form.DefaultButton = btnSearch.UniqueID;
        }
    
    0 讨论(0)
提交回复
热议问题