Note that there are some explanatory texts on larger screens.

plurals
  1. POPreventing a button from responding to 'Enter' in ASP.net
    primarykey
    data
    text
    <p>I have a master template that all the pages on my site use. In the template there is an empty panel. In the code-behind for the page an imagebutton is created in the panel dynamically in my Page_Load section (makes a call to the DB to determine which button should appear via my controller). </p> <p>On some pages that use this template and have forms on them, pressing the Enter key fires the click event on this imagebutton rather than the submit button in the form. Is there a simple way to prevent the imagebutton click event from firing unless it's clicked by the mouse? I'm thinking a javascript method is a hack, especially since the button doesn't even exist in the master template until the button is dynamically created on Page_Load (this is ugly since I can't simply do <code>&lt;% =btnName.ClientId %&gt;</code> to refer to the button's name in my aspx page).</p> <p>I tried setting a super-high tabindex for the image button and that did nothing. Also set the button to be the DefaultButton in its panel on the master template but that did not work either. Moreover, I don't want to add a property to all of my pages that use this template (there are hundreds). It would be optimal to find a solution that works globally from my master template.</p> <hr> <p>I'll try to show our example here:</p> <p>We have a button on the top of each page in our system that lets you star the page as one of your favorites, sort of a server-side bookmark system. When the page loads it looks to see if the page is one of your favorites or not and then shows a gold star if it is, and a gray star if it is not. Clicking the imagebutton of a star toggles the page favorite status.</p> <p>In my master template (FullMenu.master) I have this panel</p> <pre><code>&lt;asp:Panel runat="server" ID="pnlFavorite" style="display:inline;"&gt;&lt;/asp:Panel&gt; </code></pre> <p>Next there is a class which creates the button and adds it to the panel on the master template:</p> <pre><code> public void InsertStarButton() { CreateStarButton(); pnl.Controls.Add(bright); } </code></pre> <p>and</p> <pre><code> private void CreateStarButton() { bright = new ImageButton(); bright.ImageUrl = "~/resources/images/star.png"; bright.Height = new Unit(12, UnitType.Pixel); bright.ID = "btnStar"; } </code></pre> <hr> <p>What I tried earlier, that didn't work, was in <code>InsertStarButton()</code> I added a line <code>pnl.DefaultButton = "btnStar"</code> but that didn't change anything on the page. Even if I had the cursor blinking inside a text box in the form on the page the star button would fire its click event if the Enter key was pressed.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload