Asp.Net multiple submit buttons, enter submits only the first - asp.net

I have a few submit buttons in my ASP.NET page. Everything works fine, but when a user hits enter button always the first submit button works. I want to fire each submit button with corresponding fields but cannot have multiple forms. Is it possible to group them without using forms?

You don't need forms, just wrap the related controls in their own panels and use the Panel.DefaultButton property.
Use the DefaultButton property to indicate which button gets clicked
when the Panel control has focus and the user presses the ENTER key.
The DefaultButton can be set to the identifier for a Button control or
any control that implements the IButtonControl interface except a
LinkButton control.

Please use DefaultButton property like this
<asp:Panel runat="server" DefaultButton="bt1">
<asp:TextBox runat="server" />
<asp:Button id="bt1" Text="Default" runat="server" />
</asp:Panel>
Please refer here :- ASP.NET DefaultButton Property

Related

Issue with Required Field validation in a asp.net application having two pages workflow

I have a asp.net application with two pages. The first page is having a next button and second page is having a back and submit button. The second page contains text box control with required field validtor.
As per current design, required field validator on 2nd page should get fired on clicking on submit button.
The issue I am having is :- User Fills the first page and then go to second page. Now, user wants to comeback to first page and then go to second page, the required field validator on text box is getting fired and showing up the error message. I dont want this. This should be like - text box should be blank and no error message should be displayed. Error messages should be displayed on clicking on the Submit button on the second page.
I tried couple of options :- 1. putting InitialValue on required field etc.. but no luck.
Could you pls help me out??
The following Markup should solve your problem :
<asp:TextBox ID="tbData" runat="server" ValidationGroup="Submit"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Button" ValidationGroup="Submit" />
<asp:Button ID="btnBack" runat="server" Text="Button" />
<asp:RequiredFieldValidator ID="reqFieldVal1" runat="server" ErrorMessage="Field must be filled in."
ControlToValidate="tbData" ValidationGroup="Submit"></asp:RequiredFieldValidator>
These fields use the ValidationGroup property to set which fields will cause the validation of which validator controls.
You will notice that there is no ValidationGroup property on the btnBack button as this should not cause validation.
For more information on this see the MSDN Article for Specifying Validation Groups

Panel Default button property is not working?

In web application, i place one panel in that one textbox wiht multiline property to true and one button. when i place default button property of panel to button, but it is not working it the textboxe multy line property is the reason? help me,
Check out this example, this might help you:
<div id="LoginForm">
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnCustLogin">
<h1>User Login</h1>
<label>Email ID:</label>
<asp:TextBox ID="txtCustEmailID" runat="server" MaxLength="50"></asp:TextBox>
<asp:Button ID="btnCustLogin" runat="server" Text="Login" OnClick="btnCustLogin_Click"/>
</asp:Panel>
</div>
It is the expected behavior
Pressing the ENTER key with focus inside a multi-line textbox. In a
multi-line textbox, pressing the ENTER key should create a new line in
the textbox which is the expected behavior. In browsers where the
pressing the ENTER key inside a multi-line textbox triggers a post
back but you want the ENTER key to create a new line instead you can
attach a JavaScript function to the input control. The script should
capture the ENTER key and stop the post back. For example, you can use
the Attributes property collection to add client script for the
onKeyPress event.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx

OnTextChanged loses focus when AutoPostBack is true

I have a ASP.Net webform that has several textboxes. Some of the textboxes have an OnTextChanged event with AutoPostBack set to true.
When the user enters some text and leaves the textbox, I want some code to run. This part works fine.
The problem is that if a user enters some text, then clicks or tabs to another textbox, the OnTextChanged of the current textbox event fires fine but the textbox that the user clicked on does not keep focus. This causes problems because the user thinks they are on the next textbox but they aren't. And no object seems to have the focus.
Is there anything that can be done to make the next object keep focus while the OnTextChanged event of the current textbox fires?
One option is to use <asp:UpdatePanel> Control.
Facts about using it:
The postback request would be made via AJAX.
It would not recreate the whole page HTML.
When the UpdatePanel updates, it replaces the DIV innerHTML, that would make the textbox lose focus (bad point).
To maintain the focus, you would have to avoid the UpdatePanel from updating when the textbox posts back.
You can avoid the update by setting the UpdateMode and ChildrenAsTriggers properties.
Here is an example:
<asp:UpdatePanel ID="uppTextboxes" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:TextBox ID="txb1" runat="server" AutoPostBack="true" OnTextChanged="txb1_OnTextChanged" />
<asp:TextBox ID="txb2" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Since the whole page is recreated on postback on serverside and the browser will recreate all html on clientside, you have to tell ASP.NET that your TextBox needs focus.
Use Page.SetFocus:
Page.SetFocus(IdOfControl);
However, i would prefer not to postback at all if i can. Can't you use a button that the user has to click after he has entered all necessary data?

2 user controls on registered on one aspx page not validating validation properly

I have 2 user controls on registered on one aspx page.
UserControl1 us having one text box with require field and one submit button.
UserControl2 is also having one text box with requirefiled and save button.
Expected o/p is-
When I am clicking on any button out of 2(submit or save). Then only related text boxof that user control should be validate.
But the error is
Both text boxes are validate.
Please help me .
Set the ValidationGroup properties to limit which fields get validated when the buttons are pressed.
So for example, if these were contained within the first user control:
<asp:requiredfieldvalidator id="NameValidator"
controltovalidate="NameTextBox"
validationgroup="UserControlOne"
errormessage="required"
runat="Server" />
<asp:button id="Submit"
text="Submit"
causesvalidation="true"
validationgroup="UserControlOne"
runat="Server" />
Clicking the "Submit" button would only cause the validators that have UserControlOne specified as the ValidationGroup to validate.
Edit: When you call Page.Validate() you are validating every group on the page. Call the overloaded Page.Validate(validationGroup) to validate a specific one. e.g. Page.Validate("UserControlOne")

ASP.NET: How to specify which button should defaultly react for an Enter press?

I have a webpage where there are two buttons and a textbox. When the page loads and user presses enter, the first button is clicked - and that is ok.
I would like to assure that if user enters the textBox and types anything there and THEN presses enter, the second button is clicked.
When I enter the textBox I can tell that the first button will be used when I press enter, because it is a bit dark-blue-bordered then.
I tried javascript's
if (event && event.keyCode == 13)
document.getElementById("Button2").click();
but it somehow doesn't work. So my second thought was that there must be a possibility to tell the browser which button should defaultly react for an enter press when I'm in a specific textBox (the dark-blue-bordered button).
Edit: it might matter that first button is input and the second one is asp:Button
Edit2: Page.Form.DefaultButton is not an option, as there are somehow two default buttons. One should be clicked if user doesn't enter a textBox, other one if he is inside the textBox. Changing DefaultButton is not an option as this would require a postback after entering textBox by a user...
The Page.Form.DefaultButton allows to set which button will react to the Enter key.
<asp:Panel runat="server" DefaultButton="Button2" >
<asp:TextBox runat="server" />
<asp:Button ID="Button2" runat="server" Text="Button2" OnClientClick="alert('Button2')"/>
</asp:Panel>
As soon as you start using the textbox - button2 will be the default button. Up until then the other button will be the default
DefaultButton property

Resources