Control default action in ASP on Enter - c#

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

Related

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

popup window according to CheckBox checked

If user don't checked, I want to show asp:ModalPopupExtender and get numeric value from this form.
I'm using asp.net wizard control. I don't know "finish" button id. Can somebody help me?
You should set the target of the ModalPopupExtender as a dummy control, i.e., a hidden Button, LinkButton,... that never is going to be clicked by the user.
The Wizard control have a method called FinishButtonClick. Here is where you have to check the state of the CheckBox and show or not the popup calling to the method Show() of the ModalPopupExtender. You also can call to the Click() method of the hidden control or do it with JavaScript usign the BehaviourID of the ModalPopupExtender. Your choice.
Cheers!
To get "Finish" button id, just open the page in browser & view the rendered html code (Right Click -> View Source). From their you can get the finish button id.
And after getting "Finish" button id, you can easily associate a Client-Side event to do the required job.

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

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.

Preventing a button from responding to 'Enter' in ASP.net

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.
http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx
Pressing ENTER on a single-line textbox on a form will submit the form. That's how it works.

Categories

Resources