Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

asp.net user control

Posted on 2007-11-15
10
Medium Priority
?
449 Views
Last Modified: 2013-12-17
Hi,
I want to create a user control for asp.net web page where a Textbox accepts only Numeric values.. I also want to expose 1 or 2 properties additional in it..........Is it mandatory that I have to use javascript?
Can an example be shown how to create one for my webapplication whichcan be used across pages
0
Comment
Question by:dotnet0824
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 200 total points
ID: 20292035
>> Textbox accepts only Numeric values
You can enforce this with a normal input box using input validators...

http://www.codeproject.com/aspnet/aspnetvalidation.asp
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 200 total points
ID: 20292121
for the numeric textbox, start with http://www.eworldui.net/CustomControls/NumericBox.aspx
0
 
LVL 3

Assisted Solution

by:Aurora27
Aurora27 earned 400 total points
ID: 20292210
If you want to expose additional properties, its best to have it as a user control.

Basically, you create a user control rom the asp.net create new item and then add code for it, control it and distribute it. You can expose any no. of methods in it.

For e.g:

http://www.15seconds.com/issue/020319.htm

CT.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dotnet0824
ID: 20293294
i want to build a user control of my own .. how is that i can enfore the user to type only numerics.. i dont want to have any validation controls... The text box when typed in should allow only numerics.
0
 
LVL 1

Expert Comment

by:adam_ingeniti
ID: 20294693
The way I've solved this recently was to create my own textbox control (inheriting from Textbox), which also implements IValidator... that way you can have whatever custom properties you want... for instance, I've defined an enum for "FieldType", which can be Short Text, Long Text, Numeric, Money, etc...

The nice thing about using IValidator is that you're working *with* the ASP.Net page lifecycle... so when the user clicks "Submit" and the data entered doesn't match your field type, you never even hit the code attached to the Submit button's OnClick!  This keeps everything very neat and tidy :)

Incidentally, I'd suggest that you use a Regex (regular expression) such as "^[0-9]{0,15}[.]{0,1}[0-9]{0,2}$" - which is valid for the SQL Money type (ie. between 0 and 15 digits, from the beginning of the string, possibly followed by a single decimal point, followed by between 0 and two digits.

You might simply use "^[0-9]+$" if all you wanted to do is ensure numeric input.

Post a reply to this comment if you don't know how to use Regex, or if the above needs clarification!
0
 

Author Comment

by:dotnet0824
ID: 20296706
Hi,
Thanks for the message... Should javascript be used for sure to do this... My friend had a debate that javascript is client side so its the best.... I feel that this has to be done in server side.. can a model how to do it be shown
0
 
LVL 1

Expert Comment

by:adam_ingeniti
ID: 20303092
Validating client-side is a good start, as it reduces server load for the application... however, you are correct, it's generally easier to implement custom validation server-side only. An IValidator implementation *can* support Javascript client-side validation *as well as* server-side code... but it takes more work.

I'll post some code when I have more time (probably tomorrow)...
0
 

Author Comment

by:dotnet0824
ID: 20303838
Thanks adam
0
 
LVL 1

Accepted Solution

by:
adam_ingeniti earned 1200 total points
ID: 20309785
Here is a good example of how to create a custom control entirely in code, which implements the IValidator interface:
	
public class ValidatingField : System.Web.UI.WebControls.WebControl, INamingContainer, IValidator, IDisplayable
{
	private CheckBox _checkBox = new CheckBox();
	private DropDownList _dropDown = new DropDownList();
	private TextBox _textBox = new TextBox();
	private FieldType _fieldType;
 
 
	public ValidatingField()
	{ }
 
 
	[Flags]
	public enum FieldType
	{
		ShortText = 1,
		Dropdown = 2,
		Checkbox = 4,
		Money = 8
	}
 
	public string TextboxText
	{
		get { return (string)(ViewState["FieldTextBox_Text"] ?? _fieldTextBox.Text);  }		
		set {  _fieldTextBox.Text = value; }
	}
 
	public FieldType FieldType
	{
		get
		{
			if (_fieldType == 0)
				_fieldType = FieldType.ShortText;
 
			return _fieldType;
		}
 
		set  { _fieldType = value;  }
	}
 
 
 
	/// <summary>
	/// Set this property to make the field validate as a mandatory entry
	/// </summary>
	public bool IsRequiredField
	{
		get { return ViewState["IsRequiredField"] == null ? false : (bool)ViewState["IsRequiredField"]; }
		set { ViewState["IsRequiredField"] = value; }
	}
 
 
	protected override void OnInit(EventArgs e)
	{		
		EnsureChildControls(); //you need this to ensure ViewState for controls like a combobox
 
		Page.RegisterRequiresControlState(this);
 
		Page.Validators.Add(this);
		
		base.OnInit(e);
 
	}
 
	protected override void CreateChildControls()
	{
		Controls.Add(_textBox);
		Controls.Add(_dropDown);
		Controls.Add(_checkBox);
		this._fieldDropDown.SelectedIndexChanged += new EventHandler(fieldDropDown_SelectedIndexChanged);
 
		base.CreateChildControls();			
	}
 
 
	protected override void Render(HtmlTextWriter output)
	{
		if (!Visible) return;
 
		if (!Page.IsPostBack)
		{
			//Set up properties which can be altered on postback:
		}
 
		output.WriteLine(String.Format(@"<div class=""Container"">"));
 
		switch (FieldType)
		{
			case FieldType.Checkbox:
				_checkBox.RenderControl(output);
				break;
 
			case FieldType.Dropdown:
				_dropDown.RenderControl(output);
				break;
 
			case FieldType.ShortText:
			case FieldType.Money:
				_textBox.RenderControl(output);
				break;
			
		}
		
		output.WriteLine(String.Format(@"</div>"));
	}
 
 
 
 
	protected override void OnUnload(EventArgs e)
	{
		if (Page != null)
		{
			Page.Validators.Remove(this);
		}
	
		base.OnUnload(e);
	}
 
	public string ErrorMessage
	{
		get { return _errorMessage; }
		set { _errorMessage = value; }
	}
 
	public bool IsValid
	{
		get { return _isValid; }
		set { _isValid = value; }
	}
 
	/// <summary>
	/// Tip for new players: Make sure you set the _errorMessage BEFORE setting IsValid=false!  
	/// Otherwise, the Page_Load seems to continue, and a blank error message will be displayed :)
	/// </summary>
	public void Validate()
	{
		if (Visible)
		{
			if (FieldType == FieldType.Money)
			{
				if (!new Regex("^[0-9]{0,15}[.]{0,1}[0-9]{0,2}$").Match(_fieldTextBox.Text).Success)
				{
					_errorMessage = "You must enter a dollar value for this field";
					IsValid = false;
					return;
				}
			}
 
			if (FieldType == FieldType.ShortText && IsRequiredField)
			{
				if(_textBox.Text.Length == 0)
				{
					_errorMessage = "You must enter a value for this field";
					IsValid = false;
					return;
				}
			}
		}
	}
}

Open in new window

0
 
LVL 1

Expert Comment

by:adam_ingeniti
ID: 20309796
Incidentally, you must *always* validate server-side, if nowhere else... as client-side validation is only effective until someone decides to hack your html :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

671 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question