Thursday, January 4, 2018

How To Using asp net Validation Controls to be used with Custom Usercontrols

How To Using asp net Validation Controls to be used with Custom Usercontrols


To use asp.net Required Field Validator, Range Validator ..., etc you must have a ValidationProperty attribute that specifies which value from your custom user control the validation controls to be validated validate.

So.. In your custom usercontrol  class add the ValidationProperty attribute and a property that returns the data in a format that is suitable for the validation controls.

Usercontrol
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="jpTimeChooser35UC.ascx.cs"

    Inherits="jpTimeChooser35UC" %>

<table cellpadding="0" cellspacing="0" dir="rtl" style="text-align: center;">

            <tr>

                <td>

                    <asp:TextBox ID="minTxtBx" MaxLength="2" runat="server" Width="30px" CssClass="tblField" Text="30" Style="text-align: center" >

                    </asp:TextBox>

                </td>

                <td>

                    :

                </td>

                <td>

                    <asp:TextBox ID="hourTxtBx" runat="server" MaxLength="2" Width="30px" CssClass="tblField" Text="07" Style="text-align: center" >

                    </asp:TextBox>

                </td>

                <td style="padding-right: 5px">

                    <asp:DropDownList ID="timeDDL" CausesValidation="false" Font-Size="10pt" Width="50px" runat="server" CssClass="tblField">

                        <asp:ListItem Selected="True">?</asp:ListItem>

                        <asp:ListItem>?</asp:ListItem>

                    </asp:DropDownList>

                </td>

            </tr>

            <tr style="color: Gray; text-align: center; padding: 0px; font-family: Tahoma; font-size: 9pt;">

                <td>

                    ?????

                </td>

                <td></td>

                <td>

                    ????

                </td>

            </tr>

        </table>

        
 
Preview 



Usercontrols Code:
using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Globalization;

[ValidationProperty("TimeValue")]

public partial class TmeChooser35UC : System.Web.UI.UserControl

{

    public object TimeValue

    {

        get

        {

            string TimeString = this.ucHours+":"+this.ucMinutes+" "+this.ucTime;

            if (TimeString == ": ?")

            {

                return "";

            }

            else

            {

                return TimeString;

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.