asp:Button OnClick appears to override RequiredFieldValidator - asp.net

In asp.net webforms, I have a number of textfields on which I'd like to use the RequiredFieldValidator. In coding them up and testing them, they work beautifully right up to the point that I add an OnClick event to the submitting button. For example:
This works...
<asp:TextBox runat="server" ID="NameTextBox" CssClass="form-control" placeholder="First Name" ValidationGroup="NotifyMe" />
<asp:RequiredFieldValidator runat="server" ID="NameValidator" ControlToValidate="NameTextBox" ValidationGroup="NotifyMe" ErrorMessage="You must enter a name." Display="Dynamic" />
<asp:Button runat="server" ID="SubmitNotificationButton" Text="Let me know..." CssClass="das-link btn btn-default blightblue" CausesValidation="true" ValidationGroup="NotifyMe" />
There is no OnClick tied to the asp:Button, so when it is clicked with an empty field, I get the expected error message. However, when I add an OnClick event, and the button is clicked, regardless of the contents of the NameTextBox, the validation does not happen, and execution of the code moves to the button's OnClick event in the codebehind.
<asp:Button runat="server" ID="SubmitNotificationButton" Text="Let me know..." CssClass="das-link btn btn-default blightblue" CausesValidation="true" ValidationGroup="NotifyMe" OnClick="NotifyButton_Click" />
The code above will fail. What am I doing wrong?

Related

Asp panel Default button fire only specific key enter press

I have an asp panel in page which have many input filed like(textbox,checkbox,select etc)
and two button one for saving(btnSave) the form and the second is for cancel(btnCancel) purpose, and the btnSave is the default button of panel.
The problem occurs when the cancel key get the focus and user press the Enter key, then the default key of asp panel which in this case is save button fire.
<asp:panel DefabultButton="save">
<asp:textbox id="someid" runat="server"></asp:textbox>
<asp:textbox id="someid2" runat="server"></asp:textbox>
<asp:textbox id="someid3" runat="server"></asp:textbox>
<asp:textbox id="someid4" runat="server"></asp:textbox>
<asp:button id="btnSave" run="server" value="Save" />
<asp:button id="btnCancel" run="server" value="Cancel" />
</asp:panel>
is there any way to prevent the default key firing on cancel key Enter press.

Button Click on Enter in asp.net

I have several panels in my page,and each panel has a submit and clearall buttons.
I am trying to set focus of enter to my submit button.
I have an image button in my logo,and everytime user hits enter,its downloading file on imageclick.
Can someone suggest how do i set focus of enter to my submit button.Any ideas?
<asp:Panel ID="Panel2" runat="server" Width="100%" ForeColor="Black" DefaultButton="btnSubmit">
<telerik:RadButton ID="btnSubmit" runat="server" TabIndex="1" OnClick="OnSubmitClick" CausesValidation="true" />
<telerik:RadButton ID="btnclearall" runat="server" OnClick="OnClearAllClick" CausesValidation="false" />
</asp:Panel>
try defaultfocus attribute in <form> tag ,
<form id="form1" runat="server" defaultfocus="btnSubmit" >
Have you tried the UseSubmitBehavior attribute?
<telerik:RadButton ID="btnSubmit" runat="server" TabIndex="1" OnClick="OnSubmitClick" CausesValidation="true" UseSubmitBehavior="true" />
Set the other buttons to false. Technically you shouldn't have to set UseSubmitBehavior to true since it is default but setting all others to false might help.

Multiple ASP.NET validators

I have an ASP.NET page with the following code.
<asp:TextBoxEx ID="CompSCEmail" MaxLength="100" runat="server"></asp:TextBoxEx> <asp:RegularExpressionValidator Display="Dynamic" ID="RegSubmitCompSCEmail" runat="Server" ControlToValidate ="CompSCEmail" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="(*)"></asp:RegularExpressionValidator>
<asp:ImageButton ID="SubmitButton" SkinID="submitButton" OnClick="SubmitButton_Click"
Action="Submit" ValidationGroup="submit" runat="server" VisibleOnRecall="false" />
<asp:ImageButton ID="SaveButton" SkinID="saveButton" OnClick="SaveButton_Click" Action="Save" runat="server" Visible="true" VisibleOnRecall="makerfirst" />
I want to validate the format of the email when the user click both "Submit" and "Save" button. Does anyone know how to do that?
Thank You.
Give your RegularExpressionValidator ValidationGroup Property.
On click of both of your Buttons call Page.Validate("yourvalidationgroup").
This way you can validate email on click of both the buttons.

Disable an asp:RequiredFieldValidator if a checkbox is checked

I have 2 textboxes with required field validators, a button and a checkbox. When the check box is checked I want to disable the required field validator on the second textbox and allow the button to be clicked when the first textbox is completed.
This is generally what my aspx page looks like, although this is just a made up example:
<asp:RequiredFieldValidator ID="rfvName" ControlToValidate="Name" Display="Dynamic" ErrorMessage="Name Required" runat="server" />
Name: <asp:Textbox ID="Name" runat="server"/>
<asp:RequiredFieldValidator ID="rfvID" ControlToValidate="Name" Display="Dynamic" ErrorMessage="ID Required" runat="server" />
ID: <asp:Textbox ID="ID" runat="server"/>
<asp:Button ID="btn" Text="Query" runat="server"/>
<asp:CheckBox ID="chxNoID" Text="I don't have an ID yet" runat="server" />
use javascript. Get the checkbox change event & put the code below in function
var ctrl1 = document.getElementById('<%=rfvID.ClientID%>');
ValidatorEnable(ctrl1, false);

ModalPopupExtender only show on Selected index changed event, not on clicking DropDownList?

I said in my last question and will say in this one too, basically my username reflects my experience!
I currently have a page with 2 listviews, one of which has a number of controls in the InsertItem template.
One of these controls in particuilar is a DDL and I have a Modal Popup Extender hooked up to it. I want to trigger the MPE only when a particular value(not index) is selected. Here is where I am up to now!
DropDownList ExpenseTypeDDL =
(DropDownList) Expenses.InsertItem.FindControl("ExpenseTypeDDL");
int ExpenseType = (Int32.Parse(ExpenseTypeDDL.SelectedValue.ToString()));
if (ExpenseType == 1)
{
AjaxControlToolkit.ModalPopupExtender mpeMiles =
(AjaxControlToolkit.ModalPopupExtender)Expenses.InsertItem.
FindControl("mpeMiles");
mpeMiles.Show();
}
Above are the contents of the DDL SelectedIndexChanged event. This DDL is based on expense types, I want to target a particular value (db primary key) and then display the modal popup so the user can enter their mileage then do some other stuff after.
Here is my mpe
<cc1:ModalPopupExtender ID ="mpeMiles" TargetControlID ="ExpenseTypeDDL"
runat="server" DropShadow="true" PopupControlID="pnlMiles"
BackgroundCssClass="modalBackground" />
<asp:Panel CssClass="modalPopup" ID="pnlMiles" runat="server"
Height="170px">
<div style="padding: 5px; text-align:center">
<asp:Label ID="lblStart" runat="server">Start location.</asp:Label>
<asp:TextBox ID="txtLocationStart" runat="server" />
<asp:RequiredFieldValidator ID="reqLocation" runat="server"
ErrorMessage="You must enter a start location"
ControlToValidate="txtLocationStart" Display="Dynamic" Text="*" >
</asp:RequiredFieldValidator>
<asp:Label ID="lblDestination" runat="server">Destination.</asp:Label>
<asp:TextBox ID="txtDestination" runat="server" />
<asp:RequiredFieldValidator ID="reqDestination" runat="server"
ErrorMessage="You must enter a destination"
ControlToValidate="txtDestination" Display="Dynamic" Text="*" >
</asp:RequiredFieldValidator>
<asp:Label ID="lblMiles" runat="server">Please enter your Mileage</asp:Label>
<asp:RequiredFieldValidator ID="reqMileage" runat="server"
ErrorMessage="You must enter your mileage" ControlToValidate="txtMiles"
Display="Dynamic" Text="*" ></asp:RequiredFieldValidator>
<asp:TextBox ID="txtMiles" runat="server" />
<br />
<br />
<asp:Button ID="btnMiles_OK" runat="server" Text="Save"
CausesValidation="false" />
<asp:Button ID="btnMiles_Cancel" runat="server" Text="Cancel"
CausesValidation="false"/>
</div>
</asp:Panel>
At the moment the mpe shows as soon as the DDL is clicked, but I want that to happen only for the selected value of 1.
Can someone please tell me what I am doing wrong?
TIA
dotnetnewb
This is happening because you have set up the DDL as target of your modal popup extender - so whenever target index is changed, the dialog is shown. The solution is to have an hidden button and make that a target control for modal popup extender - if your DDL has auto-postback as true then your server side code will check the selected index and popup the dialog.
From user experience perspective, unless you have UpdatePanel on page, this would mean on DDL change, page will refreshed and a dialog will be shown. You can also use modal pop-up javascript API to show/hide on DDL selected index change w/o doing post-back. For example,
$find('mpeMiles').show();

Resources