.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Easier way to programiclly create validation controls.

Posted By:      Posted Date: September 23, 2010    Points: 0   Category :ASP.Net

I know you can create validation controls in C# code

        RequiredFieldValidator Name = new RequiredFieldValidator();
        Name.ControlToValidate = "ControlName";

        RegularExpressionValidator name = new RegularExpressionValidator();
        name.ControlToValidate = "ControlName";
        name.ValidationExpression = "xx/xx/xxxx";

but is there a way to do it in a single line? In a recent post I had I learned a one line method to make textboxes and drop down lists

        Label lblReleaseDate = DynamicControlCreator.Label("lblReleaseDate" + i, " Release Date (MM/DD/YYYY): ");
        TextBox tbReleaseDate = DynamicControlCreator.TextBox("tbReleaseDate" + i);
        Label lblRegion = DynamicControlCreator.Label("lblRegion" + i, " Region: ");
        DropDownList ddlRegion = DynamicControlCreator.DropDownList("ddlRegion" + i, Region);

So,  is there a oneliner I can use for the validators? 

View Complete Post

More Related Resource Links

Dynamically Create Controls in ASP.NET by Using Visual C# .NET

This article demonstrates how to dynamically create a control for an .aspx page. The sample project does the following:
Creates two TextBox controls.
Verifies that the contents (TextBox.text) and the attributes of the TextBox are saved across posts to the server.
Illustrates handling events that are posted by a dynamically-created control

Dynamically Create Controls in ASP.NET with Visual Basic .NET

This step-by-step article describes how to dynamically create controls for an ASPX Web page.

The sample project does the following:
It creates two TextBox controls.
It verifies that the TextBox contents (TextBox.text) and attributes are saved across posts to the server.
It describes how events that are posted by a dynamically created control are handled.

ASP.NET: Selectively Enable Form Validation When Using ASP.NET Web Controls


Sometimes the extra controls that come with Visual Studio .NET can be a bit inflexible or they just don't provide enough functionality or flexibility for all situations. The ASP.NET form validation controls, while powerful and easy to use, require that the entire page be valid before it's submitted back to the server. Through the use of the new object-oriented features of Visual Basic .NET, it is possible to extend their functionality to overcome this limitation. This article tells you how and helps you decide when it's a good idea to keep validation on the client and when you'd be better off disabling it.

James M. Venglarik II

MSDN Magazine April 2002

How to dynamically create user controls ?



can you explain how to create user control dynamically in a web form ?When i am creating server controls of user controls are not instantiated and it is not getting initialized .when i am tyying to initialize its properties error occurs tha object instance not created .

Any suggestion ? 

Quick Note: Create Visual Web Parts by Using SharePoint 2010 Controls

In this exercise, you create a SharePoint 2010 visual Web Part in Visual Studio 2010 and then add a SharePoint ListViewByQuery control, a ASP.NET Label control, and a SharePoint DateTime control to it.

XmlReader.Create(textReader, XmlReaderSettings) Validation Issue

For the most part, when I perform an XmlReader.Read() I am catching validation errors based on my schemas.  This, however, is a large group of schemas with several imports and includes.  My question is why I'm having validation errors in two places that seem to be defined in the schema.   Here's an example:   Schema 1 (you can see that IdentificationType has sub-elements for IdentificationID and IdentificationCode):   <xsd:complexType name="IdentificationType">                 <xsd:annotation>                         <xsd:documentation>A data type for a representation of an identity.</xsd:documentation>                         <xsd:appinfo>                                 <i:Base i:namespace="http://niem.gov/niem/structures/2.0" i:name="Object"/>               &nb

Validation Controls Inside UpdatePanel

Hi All,If you use validation control, the base validator will render script WebForm_OnSubmit() to the page and add form onsubmit "onsubmit="javascript:return WebForm_OnSubmit();". This script is used to fire client side validation event.Now I have a listview inside updatepanel, when click edit button to fire the listview edit mode, the above validation script is just not rendered. How shall I work around with that? I need to customize the client validation event here.Thanks.

VS2008 and C#: How to create PictureBox controls programmatically in a FlowLayoutPanel ?

I am trying to create multiple PictureBox controls in a FlowLayoutPanel programmatically. The number of the PictureBox controls is dynamic depending on the target image folder. My current approach results a compilation error:             // (Works !)             pictureBox1 = new System.Windows.Forms.PictureBox();             pictureBox1.Width = 150;             pictureBox1.Height = 150;             flowLayoutPanel.Controls.Add(pictureBox1);             string sFileFullName = "Resources\\file1.bmp";             pictureBox1.Image = Image.FromFile(@sFileFullName);             // (Error !)             string pbName = "pictureBox";             for (int i = 0; i < 100; i++)             {             

how to apply validation controls on Gridview which is created dynamically

 hi, i created a gridview with two columns and 8 rows dynamically. All the rows are empty and editable at runtime. I am giving column names for this grid from some other .aspx page by passing column names using querystring.Because i am using this grid for three different pairs of column names.  i would like to know how to apply Required Field Validator, Regular Expression Validator  and Range Validator on Gridview textboxes. So that by using those validators i want to validate user inputs. And i would also like to know how to add rows at runtime for this grid. I am using ASP.net 2.0 and C#.net for this.(Visual Studio 2005) The code i am using to create gridview is below. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoadData.aspx.cs" Inherits="LoadData" %>  <form id="form1" runat="server">  <div> <asp:GridView ID="GrdDynamic" runat="server" AutoGenerateColumns="False" ForeColor="#333333" BorderColor="GradientInactiveCaption" BorderStyle="Solid" style="z-index: 108; left: 24px; position: absolute; top: 13px">  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  <RowStyle BackColor="#EFF3FB"

Validation on detailsview that contains both dynamic and standard controls?

I have a form that, originally, contained only dynamic controls. Validation worked perfectly, all in one spot.Today I changed three of the controls to cascading dropdown lists, two of them being required fields.After adding a RequiredFieldValidator for the two required fields, it's as though I have two separate validation events:1. Dynamic field validation - any errors show up in validation summary, and after all are resolved, no longer appear2. Required field validation - do not appear in validation summary until after all dynamic fields are validatedA user could press submit, and be alerted of one or more validation errors from the dynamic fields. Then, after resolving all those, the user would THEN see the RequiredFieldValidator messages in the validation summary. That would be understandably frustrating.Aside from using non-dynamic validation & RequiredFieldValidator for each field ... is there any way to have one validation sequence?Hope this makes sense.Thank you!

How to create templete controls inside a gridview at run time


i am creating a grid view at run time.now i want to create controls inside the gridview at run time itself,and how to Bind/Eval it.

plz help me

Create validation formating


I want to create a sort of validation formatting (like regex).  It doesn't have to actual validate; just make it easier for the user to enter the correct number of character needed.  I want something like domain: [xxx].[xxx].[xxx] (so it only allows 9 numbers, 2 periods).

So for instance when the user moves to the field they would be presented with one field with ability to enter 3 numbers.3 numbers.3 numbers

Any Ideas?  Thanks,


Strange validation behaviour on dynamically created controls


I'm trying to debug an application which generates a fixed number of questions from a database of 20 questions, and validate them on button click. On postback, I want the incorrect questions to be labeled red. However, all the questions are labeled red on postback. I'm almost positive that the problem is with "page_init" and "page_load",  I'm using session variables to keep track of the question IDs and invalid questions. Any help is appreciated.

Here is the sample code.

Protected Sub Page_Init(ByVal s As Object, ByVal e As EventArgs)
       dim i as integer
       if Not Page.IsPostBack then
           // generates a random integer array for loading the questions
       For i = 0 To 1  // I'm only loading two questions 
              //extract_question queries the database and load the controls dynamically
         end if      
       end if 
    End Sub

Protected Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
        If page.IsPostBack then
         dim textlabel as string
            dim sessionvar as string
            dim sessionc

Each AccordionPane with form containing controls with validation


Hey all,

I am using Accordion with multiple AccordionPane. Each AccordionPane has a form tag that contains asp controls such as dropdownlist and textboxes and buttons to submit or cancel. Each asp control has a requiredFieldValidator assign to it.  However when a i am in a particular AccordionPane to submit the form, the validator for other fields in other fields is highlighted preventing me from submitting.


Pleaseeee provide me with some assistance or light on what the problem is.  I have deadlines today.




How to create easier approval form in list


FYI - I'm a basic user (no code,etc.).  I have a site and I need to post under 1 list (A), a list of 25 items (B) that each contain a document attached and each of these items will have 20+ people (C) who need to approve/not approve/N/A each item (B).  In the list, I've created a field that provides enough description so that an approver (C) need only read the summary directly from the list page without having to go into each item, review the document then, edit each item to select approval status.  I currently have this set with each of the people (C) as a Choice field within each item (B) so that it must be opened fully to select approval status.  I am having compliance issues because it takes so many steps to get to the approval status.  Is there a way to make the approval drop down on the main task/list page?

Also, if I am assigning tasks, will they process correctly with Lotus users vs. Outlook users?

i use SharePoint 2007

Create controls inside the datagrid cell based on the datatype uisng WPF


I have a requirement that I need to develop a component which should display variable types of data inside a datagrid. The datagrid will have two coulmns. Fisrt column will display the list Captions (like: FirstName, DateOfBirth, IsMarried, Age) and the second column will display the list of data based on their data type, like:

> As FirstName is in string format it should display the value inside a TextBox control on right side of FirstName caption

> As DateOfBirth is in Date format it should display the Date value inside a DatePicker control on right side of DateOfBirth caption

> As IsMarried is in Boolean format it should display the CheckBox control with check state on right side of IsMarried caption

> As Age is in integer format it should display the integr value inside a TextBox Control on right side of Age caption.

If any body has any idea please guide me the way to develop. I request you to kindly help on this for creating this control



How to dynamically create extender controls


Hi all,

Ok thera's a couple of post on this already but I can't figure out a solution. I'm dynamically adding a calendar extender like so:

CalendarExtender CE = new CalendarExtender();
CE.ID = "ce" + parameter.Name;
CE.TargetControlID = TB.ID;
CE.PopupButtonID = IB.ID;
CE.Format = "dd MMMM yyyy";

When a postback occurs I get the following error:

Extender control 'ceDateFrom' is not a registered extender control.
Extender controls must be registered using RegisterExtenderControl() before calling RegisterScriptDescriptors().
Parameter name: extenderControl

Does anybody know how to resolve this?


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend