views:

46

answers:

2

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

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 <% =btnName.ClientId %> to refer to the button's name in my aspx page).

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.


I'll try to show our example here:

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.

In my master template (FullMenu.master) I have this panel

<asp:Panel runat="server" ID="pnlFavorite" style="display:inline;"></asp:Panel>

Next there is a class which creates the button and adds it to the panel on the master template:

    public void InsertStarButton()
    {
        CreateStarButton();
        pnl.Controls.Add(bright);
    }

and

    private void CreateStarButton()
    {
        bright = new ImageButton();
        bright.ImageUrl = "~/resources/images/star.png";
        bright.Height = new Unit(12, UnitType.Pixel);
        bright.ID = "btnStar";
    }

What I tried earlier, that didn't work, was in InsertStarButton() I added a line pnl.DefaultButton = "btnStar" 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.

+2  A: 

http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx

Raj Kaimal
This is good, but does not help my problem. As I mentioned in my original post, I have the DefaultButton property set, but the problem still occurs. If I press enter while entering text in a form field, the Imagebutton click event still fires.
kd7iwp
Can you post a sample project that demonstrates the problem?
Raj Kaimal
@Raj Kaimal: I added some of my example code. I wish I could show it in a very clear manner but unfortunately much of this code spans through various interfaces and classes so that there would be far too much code to add to this post.
kd7iwp
Can you create a new web application/website in visual studio that shows the issue and upload it somewhere? I will be more than happy to check it for you.
Raj Kaimal
A: 

Pressing ENTER on a single-line textbox on a form will submit the form. That's how it works.

Joshua
No, it is not submitting my form, it is firing the click event on my image button, which is not inside my form. Please re-read my initial post.
kd7iwp
Oh this one. That's an IE6 bug and the only fix is to use javascript on button press to disable all other buttons.
Joshua
I've never used the page with IE6, only Firefox 3.6.
kd7iwp