Pages

Thursday, October 9, 2014

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 



Usercontrol's 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;
            }
        }
    }
    public string ucMinutes
    {
        get { return minTxtBx.Text.ToString(); }
        set { minTxtBx.Text = ucMinutes; }
    }
    public string ucHours
    {
        get { return hourTxtBx.Text.ToString(); }
        set { hourTxtBx.Text = ucHours; }
    }
    public string ucTime
    {
        get { return timeDDL.SelectedValue.ToString(); }
        set { timeDDL.SelectedValue = (timeDDL.Items.Contains(new ListItem(ucTime))) ? timeDDL.SelectedValue = ucTime : ""; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

 
and then use a RequiredFieldValidator and the Usercontrol  in a WebPage 
<%@ Page Language="C#" Culture="ar-sa" Title="Untitled Page" %>
<%@ Register Src="jpTimeChooser35UC.ascx" TagName="jpTimeChooser35UC"   TagPrefix="uc1" %>

<uc1:jpTimeChooser35UC ID="fromTimeUC"  runat="server" />
<asp:RequiredFieldValidator runat="server"ErrorMessage="ERROR_MESSAGE" 
ID="RequiredFieldValidator1" ControlToValidate="fromTimeUC" 
SetFocusOnError="True"></asp:RequiredFieldValidator>

<asp:Button ID="submitBtn" runat="server" Text="submit"
CssClass="tblButtons" Visible="true" OnClick="submitBtn_Click"/>
Preview 


  













That's It .. Hope this was helpful :)

Reference: Microsoft Support #310082