asp.net custom control with form field validation issue - c#

I have created a custom asp .net control some fields have validation controal associated with them.
The problem arised when I declare more than one initialization on a page. When I hit submit on one of the intialized control, form validation occur on all other control that were declared. Due to this issue I cannot submit a form. Here is the code
<%# Control Language="C#" AutoEventWireup="true" CodeFile="FinancialAdvisorHelp.ascx.cs" Inherits="FinancialAdvisorHelp" %>
<table width="316" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" >
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorMember" runat="server"
ErrorMessage="Pleae enter member name. " ControlToValidate="TextBox_Name"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail" runat="server" ErrorMessage="Please enter email. " ControlToValidate="TextBox_email"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorDarryEmail" runat="server"
ControlToValidate="TextBox_email" ValidationExpression=".*#.*\..*"
ErrorMessage="<br>
Invalid Email."> </asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td width="165"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding:0;">Member Name*</p>
<asp:TextBox ID="TextBox_Name" runat="server" CausesValidation="True"></asp:TextBox>
</td>
<td width="151" align="right"><p style="font-family:Palatino Linotype; font-size:12px; margin:0 0 0 4px; padding:0; text-align:left;">E-Mail Address*</p>
<asp:TextBox id="TextBox_email" runat="server" CausesValidation="True"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="right"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding-right:50px;"><br />
Telephone Number</p>
<asp:TextBox id="TextBox_phone" runat="server" CausesValidation="True"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding:0;">Ask Darryl your question</p>
<asp:TextBox ID="TextBox_question" runat="server" Rows="7" Columns="48"
style="width:310px" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2"><p style="font-size:10px; margin:0; padding:0; float:left; width:240px; line-height:12px;">Note: you should receive a response within two (2) business days.<br />
*Required information.</p>
<asp:ImageButton ID="ImageButton_Advisor" runat="server" alt="submit"
width="63" height="18"
style="border:0; padding:0; margin:10px 0 0 0; float:right;" ImageUrl="./images/investments/submit.gif"
/>
<asp:HiddenField ID="HiddenFieldAdvisorEmail" Value="" runat="server" />
</td>
<tr><td colspan="2">
</td></tr>
</tr>
</table>

Use the ValidationGroup attribute on all form elements that cause validation including the submit button. When validation is triggered, it will only check other elements with the same ValidationGroup specified.
To make sure it is unique across each instance of your control on the page use your UserControl's ID as the validation group in the code behind.
RequiredFieldValidatorEmail.ValidationGroup = this.ClientID;
More information can be found here: http://www.dotnet-guide.com/validationgroups.html

I'm having a hard time deciphering your question.
Validation Groups
It sounds like you might want to take advantage of validation groups . When you have multiple triggers that should validate different controls, this is what to use.
Check your Validators and Custom Controls
If you are still having issues, when validators have errors and you trigger validation, it appears that "nothing happens" (what really is happening is that the validators error out and the page kind of just stops). It can also be possible that your custom controls have issues.
Check your Validators and ID's
The problem arised when I declare more
than one initialization on a page
What do you mean by this? I wonder if you are declaring controls dynamically in your code? This could also be your problem. Each ASP.NET control (including validators, custom controls, ... ) needs it's own unique ID. You can also have more than one validator checking a single control, but a single validator can't validate multiple controls.

Related

Control 'txtUserName' of type 'TextBox' must be placed inside a form tag with runat=server

I have this error - Control 'txtUserName' of type 'TextBox' must be placed inside a form tag with runat=server. I am unable to get rid of it, my text box is placed inside a form tag with runat=server.
The page which I am getting this on is my 'reset password' page I want the users to be able to enter their username and once they press the reset password button it takes them to a page which allows them to change their password.
Here is the code that I am currently working with.
<head runat="server">
<title></title>
</head>
<body>
<div style="font-family:Arial">
<table style="border: 1px solid black; width:300px">
<tr>
<td colspan="2">
<b>Reset my password</b>
</td>
</tr>
<tr>
<td>
User Name
</td>
<td>
<form action="ChangePassword.aspx" method="get">
<asp:TextBox ID="txtUserName" Width="150px" runat="server"
ontextchanged="txtUserName_TextChanged">
</asp:TextBox>
</form>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnResetPassword" runat="server"
Width="150px" Text="Reset Password" onclick="btnResetPassword_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</body>
</html>
As the error message says, you need to add a runat="server" in the form tag. This is just something ASP.NET demands to work.
The way ASP.NET handles events and postbacks forces this requirement. It tries to keep the page's state inside the actual page (called the 'view state'), and on form submission it sends it back. This was invented to make the stateless web stateful.
Just put in the runat="server" inside the form tag and you will be set.
<form runat="server" action="ChangePassword.aspx" method="get">
Add < form> tag like this...,
it will work..

Default Button not working with multiple validation groups

Thank you very much for taking the time to read this. I have an issue in which I was setting a default button for an ASP.NET page on page load from code behind, but now that I have multiple validation groups targeting one control, that is no longer working. Now, when I hit enter while in that control (textbox), validation for both groups are triggered minus the validation summary text.
Here is my examplefied code:
ASPX
<table>
<tr>
<td><asp:Textbox runat="server" ID="validateMe"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="firstValidator" runat="server" ErrorMessage="First check not valid" Text="*" ControlToValidate="validateMe" ValidationGroup="firstGroup"></asp:RequiredFieldValidator>
<td><asp:RequiredFieldValidator ID="secondValidator" runat="server" ErrorMessage="Second check not valid" Text="*" ControlToValidate="validateMe" ValidationGroup="secondGroup"></asp:RequiredFieldValidator>
</tr>
<tr>
<td><asp:Button runat="server" ID="firstButton" Text="V1" ValidationGroup="firstGroup"/></td>
<td><asp:Button runat="server" ID="secondButton" Text="V2" ValidationGroup="secondGroup"/></td>
</tr>
<table>
<asp:ValidationSummary ID="firstSummary" runat="server" ValidationGroup="firstGroup"/>
<asp:ValidationSummary ID="secondSummary" runat="server" ValidationGroup="secondGroup"/>
C#
protected void Page_Load(object sender, EventArgs e)
{
this.Form.DefaultButton = firstButton.UniqueID;
}
If I use this and hit 'Enter' while inside of the textbox without typing anything into it, then neither of the validation summaries will appear but I will have two asterisks next to the textbox (one for each group). If the user presses 'Enter' I would expect a full validation using only the first group which is supposed to be assigned to the DefaultButton (here, 'firstButton'). Is there any way to achieve this functionality and initiate the client-side validation that would have happened had the user clicked 'firstButton' instead?
I have also tried wrapping the whole table plus the validation summaries inside of an asp:Panel and setting the DefaultButton there, but I received the same results. Any help or pointers in the right direction would be greatly appreciated!
Set
EnableClientScript="false"
in RequiredFieldValidator control. It will help.
<asp:Panel runat="server" DefaultButton="secondButton">
<table>
<tr>
<td>
<asp:TextBox runat="server" ID="validateMe"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="firstValidator" runat="server" ErrorMessage="First check not valid" Text="*" ControlToValidate="validateMe" EnableClientScript="false" ValidationGroup="firstGroup"></asp:RequiredFieldValidator>
</td>
<td>
<asp:RequiredFieldValidator ID="secondValidator" runat="server" ErrorMessage="Second check not valid" Text="*" ControlToValidate="validateMe" EnableClientScript="false" ValidationGroup="secondGroup"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="firstButton" Text="V1" ValidationGroup="firstGroup" /></td>
<td>
<asp:Button runat="server" ID="secondButton" Text="V2" ValidationGroup="secondGroup" /></td>
</tr>
</table>
<asp:ValidationSummary ID="firstSummary" runat="server" ValidationGroup="firstGroup" />
<asp:ValidationSummary ID="secondSummary" runat="server" ValidationGroup="secondGroup" />
</asp:Panel>

Using AJAX to send create new user details via asp.net

What's the best way to send a username and password via ajax to the code behind page in .net.
I have an index.aspx page like so:
<asp:TemplatedWizardStep>
<CustomNavigationTemplate>
<div style="margin-bottom:7px;text-align:left;">To create an account, simply send us your e-mail address. A password will be sent to this address upon submission.</div>
<table border="0" cellpadding="3" cellspacing="0" style="width:99%;">
<tr>
<td style="text-align:left; vertical-align:middle;width:90px;">E-Mail:</td>
<td style="text-align:left; vertical-align:middle;">
<asp:TextBox ID="UserName" runat="server" CssClass="newUserTextbox"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align:left; vertical-align:middle;width:90px;padding-top:5px;">Screen name:</td>
<td style="text-align:left; vertical-align:middle;padding-top:5px;">
<asp:TextBox ID="ScreenName" runat="server" CssClass="newUserTextbox"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align:left; vertical-align:middle;"> </td>
<td style="text-align:right; vertical-align:middle;padding-top:5px;">
<asp:imagebutton ID="CreateUser" CommandName="CreateUser" Enabled="true" runat="server" AlternateText="OK" ImageUrl="powerstats/inc/img/ok_up.png"></asp:imagebutton>
<img src="powerstats/inc/img/cancel_up.png" style="cursor:pointer;" onmouseover="SwapImageIndex(this,'cancel_ov.png')" onmouseout="SwapImageIndex(this,'cancel_up.png')" />
</td>
</tr>
</table>
</CustomNavigationTemplate>
</asp:TemplatedWizardStep>
I want the imagebutton to be clicked and send an ajax call with the username and screenname (and eventually password), to the back end page. If I do onClick=X the page refreshes. I want the page NOT to refresh and send an alert back if something is wrong.
You would need to use an UpdatePanel with a ScriptManager. If a submit happen inside the UpdatePanel, it's gonna return only what's inside the UpdatePanel itself through AJAX.
If you want to handle possible Error, you can use the AsyncPostBackError Event of the ScriptManager. See MSDN page for a complete exemple of error handling.
Code would look like (+ your error handling logic):
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TemplatedWizardStep>
<CustomNavigationTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
/* Your login controls here ...
Make sure to include all the fields
to be refreshed + the submit button */
</ContentTemplate>
</asp:UpdatePanel>
</CustomNavigationTemplate>
</asp:TemplatedWizardStep>

In IE10+Win7 a ASP.NET ImageButton only works in Compatibility Mode

The following is the markup for the for my login piece and the ImageButton (they all are withing a ):
<tr style="width:150">
<td style="padding-left:0px;font-family:Verdana;font-size:70%;width:30%;font-size:small;">Username</td>
<td style="width:70%" align="left"><asp:TextBox id="txtUsername" runat="server" Width="90px" /></td>
<td><asp:RequiredFieldValidator ID="rfvUserName" runat="server" ErrorMessage="*" ControlToValidate="txtUsername" ValidationGroup="credentials" Display="Dynamic" /></td>
</tr>
<tr>
<td style="padding-left:0px;font-family:Verdana;font-size:70%;font-size:small;" >Password</td>
<td style="padding-right:0px;font-size:small;" align="left"><asp:TextBox ID="txtPassword" TextMode="Password" runat="server" Width="90px" /></td>
<td><asp:RequiredFieldValidator ID="rfvPassword" runat="server" ErrorMessage="*" ControlToValidate="txtPassword" ValidationGroup="credentials" Display="Dynamic" /></td>
</tr>
<asp:Panel ID="pnlInvalidCredentials" runat="server" >
<tr>
<td colspan="2" align="center" style="color: Red;">
<asp:Literal runat="server" ID="litInvalidCredentials" Text="Invalid Username or Password" />
</td>
</tr>
</asp:Panel>
<tr valign="middle">
<td style="padding-left:20px;" valign="top"><asp:ImageButton ID="ibLogin" runat="server" ImageUrl="~/images/Sign_in_button.png" OnClick="ibLogin_OnClick" Width="83px" Height="25px" /></td>
<td align="center" style="font-family:Verdana;font-size:70%;font-size:small;">
<asp:LinkButton runat="server" ID="lbForgotPassword" Text="Forgot password?" OnClick="lbForgotPassword_OnClick" />
</td>
</tr>
Also the very first line of ibLogin_Click(...) is Debug.WriteLine("ImageButton clicked.");. In IE10 on Win7 if I am not in Compatibility Mode when I click the button, nothing happens. The code-behind OnClick is never called (I never see "ImageButton clicked." in my debug output and login doesn't occur).
However, if I turn Compatibility Mode on, everything instantly works, and I of course I see "ImageButton clicked" in my debug output and a successful login happens.
Am I using the ImageButton incorrectly in my markup? Is there known issues with
I would do a few things to see why it doesn't work. First thing I would do would be use the "F12 Developer Tools" and use that to see what may be happening. Sometimes it might be showing that something is being blocked. This way you can see if it is being fired or not. If you are unable to find anything useful in that, try resetting your Internet Explorer. You would do this by selecting internet options, click the advance key, and reset. I have run into issues where IE was just messed up from me constantly debugging. I would also check to see if it works on Firefox and Chrome. As for you code, the image button looks fine. To me, this is going to be a issue with your actual browser and not your code. You could also try uninstalling IE 10 and see if it works without compatibility mode in IE 9. Good luck!
There is a bug in IE10 that pertains directly to ASP.NET ImageButton controls. The problem can be solved simply by replacing the control with an ASP.NET Button and styling it to look how you want.

submit a form with AjaxToolkit?

Can we submit a form in ASP.NET with AjaxToolkit ?
e.g:
below in our form:
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div id="mainDiv">
<table style="width: 40%;" cellspacing="3" cellpadding="3">
<tr>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorUsername" runat="server" ErrorMessage="*"
SetFocusOnError="True" Text="*" ControlToValidate="TextBoxUsername"
ValidationGroup="login"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBoxUsername" runat="server"></asp:TextBox>
</td>
<td align="left">
: UserName
</td>
</tr>
<tr>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
SetFocusOnError="True" Text="*" ControlToValidate="TextBoxPass"
ValidationGroup="login"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBoxPass" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td align="left">
: Pass
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="ButtonSubmit" runat="server" Text="Login"
ValidationGroup="login"/>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="LabelResult" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
I wanna log-in a user with Ajax so I have to send username and pass to a Webservice and get the result and show the result in LabelResult.
Can we do it with AjaxToolkit in ASP.Net?
I'm using ajax for a cascading dropdown menu set and i don't know how to submit the contents of the form to a controller. I think this is a related question becausewht I did was generate a hidden ajax control that triggered upon the change in value of the final ajax control in the form. The webservice that the control triggered then saved al of the form information to a database at which point the submitbuton was more of a comforting decoration for the user. Once the user submitted the form the values were available in a meta data field in the database and the submit handler accessed those values rather than reading them from the postback.
I didthis because it ended up being faster than learning what I needed to in order to read them form the postback using java script or some other method of retrieval form the postback so I wouldn't claim thatthisis the best approach but it worked for me. Hope this helpsyou somehow.
D.A.P.
for working of AJAX script manager must appear before the form which i think not possible and also
Cos Callis says very well
"If you want to submit the form, that is what a post back is for"
So, If we wanna send a form to server with Ajax, we have to use something like JQuery, and we can't do it with AjaxControlToolkit.

Categories