disable default behaviour of Internet Explorer - c#

I am using asp.net C#.
I have one html input field, and 3 image buttons (asp.net controls)
ImageButton 1 = perform delete operation
ImageButton 2 = perform Edit operation
ImageButoon 3 = perform duplicate operation
My problem occurs when the user presses the enter key when the input field is in focus.
When he presses the enter key the browser automatically fires by default the next button.
In my case it is the delete operation.
How can I prevent this?

Configure DefaultButton property properly.

you can also hide a button at the top of your page/form that will attract the attention of IE when you hit enter.
<button style='position:absolute;left:-1000px'>IE sux</button>
that button won't display on the screen but IE will click it whenever you hit enter. since it has no action on click, nothing will happen.
note: only works in IE9. perhaps useful with the default button option above.

As mentioned above, use the form's defaultbutton property or it you use a panel use that of the panel.
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton(VS.80).aspx

Related

Omit action when pressing Enter on button

Is there a way to omit the (whatever) performed action on a button when the key is pressed? I already got some good inputs from SO and tried to catch the event via OnKeyPress or OnKeyDown - but I could not omit the Enter-action. I also liked the idea of disabling the Tabstop property, but as soon as I click the button the problem reoccurs.
OnKeyDown only seems to fire when "casual" keys (like a or z) are pressed, but not when Enter is hit. How can I detect it - when Enter is hit - on the button itself?
When you press ENTER inside a non-multiline field on a form with a default button set, the click event for this button is fired, like a click, not like a keyevent (keypress, keyup, keydown, etc).
Form.AcceptButton
Gets or sets the button on the form that is clicked when the user
presses the ENTER key.
If you don't want this behaviour (or want to prevent it), maybe you should remove it as the default button.
But if you need it to be default only in certain situations, you can bind/unbind it programatically:
form1.AcceptButton = btnSave;
form1.AcceptButton = null;
You can use : if(e.KeyCode == Keys.Enter){}
Check the Button.IsDefault property
Well, the fact that
When you press ENTER [...] the click event for this button is fired
made me think about the use of the OnMouseUp(MouseEventArgs e) method instead of OnClick(EventArgs e). I gave it a shot and it totally fits my requirements - plus: it gives me the opportunity to only exclude the wrapped buttons from said issue, not involving any other controls in my application (like Form.AcceptButton).
Thanks for all your input, which made me think about the issue from another perspective!

Control default action in ASP on Enter

This may be an easy question for you guys.
I have a web form with several buttons and only one textbox. When I enter a value in the textbox and press enter the method gets executed that is wired to the first top left button. I need to control what button's method gets executed (in this case it is a button in a whole different location that does db query).
I tried to alter tab index to have my desired button the lowest and I tried to pass focus to my desired button on page_load (like I would have done in Win Forms) but still if I enter text in textbox and hit eneter, the top left button's method gets executed.
I could put conditions in execution of top left button's method but that would be a workaround not a solution. So how do I control that behavior?
ASP.NET form has a defaultbutton property that specifies which button is clicked when ENTER is pressed. Just specify your button ID there. E.g.
<form id="form1" runat="server" defaultbutton="MyDbQueryButton">

pressing Enter on Dropdownlist causing postback

My application(asp.net) has few dropdowns which are dynamically created and all of them were AutoPostBack="false". if I select Item from dropdown and press enter it makes auto postback in the application. I tried to handle this issue OnKeydown event , but No Use.
Could some one help on the same .
Thanks
Pavan
Pressing submit button triggers the first button(forms default button) on the page.
Set UseSubmitBehavior="False" on your Buttons. This disables the "AutoPostback" on Enter.This prevents Postbacks on Enter completely.
Setting AutoPostBack="false" on a drop down list means it won't post back when you change the selection, the first button in an ASP.NET form is usually the default option so by pressing return you are probably invoking your forms submit button.
To avoid the postback when pressing enter inside a dropdown list it shuld be enough to add
<asp:DropDownList ...... onkeydown="return (event.keyCode!=13)"/>
But you will have se same problem if a user presses the enter key inside every control of the page. to disable the enter key on the entire page this is the code
<body onkeydown = "return (event.keyCode!=13)">
Can you please provide us with your code ? It would be easier to know which control is causing the postback by looking at your code.
My best guess is that you might have an asp.net button having the property "UseSubmitBehavior" set to true so when you are pressing enter the button is what is causing the postback.
To investigate further you can add this line of code to your Page_Load event to detect which control is causing the postback
if(IsPostback){
var controlName = page.Request.Params["__EVENTTARGET"];
}
Please let me know should you need any further clarification. :)

Postback on a submit button

I have a page that has 4 tables. Initially when the page is loaded, it shows 1 & 2. Thats working fine. On Post back(When Submit is clicked), it should show 3 & 4. Even thats working fine(code shown here). When the submit is clicked again, it has to call updatePaymentInfo() and redirect. Is there something to write as a condition to call UpdatepaymentInfo() because when submit is clicked, it is taking as an other postback and showing me 3 & 4 again.
protected void imgbtnSubmit_Click(object sender, ImageClickEventArgs e)
{
try
{
if (Page.IsPostBack)
{
trtest.Visible = false;
trCCandBilling.Visible = true;
trtest2.Visible = true;
}
else
{
UpdatePaymentInfo();
Response.Redirect(ApplicationData.URL_MERCHANT_ACCOUNT_HOME, true);
}
}
}
My thought on the easiest way to do this is to have two image submit buttons in the same place. Button A is the one you already have button B is a new one that whose submit handler runs UpdatepaymentInfo and redirects.
Button B starts off invisible while button A is visible. When Button A is clicked in addition to the visibility changes you hide button a and show button B. Then when they click button B the right stuff happens.
Its not that elegant though.
Another solution might be storing values in the page to indicate the current page state that you can then check on button click.
It sounds like you're having trouble managing the current state of your page. You could try:
Having a second submit button. It would be stylistically indistinguishable from the first, and would be hidden/shown accordingly, but would have its own click event.
Placing a hidden form value on the page to track the current "step" of the process.
Breaking the page into two pages, since from the user's perspective it's clearly a two-page process.
My personal favorite, move to MVC :) Though it's understandable if you're stuck in a pre-existing WebForms app and there's just no budget to re-write it.
I guess that imgbtnSubmit_Click handles Click event of the Submit button so this method will be called only during the postback so the condition is incorrect.
I would not use this approach. ASP.NET contains controls which support these requirements. Check MultiView and Wizard. Create separate view with table 1 & 2 and button and another view with table 3 & 4 and button. Button on the first view will switch the view and button on the second view will call the method and redirect.
Another possible way to do this is keep your current set up and add a command argument to the button. By default it has some argument that you check on the first click. Then checking the command argument on the first click you do your showing and change the command argument to be something different. So on the next button click you do the work associated with the second command argument. Thus flipping the work done without having to hide or show a new control.

Windows Forms: Multiple default buttons?

In a simple Guess-The-Number game I'm making, the user inputs a couple of numbers and hits a button. A second panel becomes enabled and the original one is disabled. The user now enters a number and hits another button, multiple times. Since the two panels will never be enabled at the same time, I'd like for both buttons to be "default", i.e., they will be pushed if Enter is pressed. Is this possible? Apparently, I can only set one of these per window.
A window in Windows by definition has only one default button. This is a functionality intended for dialog windows where you would never disable the default button.
What you can do instead is to switch the default button when you disable one panel.
Another option would be to throw out default buttons altogether and use KeyPreview on the form and handle the enter key yourself, sending it to the appropriate button that is currently active.
Nope, no way to accomplish that that I know of. You'll have to switch the default button when one button is pressed to the next button.
You can only have one default button per window. However, this can be changed at runtime, so when you activate a specific panel, you can make it's button the default one at that point.

Categories

Resources