ASP Webforms Validation control not working - c#

Please help the regex Validator is not work when I click the button .
<div class="panel-body">
<div class="form-group">
<div class="input-group">
<asp:Label runat="server" ID="lblAddBranch" Text="Branch Name"
CssClass="text-primary small" AssociatedControlID="txtBranchName" />
<asp:RegularExpressionValidator ID="RegExp1"
runat="server" ErrorMessage="Branch Name must be between 3 to 12 characters"
ControlToValidate="txtBranchName" ValidationExpression="^[a-zA-Z]{3,12}$" ValidationGroup="grpBranch" />
</div>
<asp:TextBox runat="server" ID="txtBranchName" CssClass="form-control input-sm" ValidationGroup="grpBranch" />
</div>
<asp:Button runat="server" ID="btnAddBranch" Text="Add" CssClass="form-group-btn btn btn-sm btn-primary"
OnClick="btnAddBranch_Click" ValidationGroup="grpBranch" />
</div>

Code is working perfect. As per your expression its means that a-z and A-z and 3-12 digit will accept. So when you enter only 12 in your text box the validation will be fire.
To check required field validation you need add Requirefield validation control in you code.

Related

Why does not the CommandArgument of a button inside a ModalPopupExtender with AsyncFileUpload work?

I've been trying for a while to find out the reason why the CommandName of a button does not work, in truth I always throw it empty when entering the onCommand event and even onClick of the same button despite putting the property with a value, the strange of the case is that I can not modify the property and putting the property CommandName works without problem the first time, however it is impossible for me to modify its value.
To start I have a ModalPopupExtender together with a Panel which has a series of TextBox and an AsyncFileUpload with a series of buttons, it is necessary to bear in mind that I am working on an ASPX form, son of a MasterPage:
<cc1:ModalPopupExtender ID="mpEditUser" runat="server" PopupControlID="pnEditUser" TargetControlID="lbShowEditUser"
CancelControlID="lbCloseEditUser" BackgroundCssClass="modalBackground">
<Animations>
<OnShown>
<FadeIn Duration="0.40" Fps="30" />
</OnShown>
<OnHiding>
<FadeOut Duration="0.40" Fps="30" />
</OnHiding>
</Animations>
</cc1:ModalPopupExtender>
<asp:Label ID="lbShowEditUser" runat="server" Text="" style="display:none;"></asp:Label>
<asp:Label ID="lbCloseEditUser" runat="server" Text="" style="display:none;"></asp:Label>
<asp:Panel ID="pnEditUser" runat="server" ClientIDMode="Static" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">
Modify User
</h4>
</div>
<div class="modal-body">
<asp:UpdatePanel ID="upEditUser" runat="server">
<ContentTemplate>
<p>Message</p>
<div class="form-group">
<label for="txtEditUser">Code</label>
<asp:TextBox ID="txtEditUser" runat="server" CssClass="form-control" ClientIDMode="Static" Enabled="false"></asp:TextBox>
</div>
<div class="form-group">
<label for="txtEditNameUser">Name</label>
<asp:TextBox ID="txtEditNameUser" runat="server" CssClass="form-control" ClientIDMode="Static" Enabled="false"></asp:TextBox>
</div>
<div class="form-group">
<asp:UpdatePanel ID="upX" runat="server">
<ContentTemplate>
<label class="">CV User</label>
<div class="input-group">
<label class="input-group-btn">
<span class="btn btn-primary">
Upload<cc1:AsyncFileUpload ID="fuCVUser" runat="server" class="form-control" style="display:none;" ClientIDMode="Static" OnUploadedComplete="fuCVUser_UploadedComplete" />
</span>
</label>
<input type="text" class="form-control" readonly disabled>
</div>
<span class="help-block">
<div class="form-inline">
<div class="form-group">
<asp:Button ID="btnDownloadCVUser" runat="server" CssClass="btn btn-default" ClientIDMode="Static" CommandArgument="Test" OnCommand="btnDownloadCVUser_Command" Text="Download" />
</div>
</div>
</span>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="modal-footer">
<asp:UpdatePanel ID="upControlsEditUser" runat="server">
<ContentTemplate>
<asp:Button ID="btnCloseEditUser" runat="server" AutoPostBack="true" CausesValidation="true" Text="Close" class="btn btn-default" data-dismiss="modal" OnClick="btnCloseEditUser_Click" aria-hidden="true" />
<asp:Button ID="btnEditParticipant" runat="server" AutoPostBack="true" CausesValidation="true" Text="Update User" class="btn btn-primary" data-dismiss="modal" OnClick="btnEditUser_Click" aria-hidden="true" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</div>
</asp:Panel>
Event onCommand:
protected void btnDownloadCVUser_Command(object sender, CommandEventArgs e)
{
string x = e.CommandArgument; <- (this value is always "")
}
To get out of doubt, I decided to put the "btnDownloadCVUser" control out of the Panel
............
</asp:UpdatePanel>
</div>
</div>
</div>
</asp:Panel>
<asp:Button ID="btnDownloadCVUser" runat="server" CssClass="btn btn-default" ClientIDMode="Static" CommandArgument="Test" OnCommand="btnDownloadCVUser_Command" Text="Download" />
This worked successfully on the CommandArgument, that is, I could see its assigned value on the label and manage to modify its value.
I tried to put UpdatePanel together with the AsyncFileUpload in such a way that it encloses the "from-group" with the triggers but I keep presenting the same problem. It's very strange because I have a button that does exactly the same thing but it's NOT in a MomalPopupExtender
Why does this happen?
Does ModalPopupExtender or AsyncFileUpload have something to do with the problem?
Does the level of the "form-group" have anything to do with it?
So far the only lifeguard I have is to use ViewState, but I'm still intrigued by the reason that causes my problem :(
Update 01:
I have noticed that the reason seems to be the method with which I load the files (AsyncFileUpload), apparently if I try to change a variable for this it turns out that it is as if I did not, but it allows me to upload the files without problem. Is there any solution?
private int a = 0;
protected void fuCVUser_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
a = 10; < - The value changes to '10' but if I enter the event of another button of the ModalPopupExtender the value is still '0'
}
After seeing this question:
Persisting variable after AsyncFileUpload call to OnUploadedComplete
I have come to the conclusion that it is enough to use a static global variable or Session :)
Apparently the AsyncFileUpload method works with static variables which could justify the problem with CommandArgument

How to apply/remove asp.net required validation for 3 different DIV based on condition?

I have an ASPX UI page in which there are 3 different tabs which are getting submitted one by one
The three tabs are as follows:
tab 1- registration
tab 2- details
tab 3- contact Us
I am using single submit button to submit all 3 tabs (based on condition)
I want to disable validation for other fields in div 2 & 3, when i submit form in 1 div. i.e. in registration div when i click submit other 2 divs must not give validation error. & same while submitting other 2 divs.
<div id=mainDiv>
<ul class="nav nav-tabs nav-justified" id="steps-lap">
<li role="presentation" class="active" id="listOne" runat="server"><asp:HyperLink ID="tabOne" runat="server" NavigateUrl="#tab_one" data-toggle="tab">1: Basic Details</asp:HyperLink></li>
<li role="presentation" id="listTwo" runat="server"><asp:HyperLink ID="tabTwo" runat="server" NavigateUrl="#tab_two" data-toggle="tab">2: Property Details</asp:HyperLink></li>
<li role="presentation" id="listThree" runat="server"><asp:HyperLink ID="tabThree" runat="server" NavigateUrl="#tab_three" data-toggle="tab">3: Finish</asp:HyperLink></li>
</ul>
<div class="tab-content" id="tab-content">
<!-- TAB-1 STARTS-->
<div class="tab-pane fade active in" role="tabpanel" id="tab_one" aria-labelledby="home-tab">
<div id="registrationDetails">
<asp:TextBox ID="txtFullName" runat="server" class="form-control input-md" placeholder="" TabIndex="1" onkeypress="return keyRestrictValidChars(event, 'abcdefghijklmnopqrstuvwxyz');"></asp:TextBox>
<asp:RequiredFieldValidator CssClass="rfv-error" ID="RequiredFieldValidator5" runat="server" ErrorMessage="* required" SetFocusOnError="true" ControlToValidate="txtFullName" ValidationGroup="DetailsGroup"></asp:RequiredFieldValidator>
<asp:TextBox ID="txtLastName" runat="server" class="form-control input-md" placeholder="" TabIndex="1" onkeypress="return keyRestrictValidChars(event, 'abcdefghijklmnopqrstuvwxyz');"></asp:TextBox>
<asp:RequiredFieldValidator CssClass="rfv-error" ID="RequiredFieldValidator1" runat="server" ErrorMessage="* required" SetFocusOnError="true" ControlToValidate="txtFullName" ValidationGroup="DetailsGroup"></asp:RequiredFieldValidator>
</div>
</div>
<!-- TAB-1 ENDS-->
<!-- TAB-2 STARTS-->
<div class="tab-pane fade" role="tabpanel" id="tab_two" aria-labelledby="dropdown2-tab">
<div id="AllContent" runat="server" class="form-group" style="display:block;">
<asp:TextBox ID="txtContact" runat="server" class="form-control input-md" placeholder="" TabIndex="1" onkeypress="return keyRestrictValidChars(event, 'abcdefghijklmnopqrstuvwxyz');"></asp:TextBox>
<asp:RequiredFieldValidator CssClass="rfv-error" ID="RequiredFieldValidator5" runat="server" ErrorMessage="* required" SetFocusOnError="true" ControlToValidate="txtContact" ValidationGroup="DetailsGroup"></asp:RequiredFieldValidator>
<asp:TextBox ID="txtEmail" runat="server" class="form-control input-md" placeholder="" TabIndex="1" onkeypress="return keyRestrictValidChars(event, 'abcdefghijklmnopqrstuvwxyz');"></asp:TextBox>
<asp:RequiredFieldValidator CssClass="rfv-error" ID="RequiredFieldValidator1" runat="server" ErrorMessage="* required" SetFocusOnError="true" ControlToValidate="txtEmail" ValidationGroup="DetailsGroup"></asp:RequiredFieldValidator>
</div>
</div>
<!-- TAB-2 ENDS-->
<!-- TAB-3 STARTS-->
<div class="tab-pane fade " role="tabpanel" id="tab_three" aria-labelledby="dropdown3-tab">
<div id="CompletedContent">
<asp:TextBox ID="txtEmail" runat="server" class="form-control input-md" placeholder="" TabIndex="1" onkeypress="return keyRestrictValidChars(event, 'abcdefghijklmnopqrstuvwxyz');"></asp:TextBox>
<asp:RequiredFieldValidator CssClass="rfv-error" ID="RequiredFieldValidator1" runat="server" ErrorMessage="* required" SetFocusOnError="true" ControlToValidate="txtEmail" ValidationGroup="DetailsGroup"></asp:RequiredFieldValidator>
</div>
</div>
<!-- TAB-3 ENDS -->
<div class="input-group">
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Class="btn btn-default-color btn-sm" Text="Submit" CausesValidation="false" ValidationGroup="DetailsGroup"/>
</div>
</div>
</div>
As shown in the code on page load 1st tab will be active when i submit fields in 1st tab will be saved, will move to 2nd tab, then fields in 2nd tab will be filled & submitted, 3rd tab will be active.
But what happening now is when i submit details of 1st tab, it gives required field validation error because of textfields in other two divs.
Is there any way to handle such condition??
I have switch case in codebehind .cs file, based on which the action on submitted div will be performed.
Note : textbox fields mentioned are for classification purpose only!
Also can i restrict user to go back to previous tab once it is filled up.
i.e. when user fills data in the 1st tab it will go to 2nd tab, also 1st tab will be disabled, user cant go to 1st tab.
Any suggestion will be helpful. thanks!!
You can use the ValidationGroup attibute of the aspnet Validators for that.
<div id="tab1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1"
ValidationGroup="tabgroup1" Display="Dynamic" runat="server"
ErrorMessage="Tab 1 required"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button tab 1" ValidationGroup="tabgroup1" />
</div>
<div id="tab2">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TextBox2"
ValidationGroup="tabgroup2" Display="Dynamic" runat="server"
ErrorMessage="Tab 2 required"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button2" runat="server" Text="Button tab 2" ValidationGroup="tabgroup2" />
</div>
<div id="tab3">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="TextBox3"
ValidationGroup="tabgroup3" Display="Dynamic" runat="server"
ErrorMessage="Tab 3 required"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button3" runat="server" Text="Button tab 3" ValidationGroup="tabgroup3" />
</div>

Asp.net Textbox Empty on PostBack

I am trying to create a Login Page.Whenever, I click on login button username textbox's text becomes empty.
I have tried :
Using Updatepanel didnt work
OnClientClick- it doesnt fires OnClick event
I tried getting value by string usrnm=Page.Request.Form["username"].ToString(); but it gave null value
I tried putting AutoPostBack="true" on textbox
Heres by markUp:
<label for="username">Username</label>
<%--<input type="text" name="Uname" id="username"/>--%>
<asp:TextBox ID="txtusrnm" runat="server" AutoPostBack="true"/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="Pname" id="password">
</div>
<div class="form-group">Forgot password?</div>
<hr class="hr-sm hr-stroke" />
<div class="form-group">
<%--<input id="btnlogin" type="button" class="btn btn-primary btn-wide" value="Login" runat="server" onserverclick="btnlogin_ServerClick" >--%>
<asp:Button ID="btnLogin" runat="server" Text="Login" class="btn btn-primary btn-wide" OnClick="btnLogin_Click" UseSubmitBehavior="false" EnableViewState="true" />
I am able to resolve my issue , i was having AutoPostBack='true' in textbox so just removed and also replace UseSubmitBehavior="false" EnableViewState="true" with CausesValidation="False" , works fine now.
Textbox:
<asp:TextBox ID="txtusrnm" runat="server"/>
and button :
<asp:Button ID="btnLogin" runat="server" Text="Login" class="btn btn-primary btn-wide" OnClick="btnLogin_Click" CausesValidation="False"/>
Thanks

ASP ListView SelectedIndexChanged/Changing Fires in Internet Explorer for All Records ONLY

I have got an ASP ListView, bound programmatically to its DataSource at runtime.
Firefox and Chrome browsers have the issue where ONLY the first record of the ListView is causing the SelectedIndexChanged/Changing events to fire. All other selected records (2nd, 3rd, 4th record etc) are causing a Postback only, without the SelectedIndecChanged/Changing events being fired.
The strange thing, is that Internet Explorer fires the SelectedIndexChanged/Changing events on every record (not just the first record).
Does anyone know what I may be doing wrong?
<asp:ListView ID="lvUsersShoppingCart" runat="server" DataKeyField="ProductID" DataKeyNames="ProductID" OnSelectedIndexChanged="lvUsersShoppingCart_SelectedIndexChanged">
<ItemTemplate>
<div class="top-buffer">
<div class="row">
<div class="col-sm-3">
<asp:Image ID="imgProductThumbnail" class="img-responsive" runat="server" ImageUrl='<%# Eval("ThumbnailImageLocation")%>' />
</div>
<div class="col-sm-9">
<form class="form-horizontal">
<div class="form-group">
<strong>
<asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName")%>' /></strong>
</div>
<div class="form-group">
<asp:Label ID="lblProductShortDescription" runat="server" Text='<%# Eval("ProductShortDescription")%>' />
</div>
<div class="form-group">
$<asp:Label ID="lblProductPrice" runat="server" Text='<%# Eval("ProductPrice")%>' />
(AUD)
</div>
<div class="form-group hidden">
<div class="col-sm-3">
<label for="lblProductQuantity" class="control-label">Quantity</label>
</div>
<asp:Label ID="lblProductQuantity" class="input-sm" runat="server" Text='<%# " x " + Eval("ProductQuantity").ToString%>'></asp:Label>
<asp:Button ID="btnEditQuantity" runat="server" Text="Change Qty" CssClass="btn btn-link" CommandName="Edit" />
</div>
<div class="form-group">
<asp:Button ID="btnDelete" runat="server" Text="Remove" CssClass="btn btn-danger" CommandName="Select" />
</div>
</form>
</div>
</div>
</div>
</ItemTemplate>
</asp:ListView>
The problem is caused by the form element in your ItemTemplate:
<form class="form-horizontal">
...
</form>
You can get the correct behavior if you replace it by another type of container (typically a div):
<div class="form-horizontal">
...
</div>

Why ScriptManager causes postback on RequiredFieldValidator in ASP.NET (Web Forms)?

Before adding the ScriptManager when I was clicking on the login button it was showing the required field warning without page postback but after that it has started to do postback first and then validate fields.
It's required to add a ScriptManager when there is an UpdatePanel on the page.
How can I fix this so it doesn't postback on RequiredFieldValidator?
Login Page
<form id="signinform" runat="server" defaultfocus="username" defaultbutton="LogInBtn">
<asp:scriptmanager id="FormScriptManager" runat="server"></asp:scriptmanager>
<div class="form-signup">
<div class="form-group form-group-info">
<div class="append-icon m-b-30">
<asp:textbox id="username" runat="server" cssclass="form-control c-white form-control-success" placeholder="Username" />
<i class="mdi-action-perm-identity c-light"></i>
<asp:requiredfieldvalidator runat="server" id="UserNameValidator" controltovalidate="username" display="Dynamic" validationgroup="LoginVAL" setfocusonerror="true" cssclass=" f-11 c-red m-b-0" errormessage="The username is required." />
</div>
</div>
<div class="form-group form-group-info">
<div class="append-icon m-b-30">
<asp:textbox id="Password" textmode="Password" runat="server" cssclass="form-control c-white form-control-success" placeholder="Password" />
<i class=" mdi-action-lock-outline c-light"></i>
<asp:requiredfieldvalidator runat="server" id="PasswordValidator" controltovalidate="Password" display="Dynamic" setfocusonerror="true" validationgroup="LoginVAL" cssclass="f-11 c-red m-b-0" errormessage="The password is required." />
</div>
</div>
<div class="togglebutton togglebutton-info">
<label class="c-light normal f-11 m-b-15">
<input type="checkbox" runat="server" name="RememberMe" id="RememberMe" class="md-checkbox">
Remember me?
</label>
</div>
</div>
<asp:placeholder runat="server" id="ErrorMessage" visible="false" viewstatemode="Disabled">
<p id="ErrorMessageContainer" runat="server" class="badge badge-danger m-b-5 f-11">
<asp:Literal runat="server" ID="FailureText" ViewStateMode="Disabled" />
</p>
</asp:placeholder>
<div class="progress-demo">
<asp:linkbutton id="LogInBtn" runat="server" onclick="LogIn" text="Login" cssclass="btn btn-material-indigo btn-block btn-embossed ladda-button" validationgroup="LoginVAL" data-style="zoom-in"></asp:linkbutton>
</div>
<p>
<%-- Enable this once you have account confirmation enabled for password reset functionality--%>
<asp:hyperlink runat="server" id="ForgotPasswordHyperLink" viewstatemode="Disabled">Forgot your password?</asp:hyperlink>
</p>
<div class="modal fade" id="LoginModal" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog">
<div class="modal-content">
<div id="LoginModalHeader" runat="server" class="modal-header bg-aero">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="icons-office-52"></i></button>
<h4 class="modal-title c-white">
<asp:label id="LoginModalTitle" runat="server" />
</h4>
</div>
<asp:updatepanel id="LoginModalUpdatePanel" runat="server">
<ContentTemplate>
<div class="modal-body m-t-10">
<p class=" c-gray w-300 f-13"><asp:Label ID="LoginModalDetails" runat="server" /></p>
</div>
<div class="modal-footer">
<asp:LinkButton runat="server" ID="ResendConfirm" OnClick="SendEmailConfirmationToken" Text="Resend Confirmation" Visible="false" CssClass="btn btn-material-blue-grey btn-embossed" />
<button id="LoginModalCancel" runat="server" type="button" class="btn btn-default btn-embossed" data-dismiss="modal">Cancel</button>
</div>
</ContentTemplate>
</asp:updatepanel>
</div>
</div>
</div>
</form>
I appreciate your efforts in reaching a solution for my problem.
I came across the same issue a while back and was able to resolve it via Page.Validate();.
Basically, before I added ScriptManager everything was working well. When I would click the update button, the field validators would run and prevent a post back until requirements were met. After adding ScriptManager tag, the page would post back rendering my validators pretty much useless.
What I did was add on my update (submit) button click event:
page.validate();
if (Page.IsValid)
{
// Your update or submit code here.
}
This seems to work fine!
Write like this..
<asp:updatepanel id="LoginModalUpdatePanel" runat="server">
<ContentTemplate>
//paste all codes inside here
</ContentTemplate>
</asp:updatepanel>

Categories

Resources