pressing Enter on Dropdownlist causing postback - c#

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

Related

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">

Change Enter for Tab

I need to make the Enter key as Tab, changing the focus of the controls.
I tried several methods of javascript and it worked well... but in some ModalPopupExtender when i click in enter, it closes the PopUp, or in other cases that have a gridview with TextBox inside, by clicking enter it also closes a PopUp or generates a postback.
Someone have any solutions?
Thanks for the help!
I fix the problem with this code in Page_load:
GridView.Attributes.Add("onkeydown", "if(event.keyCode==13)return false;");
Thanks!

Is this a good workaround for enter button submitting in a text box?

So I have a page, it had multiple textboxes, linkbuttons, and buttons. Currently, a user clicks a linkbutton "Edit" which allows them to enter data in that row, via textboxes. However, upon pressing enter, the event for one of the buttons fired...this is not desired at all. Apparently, this is the default behavior built in?
Is it possible to associate each textbox so that pressing enter will act on a specific assigned linkbutton or button, without the textboxes being in separate forms?
If this is not possible, I'm going to try and create a button and make it the default form button, but disable and hide it, so that pressing enter submits nothing.
Thanks!
You can set the UseSubmitBehavior property to "false" on a Button control. This will cause the button to be rendered as an <input type="button" /> tag instead of an <input type="submit" /> tag. As a result, pressing the Enter key will not cause the button to be pressed, unless the button actually has the focus.
<asp:Button runat="server" UseSubmitBehavior="false" ... />
You can override the KeyPress event for each textbox to catch the enter key and then fire the code for the corresponding button.
if(e.KeyChar == '\n')
// execute button code
you can put them in separate panels and use the defaultbutton attribute to tell the panel which button to use when enter is pressed.

disable default behaviour of Internet Explorer

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

Asp.net is it possible to check page_load a click event is triggered?

When a button is clicked, I would like to check whether a button is clicked in my page_load. Is this possible? I am using asp.net 2.0 C#
You can check the IsPostBack flag to see if it was a postback rather than an initial load. This may be what you're after. Also, you can check the Request.Form["__EVENTTARGET"] from which you can obtain information about the control that raised the event and therefore find out if it was one of your buttons from there.
The button click event will fire after the page load event has. That being said, you can always check the http header to see what value is being pushed back through the request.form event. The button id will be in there if it has been fired.
I can think a work around by creating a hidden field and changing the value when a certain button is clicked and checking it value in Page_Load.

Categories

Resources