Can't pass 'complex objects' as parameters in Design view - c#

I've created my own Validation controllers, to work with my own User Controls.
My Problem :
the classic validators accept parameters in 'design view', e.g.
<asp:RequiredFieldValidator ControlToValidate="txtFirstname" runat="server" />
and I'd like to do the same in my controls, but it appears when I pass 'ControlToValidate' a control ID, I get this error :
Cannot create an object of type 'System.Web.UI.WebControls.WebControl'
from its string representation 'txtFirstname' for the
'ControlToValidate' property.
What 'pattern' do I need to implement to ensure I can make the most of my 'ascx' page instead of having to hook up everything in my 'ascx.cs' code-behind page.
p.s. I'm calling the 'tags' I create on the 'ascx' page 'design view', but I think that is probably the wrong term, which I suspect is half the reason I cant find anythign on google for this.

Your code simply should look something like this (all in the .ascx):
<asp:TextBox id="txtFirstName"
Text="Enter a value"
runat="server"/>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
ControlToValidate="txtFirstName"
Text="Required Field!"
runat="server"/>
I'm guessing that you're not placing the RequiredFieldValidator in the same scope - causing this problem.

Related

ASP.NET C# client-side & server-side validation with regular expressions

I have a pretty straightforward aspx form that collects name, address, email etc and I'm trying to do server-side validation using asp controls. It's working (I think?) and I know I need to do it server-side, but I'm not sure exactly where to put the regex for the actual requirements for each field.
Here's one of my fields as an example:
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="FirstName" CssClass="col-md-2 control-label">* First Name</asp:Label>
<div class="col-md-3">
<asp:TextBox runat="server" ID="FirstName" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="FirstName"
CssClass="text-danger" ErrorMessage="First Name field is required." Display="Dynamic" />
</div>
</div>
I've been using an asp:RequiredFieldValidator and the client-side validation part is working perfectly. The form knows when the user has or hasn't entered something into my required fields and notifies them accordingly before sending anything to the server. I've tested it by putting a label on my page called "valid" and then putting this into my submit button click event:
if(Page.IsValid)
{
valid.Text = "it's working";
}
And when I fill out the required fields my label does indeed appear and say it's working. However, the asp:RequiredFieldValidator, by definition, only indicates whether my required fields have a value of some kind. It does nothing to check the formatting of what the user entered.
I do need to make certain fields required and I like how the client-side validation part is working. But I also need to do server-side validation and use regex somewhere in the mix to make sure they actually entered an e-mail address in the e-mail field and actually put numbers in the zip code field etc.
I'm fairly new to ASP.NET and it's starting to feel like I'm fighting it. I don't want to fight it. I want to use the available tools to do pretty standard server-side validation, I'm just not exactly sure where to put the regex.
Can anyone help?
Thanks
Web Forms validators add client and server side code automatically to cover both bases. In the case of your RequiredFieldValidator, it simply checks the value is present on the client and if you also call "Page.IsValid" when Page_Load is fired it will do so on the server end.
What you are after is the "RegularExpressionValidator". You can use one or both on the page depending on what you are trying to achieve.
Here's a good example: https://msdn.microsoft.com/en-us/library/ff650303.aspx
You can write your own validator, client and server side. Check out
CustomValidator
Example:
<asp:CustomValidator runat="server" id="CustomValidator" ValidateEmptyText="false" OnServerValidate="customValidation_event" ClientValidationFunction="customValidation_client" />
And add javascript function and validation method in your code-behind.

How to use asp:Login and asp:CreateUserWizard on Same Page

I want to place two Zurb Foundation 5.5.2 modals on the same page. One modal contains a customized <asp:Login> and the other contains a customized <asp:CreateUserWizard>.
I've created two separate user controls for this purpose. Independently, they work great. However, when I try to place both of them on the same page I either get a KeyError or the user can't login.
The problem is that the ID="UserName" and ID="Password" are required for both the <asp:Login> and <asp:CreateUserWizard>.
There is custom ASPX similar to this inside of each of these ASP element tags.
<asp:TextBox ID="UserName" runat="server" />
<asp:TextBox ID="Password" runat="server" TextMode="Password" />
Obviously, I can't have identical / clashing IDs on the same page, but ASP.NET expects to find an ID of UserName and Password inside of <asp:Login> and <asp:CreateUserWizard>.
I tried changing the Password ID inside <asp:Login> like so:
<asp:TextBox ID="Password_Different_ID" runat="server" TextMode="Password" />
and I get an error.
LoginUserModal: LayoutTemplate does not contain an IEditableTextControl with ID Password for the password.
I did the same thing for <asp:CreateUserWizard> and I get.
CreateUserWizard1: CreateUserWizardStep.ContentTemplate does not contain an IEditableTextControl with ID Password for the new password, this is required if AutoGeneratePassword = true.
I set AutoGeneratePassword="false", and I get the exact same error message even though I set it to false.
Any idea on how to change these IDs, or another way to solve the problem?

comparevalidation for text box value and hidden field value

<asp:TextBox ID="txtAppSanctionLimit" runat="server" onblur="calcCustDebtEquity()"> </TextBox>
<asp:HiddenField ID="hfAppReqeustAmt" runat="server" Value="0" />
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Proposed Limit is never greater than Request Limit" ControlToCompare="txtAppSanctionLimit" ControlToValidate="txtRequestLimits" Operator="GreaterThan"
ValidationGroup="Report3" Display="none" ></asp:CompareValidator>
<asp:Button ID="btnLimtUpdate" runat="server" Text="Update" ValidationGroup="Report3"
onclick="btnLimtUpdate_Click"/>
<asp:ValidationSummary ID="ValidationSummary3" runat="server" ShowMessageBox="true" ShowSummary="false" ValidationGroup="Report3" />
Above all has been used in my defalt.aspx page under Visual Studio 2010.
I want to show Validation Message if txtAppSanctionLimit value is greater than hfAppReqeustAmt value. note that hfAppReqeustAmt value get from database with query.
how can I solve this problem.
The accepted answer is completely incorrect!
Simply put you can't use a CompareValidator with a HiddenField. You need to use a CustomValidator.
See: asp:RequiredFieldValidator does not validate hidden fields
The attribute for the target
ControlToCompare="txtAppSanctionLimit"
should probably be pointed at your hidden field.
ControlToCompare="hfAppReqeustAmt"
and the attribute for the ControlToValidate should be pointed to your user data entry field.
ControlToValidate="txtAppSanctionLimit"
as it is currently pointing to a control that is not shown in the example code.
EDIT: Per MSDN for the CompareValidator.ControlToCompare -
If the control to compare is hidden or is inside a container (such as
a Panel control) that is not visible, the validator performs
server-side validation only. The validator client script supports only
visible controls.

How to validate expression on button click in asp.net C#

I am making a website and I have made a form fields like email field and validation expressions associated with it. Validation is initiated on text change. But i want it t execute on "submit" button click event.
I have searched but could not locate the solution to my problem. Please tel me why is this happening and how can i make it right. I am new to this web development field, So need help from you guys.
Thanks in advance!!!
Hamad
You could disable showing errors in the validator itself and instead make a validation summary which will be shown only after you click submit.
Like this:
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtEmail" ValidationGroup="vRegister" Display="None" ErrorMessage="Email field cannot be empty"></asp:RequiredFieldValidator>
and then declare a validation summary:
<asp:ValidationSummary runat="server" ID="vSummary" ValidationGroup="vRegister" DisplayMode="BulletList" />
What you should do is change the value of EnableClientScript to false. Then call the validation from your code behind (which should always be done since a user can disable their client side validation anyway. Security rule 1, never trust the client)
EnableClientScript: Gets or sets a value indicating whether client-side validation is enabled.
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server" EnableClientScript="false"
ErrorMessage="*" ControlToValidate="txtName" ></asp:RequiredFieldValidator>
Code Behind:
protected void btnSave_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
//Do stuff
}
//No need for else, the validations should display accordingly
}
Additional resources: http://weblogs.asp.net/rajbk/archive/2007/03/15/page-isvalid-and-validate.aspx
Are you simply looking for the validation type controls to validate input?
If so look at the property EnableValidation and set it to true. Doing so will force the validation of the textbox even before the button_Click event can execute.
Try this:
<asp:TextBox ID="txtName" runat="server" Height="23px"
Width="252px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server"
ErrorMessage="*" ControlToValidate="txtName"
ValidationGroup="vadd"></asp:RequiredFieldValidator>
The "Causes Validation" property on the button itself will automatically force your page to meet you validation specifications before firing the rest of the code associated with the button press.
If i have understood your question, you need to create a same validation group for each of the validation control in your aspx page. You also need to have a validation summary with same validation group. And atlast in the submit button in aspx page you have to mention same validation group...

Show and hide labels with assosicatedControls and their controls

I want to show and hide a label and its control. I can do this in c# in the code behind. But, I can only show/hide the control. Any ideas?
<asp:label AssociatedControlID="thisLabel" runat="server">This:
<asp:label ID="thisLabel" CssClass="ascontrol" runat="server" />
</asp:label>
I want to be able to show and hide that whole thing depending on what user gets to the page. I just need to know how to show/ hide that whole thing in the c# code behind...cannot seem to get the visibility of the wrapper label to go away.
You haven't supplied a server-side Id:
<asp:Label ID="label_MyControl" AssociatedControlID="txt_MyControl" runat="server" />
<asp:TextBox ID="txt_MyControl" runat="server" />
What you've done is nest a asp:Label control within another asp:Label control....
Since I normally hide more than one field contiguously, I tend to wrap the whole thing in an asp:Panel and hide the panel. However, that's just my particular usage. But since it's my usage, I tend to block those sorts of things out into panels even for something as simple as your example.
Just my nickel's worth, your mileage may vary, as always.
It should work if you get you r markup correct, like this:
<asp:Label ID="lblYear" runat="server" Text="Year (yyyy):"
AssociatedControlID="txtYear"></asp:Label>
<asp:TextBox ID="txtYear" runat="server" Columns="30" MaxLength="4"></asp:TextBox>
Then in the code behind you could have:
lblYear.visible = False
txtYear.Visible = False
Now, my understanding of the "AssociatedControlID" property of an asp:label is mainly for accessibility purposes. You don't need to have the AssociatedControlID value set to make things work as I've shown.

Categories

Resources