Click here to Skip to main content
11,482,402 members (71,830 online)
Click here to Skip to main content

Tagged as

Create dynamic controls from code-behind and validate them through JavaScript

, 29 May 2013 CPOL 12.6K 5
Rate this:
Please Sign up or sign in to vote.
Dynamically creating controls and validating using JavaScript.


This article explains how to create dynamic controls from code-behind and validate them through JavaScript.

Using the code

On page load the controls will be loaded dynamically from code-behind. On submit click event, validation is performed using JavaScript.

In case of failure, error messages are displayed. In case of success, submit click event is performed.

ASPX code:

<%@ Page Language="C#" AutoEventWireup="true" 
    Inherits="Practice.Aspx.DynamicControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="">
<head runat="server">
    <style type="text/css">
            color: Red;
    <script type="text/javascript">
        function fnValidate() {
            var txt = document.getElementById("txtSample");
            var drpGender = document.getElementById("drpGender");
            var x = document.getElementById('ErrorMessage');
            x.innerHTML = "";
            if (drpGender.value == "Select") {
                x.innerHTML = "Please Select Gender<br>";
            if (txt.value == "") {
                x.innerHTML += "Enter Name in Textbox";
            if (x.innerHTML != "") {
                return false;
    <form id="form1" runat="server">
    <div id="ErrorMessage" runat="server" class="red">
    <div id="GenerateControls" runat="server">
    <div id="SubmitButton" runat="server">


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Practice.Aspx
    public partial class DynamicControls : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            TextBox txtSample = new TextBox();
            txtSample.ID = "txtSample";
            GenerateControls.Controls.Add(new LiteralControl("<br />"));
            DropDownList drp = new DropDownList();
            drp.ID = "drpGender";
            Button submit = new Button();
            submit.ID = "btnSubmit";
            submit.Text = "Submit";
            submit.Click += new EventHandler(btnSubmit_Click);
                      "javascript:return fnValidate()");
        protected void btnSubmit_Click(object sender, EventArgs e)
            TextBox txtSample = (TextBox)GenerateControls.FindControl("txtSample");
            DropDownList drpGender = 
            String[] str = new String[10];
            str[0] = txtSample.Text;
            str[1] = drpGender.SelectedItem.Text;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 29 May 2013
Article Copyright 2013 by nandakishoreroyal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid