CausesValidation=true not working - c#

I have used a Requiredfieldvalidator to validate a text field however when I click on button the page gets posted back even if validation fails. What am i doing wrong? Below is my code.
<div id="addFactDiv" style="display:none;">
<asp:TextBox ID="inputFact" Height="50px" Width="100%" runat="server" TextMode="MultiLine" placeholder="Enter a fact"></asp:TextBox>
<asp:requiredfieldvalidator CssClass="validator" Display="Dynamic" ID="Requiredfieldvalidator5" ControlToValidate="inputFact" ValidationGroup="ValidateFact" ErrorMessage="Please Enter a fact" runat="server"/>
<input type="button" Class="cancel" value="Cancel" OnClick="hideInput('addFactDiv');" />
<asp:Button ID="Submit_Fact" CssClass="submit" runat="server" CausesValidation="true" ValidationGroup="ValidateFact" Text="Submit" OnClick="Submit_Fact_Click"/>
</div>
EDIT
I had some commented out fields as well on the page causing the two errors I have mentioned in comments. When I removed those fields it started working fine. However I am not sure why.. Do commented out fields also get rendered on the page the same way as others?

I tried on my sample application and found below error,
WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).
Later I found that we need to add below key in .config file, which fixed the issue. (I am not sure why!)
Ref: Link
My aspx code looks like below,
<form id="form1" runat="server">
<div>
<div id="addFactDiv" style="float: right">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</div>
<p>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="test" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator" ValidationGroup="test"></asp:RequiredFieldValidator>
</p>
</form>
Hope this might help!!

Related

Visual studio asks for runat server tag on a component that alreayd has it

TextBox1 must be placed inside a form tag with runat=server is the error I get, my code is as follows.
<div >
<asp:Label ID="Label1" runat="server" Text="User"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" class="form-control"></asp:TextBox>
</div>
I was wondering why I get this error, and how I can fix it.
It's asking for a <form> element:
<form runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="User" />
<asp:TextBox ID="TextBox1" runat="server" class="form-control" />
</div>
</form>

How do I go about accessing the text input from a textbox within a content template in my C# code behind file?

I have used a few variations of accessing the text from StreetNbrtxt including
1) Directly: Profile.StreetNbr = StreetNbrtxt.Text; (Didn't work)
2) Using FindControl: Profile.StreetNbr = ((TextBox)RegisterUserWizardStep.FindControl("StreetNbrtxt")).Text; ('Not a instantiated class' Error message)
Further, I have not been able to see the values that I added in the text boxes that were added to the pre-coded files that were in the Account folder in Visual Studio Express 2012.
The code I have from the Register.aspx is as follows:
<asp:CreateUserWizard runat="server" ID="RegisterUser" ViewStateMode="Disabled" OnCreatedUser="RegisterUser_CreatedUser" ActiveStepIndex="0">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="wizardStepPlaceholder" />
<asp:PlaceHolder runat="server" ID="navigationPlaceholder" />
</LayoutTemplate>
<WizardSteps>
<asp:CreateUserWizardStep runat="server" ID="RegisterUserWizardStep">
<ContentTemplate>
<p class="message-info">
Passwords are required to be a minimum of <%: Membership.MinRequiredPasswordLength %> characters in length.
</p>
<p class="validation-summary-errors">
<asp:Literal runat="server" ID="ErrorMessage" />
</p>
<fieldset>
<legend>Registration Form</legend>
<ol>
<p>
<asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
<asp:TextBox runat="server" ID="UserName" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
CssClass="field-validation-error" ErrorMessage="The user name field is required." />
</p>
:
:
:
:
:
<p>
<asp:Label ID="StreetNumberlbl" runat="server" AssociatedControlID="StreetNbrtxt">Street Number:</asp:Label>
<asp:TextBox ID="StreetNbrtxt" runat="server" CssClass="style11"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="StreetNbrtxt"
CssClass="failureNotification" ErrorMessage="A street address number is required" ToolTip="Please insert your home address number"
ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
</ol>
<asp:Button runat="server" CommandName="MoveNext" Text="Register" />
</fieldset>
</ContentTemplate>
Any Idea of how I should access the Street Number from the user input textbox?
CreateUserWizard Control is based on Wizard control, which has steps. So you need to find the control inside the step. In your case, it is in the first step. You can access the control like this.
TextBox StreetNbrtxt =
(TextBox)RegisterUser.WizardSteps[0].
FindControl("CreateUserStepContainer").FindControl("StreetNbrtxt");
You can see how it works. I hope this works for you too, as it is working for me for a long time. Thanks!
Edit: Here is the link for more reading about wizard control at official website.

<asp:PlaceHolder> is not showing on iphone

I have a code in my master page that I used placeholder to show and hide 2 different parts.It is completely works fine in all the Browsers, but it is not working on Iphone Even it is working on Android.
this is my code:
<div class="actions">
<asp:PlaceHolder ID="phLogout" runat="server">
<input TYPE="button" VALUE="Log Out" class="Button Orange" onclick="window.location.href = 'Logout.aspx'">
</asp:PlaceHolder>
<asp:PlaceHolder ID="phLogin" runat="server">
<p class="Title">Current users sign in:</p>
<asp:TextBox runat="server" ID="username" holder="Username"></asp:TextBox>
<asp:TextBox runat="server" ID="password" holder="Password" TextMode="Password"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Log In" class="Button Orange" OnClick="btn_login_Click" ></asp:Button>
<p class="Forgot">Forgot password?</p>
</asp:PlaceHolder>
</div>
I just found out that this part is not showing in the iphone with all the content inside the .
what I can use instead of tag?
I tried but all the styling gets off. Is the only tag replacing this so I may work more on style sheet or you are suggesting something else? Or even if the reason for not showing is the ?
I believe the "holder" attribute in your textboxes should be "placeholder" instead.

multiple submit buttons won't postback on aspx - only last button

On my aspx page I have two fieldsets, each wrapped with an <asp:Panel DefaultButton="..."> that have their own <input type="submit"> buttons. Here's an abbreviated version...
<div id="content">...</div>
<asp:Panel runat="server" id="formPanel1" DefaultButton="form1SubmitButton">
<fieldset>
<asp:TextBox runat="server" id="textbox1"/>
<asp:TextBox runat="server" id="textbox2"/>
<asp:Button runat="server" id="form1SubmitButton" OnClick="form1SubmitButton_OnClick"/>
</fieldset>
</asp:Panel>
<asp:Panel runat="server" id="formPanel2" DefaultButton="form2SubmitButton">
<fieldset>
<asp:TextBox runat="server" id="textbox3"/>
<asp:TextBox runat="server" id="textbox4"/>
<asp:Button runat="server" id="form2SubmitButton" OnClick="form2SubmitButton_OnClick"/>
</fieldset>
</asp:Panel>
Only the last submit button form2SubmitButton will postback, the other button does nothing when clicked. I have simplified the code above but I should mention that each form, Panel included, is kept in a separate user control that is registered on the aspx.
UPDATE: After some more research I've figured out that the submits are not working because of validators on the another fieldset on the master page. That fieldset is for signing into the site, so it is needed. Thoughts?
As PCasagrande mentioned, make sure your validation groups match the default button and all validators. Specially for your logging part.
Example:
<asp:Panel ID="pnlUpdateInfo" runat="server" DefaultButton="btnUpdateInfo">
<ASP:TEXTBOX id="txtZip" runat="server"></ASP:TEXTBOX>
<asp:RequiredFieldValidator id="reqvalZipSignUp" runat="server"
ControlToValidate="txtZip" ValidationGroup="btnUpdateInfo" />
<asp:Button ID="btnUpdateInfo" runat="server" ValidationGroup="btnUpdateInfo" />
</asp:Panel>
Turns out it has to do with HTML5 constraint validation. I figured this out because in chrome, from the console, I was getting the error An invalid form control with name='...' is not focusable. This led me to this post. I added the html5 attribute formnovalidate="formnovalidate" to the submit buttons and everything works just fine (*tested in chrome v20.0.1132.57, firefox v14.0.1, IE9 v9.0.8112).
So to recap, here's the answer...
<asp:Button runat="server" id="submit1" OnClick="submit1_OnClick" text="Submit" formnovalidate="formnovalidate"/>

Submit by pressing enter asp.net

I am wondering why this, submitting on pressing enter, is only working on ie and not on google chrome...
This is the code I use actually :
<div class="TxtBox">
<asp:Panel ID="lepanel" runat="server" DefaultButton="Connect">
<asp:TextBox ID="TxtUserLogin" runat="server" TabIndex="1" Text="login" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
Display="Static" ControlToValidate="TxtUserLogin"></asp:RequiredFieldValidator>
<asp:TextBox ID="UserPass" runat="server" TabIndex="2" Text="password" TextMode="Password" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
Display="Static" ControlToValidate="UserPass"></asp:RequiredFieldValidator>
<asp:LinkButton ID="Connect" runat="server" OnClick="Connect_Click" TabIndex="3">connect</asp:LinkButton>
</asp:Panel>
<asp:Label ID="MsgError" runat="server" />
<div class="ForgottenPass">
forgotten password ?
</div>
<div class="RememberMe">
<asp:CheckBox runat="server" ID="chkBoxRemember" />
stay signed in
</div>
</div>
A LinkButton renders as a HTML anchor tag.
HTML anchor tags do not submit HTML forms. Therefore when you click Enter, this is not actioning any submit button.
A Button renders as HTML <input type="submit" />
An ImageButton renders as HTML <input type="image" />
These elements will both action your form.
Therefore changing LinkButton to Button or ImageButton is the best solution.
Using a LinkButton is also bad for users who do not have javascript enabled.
This is a known issue, when you add as default a link button (actually a link) and not a real button then there is a case that is not trigger properly.
So if you change it to real button you make it work.

Categories

Resources