Hi, I have about 20 text boxes on my form, and I want to validate each field using the requiredFieldvalidate control; however, I don't want to add that control to each text box.. it's tiring :) Is there a way to do that?
Printable View
Hi, I have about 20 text boxes on my form, and I want to validate each field using the requiredFieldvalidate control; however, I don't want to add that control to each text box.. it's tiring :) Is there a way to do that?
No.
You could make a user control called RequiredTextBox (or something) that contains a textbox and a requiredfieldvalidator.
In fact here's one I made quite sometime ago. It's pretty basic but did what I needed at the time and should give you an idea.
RequiredTextBox.ascx
RequiredTextBox.ascx.csCode:<%@ Control Language="C#" AutoEventWireup="true" CodeFile="RequiredTextBox.ascx.cs" Inherits="Controls_RequiredTextBox" %>
<asp:TextBox ID="requiredTextBox1" runat="server" /><asp:RequiredFieldValidator ID="rfv1" runat="server" Text="*" ControlToValidate="requiredTextBox1" Display="Dynamic" />
Usage:Code:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Controls_RequiredTextBox : System.Web.UI.UserControl
{
public bool Password
{
set { this.requiredTextBox1.TextMode = TextBoxMode.Password; }
}
public string ErrorText
{
set { this.rfv1.Text = value; }
}
public string ErrorMessage
{
set { this.rfv1.ErrorMessage = value; }
}
public string Text
{
get {return this.requiredTextBox1.Text; }
set { this.requiredTextBox1.Text = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
Code:<%@ Register TagPrefix="fishcake" TagName="RequiredTextBox" Src="~/Controls/RequiredTextBox.ascx" %>
....
<fishcake:RequiredTextBox ID="txtCompanyName" runat="server" />
Install Ajax control toolkit, it will more help ful for u
Which part of the Ajax toolkit and how would vbbit use it to solve his problem?Quote:
Originally Posted by koolprasad2003
I don't recommend installing an entire set of class libraries or a framework for the sole purpose of using one tiny feature. It's overkill. I'd go with fishguy's solution or simply have twenty requiredfieldvalidators or do it all the codebehind in one go and present the user with the result of that validation.
Excuse me!!! I'm Fishcake, Fishguy is that other fish related user.
Just curious, why? AjaxControlToolkit.dll is a single library that is to be deployed with your web solution. You use only the feature that you want.Quote:
Originally Posted by mendhak
Well, that's the point. You are deploying an entire set of features in form of a single DLL file and using just a small feature which can be easily replicated using simple JS trick which is easier to create, use, implement and thus in turn, hassle-free.Quote:
Originally Posted by rjv_rnjn
Not all controls in the kit use AJAX technique. There are some which use pure JS code. It's MS, who has clubbed everything in a single file and named it AJAX-something-something.dll!!!
If you had to perform one single stored procedure call in your website project, would you write the code yourself or add a reference to the DAAB and use SqlHelper just to avoid writing 3 lines of code?Quote:
Originally Posted by rjv_rnjn
Both will work, one is overkill.
Ok..ok.. I get it! Don't beat me to death over it. :p