Access Textbox Properties In A Custom Validator?

Feb 10, 2010

I want to build a custom validator control that inherits from BaseValidator. It will only be used on textboxes in my application. How can I get access to the textbox itself (read properties of the textbox) within the custom validator?

Here is what I have in my EvaluateIsValid function:

Dim t As TextBox = CType(Page.FindControl(Me.ControlToValidate), TextBox)
Return t.Text.Length <= t.MaxLength

It can't seem to find the control, so it breaks with a null reference exception. Can I do this another way?

View 1 Replies

Similar Messages:

Custom Server Controls :: Access Properties Of Custom WebControl In ASPX Codebehind File?

Jul 19, 2009

I have created a custom control that derives from WebControl class. I have defined a property called "ItemCount" in the custom control.

This custom control is created as a separate project and the assembly is added to the main web project (VS 2005 Web Site).

Just to test, I have only one line writer.Write("Hello World!") in the overriden "RenderContents" method in the web control.

Once added to the aspx file, the control renders the text perfectly fine and is declared in the aspx file as

<cc:mycontrol ID="_testControl" runat="server"/>

Now, when I try to access the control "_testControl" in the codebehind, I cannot compile and I get the error as "The name '_testControl' does not exist in current context.

After reading multiple posts, I tried declaring the control in my aspx codebehind as

protected MYControl _testControl;

This comes out to be NULL during run-time. I'm not too familiar with, but if other controls (e.g. Button, Textbox) are accessible in my codebehind including their properties, should the same logic be not applied to WebControls...they are all the same!

I can create a new instance of the _testControl in the OnInit method, but that defeats the whole purpose. I need to understand what is going on and what is the right way to solve it.

View 1 Replies

Web Forms :: Client Side Custom Validator Changing Textbox Values?

Mar 19, 2010

I've been working on a task to add a validator to compare the values of two text fields in a ASP.NET 1.1 application.

It uses validators already, so the best approached seemed to involve a CustomValidator. I was inspired by the following example:


I got the server side validation working - now I'm trying to add some client-side validation but I'm having a strange issue.

When I click in either of the two text fields, the value of the txtFinalGrade field is overridden to either "true" or "false". I've been having a tough time tracking down the code that's causing this. I tried using the IE Developer Toolbar to trace the script - as well as FireBug in Firefox (though this is an IE-only app) and I'm still trying to figure out what's causing this to happen.

Since I think this is a JavaScript issue, I'm posting that part of my code:


Anyone have ideas of things to try? Anyone have similar issues with textboxes being set to true/false when working with custom validators?

View 3 Replies

C# - How To Access Attributes Of Properties In The Model From Strongly Typed Custom HtmlHelper

Mar 18, 2011

I have a view that is strongly typed and its model is of type LogOnModel. That LogOnModel has anotated properties like this one:

[Required(ErrorMessage = "Please enter your password")]
[Display(Name = "Password", Description = "Your secreet password")]
public string Password { get; set; }

All of them has Display anotation with Display.Descripion property set. I want to create HtmlHelper extension method that will output <span> containg the value of Display.Description property. So for example if I called my extension method DescriptionFor than this code:

<%: Html.DescriptionFor(m => m.Password) %>

should produce following html: <span>Your secreet password</span>

View 1 Replies

C# - Load A User Control In An Ajax WCF Service And Access Custom Properties?

Apr 4, 2011

I can load a generic user control just fine in my WCF service doing the following:

UserControl userControl= (UserControl) new Page().LoadControl("~/UserControls/MyControl.ascx");

However i can't seem to find any way to do this

MyControl myControl=(MyControl)userControl;

The reason i need to do this is because my user control has a custom property, person, I need to do the following:

myControl.Person=new Person();

essentially i need to simulate:

<%@ Reference Control="~/UserControls/MyControl.ascx" %>

which normally goes inside page or master-pages.any way of doing this in an Ajax Enabled WCF Service?

View 2 Replies

How To Create The Custom Validator Instance That Is Linked To The Custom Validator

Dec 17, 2010

I created my custom validator in one project, something like that:


he composite control in another project. I added a reference of the custom validator project to the composite control project. I have a textbox in a composite control and I would like to validate this textbox by the composite control I created. But how can I do that? How can I create the custom validator instance that is linked to the custom validator I created?

View 1 Replies

Custom Server Controls :: User Control Can't Access The Parent Pages Public Properties

Feb 27, 2008

I have built a page that is contained within a master page and it holds 2 user controls

Control A contains a DataList

Control B contains a FormView

I want to pass the selected index from the DataList on Control A to the FormView on Control B so it acts like a master / detail scenario

I created a public property on the page that contains both controls called DataListSelectedIndex A also created a public property for DataListSelectedIndex on the Control I populate the DataListSelectedIndex property of the Control when the DataList_SelectedIndexChanged event is fired And I populate the DataListSelectedIndex property of the page on the OnLoad event of the page.

My problem is that on Control B - (or Control A for that matter) I can't access the public property DataListSelectedIndex

The method I am trying to access this is through - this.Page on the OnLoad even of the Control

View 6 Replies

Forms Data Controls :: Access Textbox Text Properties When Gridview Edit Mode

Jun 16, 2010

Is it possible to access textbox text properties from codebehind when gridview is edit mode? for example I can access label's text properties on gridview row databound event.


So I do not like use Text='<%#Eval("ColumnName")%' this method. I want to manage textbox text properties dynamcily codebehind when click the edit button on gridview. When I clik the edit button these code blocks runs


but rowdatabound event fires again and couldn't find label control. It looks for textbox control to bind data but I can not manage this step.

View 1 Replies

Does A Custom Validator Have To Be Used With The Validator Callout Extender

Jul 6, 2010

I have a validator callout extender that works, it shows the callout box. But, it does a postback. It didn't do a post back the first time I clicked the button but it did for subsequent times. I read on the AJAX site that a custom validator must be used with this for it to work. I'm currently using this with a required field and regular expression validator. It works, the callout appears, but it doesn't work every time with out the post back.I posted this to see if there is a way to get this to work with a required field and regular expression validator, with out using a custom validator. I'm using the newest release of the toolkit, so I thought the documentation may be old, since the callout is appearing.

View 8 Replies

Custom Server Controls :: Custom Validator Client-side Validation Cannot Work?

Jan 13, 2010

I've been trying to get a custom validator component working that ensures at least one checkbox from a checkboxlist has been checked. I've been used some code I found on but the client-side validation didn't work. I've been attempting to modify it to get the client-side javascript validation to work with no luck. Basically, I compile the below code into a DLL and add it to my bin folder.


View 2 Replies

Custom Server Controls :: Custom Required Field Validator?

Sep 25, 2009

I have a custom TextBox and a Custom RequiredFieldValidator. The custom RequiredFieldValidator inherit from RequiredFieldValidator. In the custom TextBox I have a Boolean property called "Required". So if this property is set from the MarkUp I will create an instance of the CustomRequiredFieldValidator in the custom TextBox class. Every thing is working fine except that I am unable to figureout where and how to check the IsValid property of custom RequiredFieldValidator.

Also I am unable to figure out that why protected override bool EvaluateIsValid() is not executing.Please note that I have set all the required properties in the constructor of custom RequiredFieldValidator calss as following


I also placed an Asp Button with CauseValidation="true"; but still the page post back is not occuring. Also the click event of the Button is not executing. What I understand that all this validation is happening on the client side.

my objective is to execute some Business Logic based on either the user has entered value in the custom Textbox or not.

View 4 Replies

Custom Server Controls :: Binding Properties Of A Custom Control In A ListView?

Jul 6, 2009

I have a custom control derived from CompositeControl that I'm using in the EditItemTemplate and InsertItemTemplate of a ListView. The ListView's data source is an ObjectDataSource.

I use <%#Bind('fieldname') %> to bind the properties of my custom control. I have found that the data will be displayed in my custom control, but when I try to update or insert, the data is not retrieved. I checked the onitemupdating event and saw that NewValues.Count was zero.

Does my custom control need to implement somthing special in order to return its data?

View 4 Replies

Custom Server Controls :: Custom Control And Properties Window?

Jun 1, 2010

I have made a composite control that has 1 Label Control ,1 TextBox ,1 button and 1 Panel Control. Below is my code:

namespace ServerControl1


The above code compiles without any error. Also I can drag and drop this control from the Toolbox of my Project on my web form. However I am unable the change the value of my fromLabel control.It allows me to change the value in the properties window.But when I run the form it always shows me its default value which is "From". I want it to be flexible so that any one can change the value in the properties window.

View 3 Replies

Custom Server Controls :: Why Custom Gridview Control Not Render HTML Properties Under Properly In Visual Studio 2005

Mar 5, 2010

Why custom Gridview control not render html properlies under <Columns> properly in Visual Studio 2005?

For example:


View 1 Replies

Custom Server Controls :: Custom Control's Constructor / User To Set That variable In The Properties Window After They Drag The Control Onto A Form?

Mar 4, 2010

I have a custom control which inherit from the Table class and in the constructor, it takes a an integer as an argument. There is no empty constructor.

Is there a way for the user to set that variable in the properties window after they drag the control onto a form.

I know some .NET controls, you can set the source for the parameter to different things like another control's property, QueryString using just the properties window.

Right now, I have to create the control dynamically. I read the query string and then created the object.

View 1 Replies

Web Forms :: Best Example For Custom Validator

Dec 18, 2009

best example for custom validatorhow can we use..i know that but iam not getting it clearly

View 2 Replies

Set Width Of Custom Validator

Feb 11, 2010

Does anybody know of a way to set the width of a custom validtor so that the error message text will wrap if it exceeds the specified width? I have a user control that contains a custom validator which the containing page can set the error message on based on specific validation results. he user control sits within a table cell in a page. If the message is very long it simply prints the entire message on a single line ignoring any column widths that are set. I have tried setting the width property on the custom validator itself to no avail.

View 2 Replies

MVC :: How And When Custom Model Validator Is Used

Apr 29, 2010

I am trying to understand a little more about implementing a custom model validator. I have implemented a custom model validator (derived from DataAnnotationsModelValidator<T>) for my custom validation attribute in order to do validation on the client side as well. There is not much documented about DataAnnotationsModelValidator. Besides, implementing client-side validation when is it a good idea or needed to implement a custom model validator for a custom attribute?

A separate question - in trying to understand how and when a custom model validator is used by the framework I noticed that my custom validator is always created with the context (2nd parameter in DataAnnotationsModelValidator<T>'s constructor) being a ViewContext, although it is declared as ControllerContext. Will a validator ever be created with something else than a ViewContext, and if so, when?

View 2 Replies

Custom Validator Not Validating Correctly?

Mar 16, 2009

In the following code I have a dropdownlist and text box, what I'm trying to achieve is when an item from the dropdownlist is selected it needs to make sure the textbox is not null before page IsValid, if not Page is not valid, what I'm getting is Page is valid is textbox is null and not valid is textbox if populated. I'm new to this and cannot figure out why this is happening

<%@ Page Language="C#" %>
<form id="Form1" runat="server">
<asp:DropDownList ID="editStatusTextBox" runat="server"
CausesValidation="True" >
<asp:ListItem Selected="true" Value="Call Attempt 1" Text="Call Attempt 1"/>
<asp:ListItem Value="Call Attempt 2" Text="Call Attempt 2"/>
<asp:ListItem Value="Contacted - Interested" Text="Contacted - Interested"/>
<asp:ListItem Value="Contacted - Not Interested" Text="Contacted - Not interested"/>
<asp:ListItem Value="Callback Required" Text="Callback Required"/>
<asp:ListItem Value="Not in Business" Text="Not in Business"/>
<asp:ListItem Value="Number not in use" Text="Number not in use"/>
<asp:ListItem Value="Using alternative product" Text="Using alternative product"/>
<asp:ListItem Value="Nurture" Text="Nurture"/>
<asp:TextBox id="editCallBack"
<script runat="server">
void Button1_Click(Object sender, EventArgs e)
if (Page.IsValid)
Label1.Text = "Page is Valid";
Label1.Text = "Page Not Valid";
void ValidateNumber(object source, ServerValidateEventArgs args)
if ((Eval(editCallBack.Text) == null) && this.editStatusTextBox.Text == "Call Attempt 1")
args.IsValid = false;
else if ((Eval(editCallBack.Text) == null) && this.editStatusTextBox.Text == "Call Attempt 2")
args.IsValid = false;
catch (Exception ex)
args.IsValid = false;
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="editCallBack" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:CustomValidator id="CustomValidator1"
runat="server" ControlToValidate="editCallBack"
ErrorMessage="You must enter a Date"
<asp:Button id="Button1" onclick="Button1_Click"
runat="server" Text="Button"></asp:Button>
<asp:Label id="Label1" runat="server"></asp:Label>

View 2 Replies

Custom Validator Not To Trigger On The First Button?

Feb 10, 2010

I have two buttons on my page, the first page is View Button and the second is Update Button. View button display's member's personal profile. If I click on the view button, my custom validator automatically trigger, meaning, it automatically displays the error message. Whereas, the custom validator must trigger if I click on the Update button. How am be able to deal with this kind of problem.

View 6 Replies

Web Forms :: Custom Validator Is Not Getting Called?

Jan 20, 2011

here is the code "when i select a value from calender customvalidator is not called . . "


View 7 Replies

Web Forms :: Calling A Custom Validator

May 12, 2010

I have a custom validator, which I have set a property for, and I can access this validator from my MVC controller method. However, when I run the code, I am not seeing the error message on the page. My code in the controller method looks like this:

If (!bool)
this.view.customValidator.IsValid = false;
this.view.customValidator.Visible = true;

View 2 Replies

Web Forms :: Error While Using Custom Validator?

May 6, 2010

I am using Customvalidator control . I am giving client side validation function in it. On running it is giving error as: ValidationStream is not defined.

Following is the code:

<asp:CustomValidator ID="CustomValidator6" runat="server" ClientValidationFunction="validateDriveDate"

View 2 Replies

Determine Which Custom Validator Will Fire First?

Feb 10, 2010

I have two custom validators that are in the same validation group. This validation group is launched by one button. My question is, in what order will the custom validators fire in? It appears that the validator that is placed earlier in the aspx page fires first, but I don't want to rely solely on this.

View 2 Replies

Web Forms :: Custom Validator Is Not Working In Ie6?

Feb 9, 2011

<asp:TextBox ID="txtMobile" runat="server" MaxLength="11"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="fteMobile" runat="server" TargetControlID="txtMobile"
FilterType="Numbers" />


View 2 Replies

C# - How To GetProfile By Custom Properties

Mar 9, 2011

<profile enabled="true" automaticSaveEnabled="false">
<add name="InvitationCode" type="String"/>

How to get profiles with InvitationCode="foo"? Also can i make property unique?

View 1 Replies

Custom Validator For MaxLength Property Of Textarea

May 16, 2010

I have quite a few text area's on my page and am wanting to limit the number of characters for each one. However each one should be able to have its own maxlength (i.e. one could be 20 characters and the other could be 100 characters). My validation on the page is all done using custom validation as the built in validation of .NET is not extensible enough for me to display my validation errors how I want to display them. So this is what im wanting to do:

Make a custom validatorPass a MaxLength value from the custom validator into the javascript function, this value must also be accessible from the code behindI can then run client side and server side validation on each textbox control. The problem I am having is passing a MaxLength value to client side script and to server side. Is there a property on custom validators which I can use to pass values around? For instance in Javascript I would want the value of the MaxLength and also the id of the Div which will show the characters remaining, and server side I would need to know the MaxLength. I have done lots of research on this and cannot seem to find an answer.

View 8 Replies

Web Forms :: Custom Validator Text Cannot Disappear

Aug 10, 2010

I have a form with textbox for name and two dropdownlist for month and years. Textbox has Requiredfieldvalidator and the dropdowns has got ustomvalidator.Initially when i dont enter anything in the text box and leave the dropdowns with default "MM" for month and "YYYY" for year i get error message in the validation summary at top and asterisk next to the textbox and dropdowns. Once i enter some text in the textbox and focus is away from textbox the asterisk disappears but even though i hav selected a valid month and year the asterisk next to the dropdowns is not disappearing. Same problem with the radio buttons.

View 5 Replies

Web Forms :: How To Write Custom Validator For DropDownlist

Jan 25, 2011

In my page i have DropDownlist.I want to write Custom validator.How to write?

View 3 Replies

Checkbox Custom Validator With ValidationSummary Not Working?

Jan 25, 2011

I have the following validation which is working fine for the rest of my fields, but trying to get a custom validator to work as part of the validation summary for a checkbox but no joy.

This is what I have at the moment

<script language="javascript" type="text/javascript">
function ValidateTandCs(source, args)
args.IsValid = document.getElementById('<%= optIn.ClientID %>').checked;
<asp:ValidationSummary CssClass="highlight"
HeaderText="<p>Please amend these errors below to continue with your
application.</p>" Runat="server" />
<asp:CheckBox id="optIn" runat="server"></asp:CheckBox> I agree to the terms and
conditions of this site and I wish to Opt In for registration.
<asp:CustomValidator ID="valTandCs" ClientValidationFunction="ValidateTandCs"
ValidationGroup="ValidationSummary1" runat="server"
ErrorMessage="Please accept Terms and Conditions before submitting.">

But when I click submit I only see the error messages for my other fields and nothing for this checkbox..

View 1 Replies

Copyrights 2005-15, All rights reserved