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

5

18

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?

Lem answered 4/9, 2012 at 11:57 Comment(3)
You should be able to use the following resource weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx to solve this problemOligopsony
It's an error focus not the Default button..@bUKaneerAbdias
possible duplicate of ASP.NET Master page DefaultButton overrideHaddock
A
39

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 + "');");
Abdias answered 4/9, 2012 at 12:3 Comment(7)
this.Form.DefaultButton = this.btnId.UniqueID; i wrote this code problem is press enter key it always fire one button , press tab button to focus another button click enter button not working , press space button it working fine , but i have to press enter buttonLem
you can try the second solution for this..It should work..@LemAbdias
where i can write ContentPage.Attributes.Add , i mean master page code behind or content page code behindLem
In Page_Load event of content load and also the javascript function..@LemAbdias
i can write the contentpageid.attributes it have shown error Doesnt Exit in content page load eventLem
use this.Form instead of contentpageidAbdias
it is also same problem , press enter button always fire btnSearch event ,i can change the focus to another button using tab like another button cancel press enter that time also fire btnsearch event but i have cancel button event please give me suggestion suthar..Lem
B
11

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>
Beading answered 9/10, 2013 at 11:3 Comment(1)
The problem is with Master pages and content panels. This is ignored.Doucet
L
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.

Landin answered 9/5, 2013 at 7:51 Comment(0)
V
5

Try this code

protected void Page_Load(object sender, EventArgs e)
    {
        this.form1.DefaultFocus = txtSearch.ClientID;
        this.Form.DefaultButton = btnSearch.UniqueID;
    }
Variant answered 4/6, 2015 at 10:31 Comment(0)
S
2
this.Page.Form.DefaultButton = btnSave.ID;
Smalto answered 6/3, 2014 at 5:35 Comment(1)
The above should use btnSave.UniqueID if there are containers involved (e.g. master pages).Avellaneda

© 2022 - 2024 — McMap. All rights reserved.