Adsence750x90

Friday, July 2, 2010

Dynamically Add AJAX Control Toolkit ValidatorCallout Extender

How to add AJAX Control Toolkit  ValidatorCallout Extender to Dynamic TextBox

To add Ajax Control ToolKit ValidatorCallout we need one TextBox and RequiredFieldValidator. Now i am creating all controls are adding dynamically.  For more information, see  ValidatorCallout control.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Dynamically Creating TextBox
        TextBox txtBox = new TextBox();
        txtBox.ID = "TextBox1";
        txtBox.MaxLength = 100;

        //Dynamically Creating RequiredFieldValidator Control
        RequiredFieldValidator requiredFieldValidater = new RequiredFieldValidator();
        requiredFieldValidater.ID = "RequiredFieldValidator1";
        requiredFieldValidater.ControlToValidate = txtBox.ID;
        requiredFieldValidater.ErrorMessage = "Name is required.";
        requiredFieldValidater.Text = "*";

        //Dynamically Creating ValidatorCalloutExtender Control
        AjaxControlToolkit.ValidatorCalloutExtender validatorCalloutExtender = new AjaxControlToolkit.ValidatorCalloutExtender();
        validatorCalloutExtender.ID = "ValidatorCalloutExtender1";
        validatorCalloutExtender.TargetControlID = requiredFieldValidater.ID;

        //Dynamically Adding controls to PlaceHolder Control
        PlaceHolder1.Controls.Add(txtBox);
        PlaceHolder1.Controls.Add(requiredFieldValidater);
        PlaceHolder1.Controls.Add(validatorCalloutExtender);
    }
}




Output Sample









<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>

No comments: