CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

ASP.NET Web Hosting – 3 Months Free!



Zone: > NEWSGROUP > Asp.Net Forum > windows_hosting.hosting_open_forum Tags:
Item Type: NewsGroup Date Entered: 8/19/2003 5:00:43 AM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 2 Views: 43 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
3 Items, 1 Pages 1 |< << Go >> >|
kimsagro
Asp.Net User
RequiredTextBox - Composite Control Or Inherit From Textbox8/19/2003 5:00:43 AM

0/0

Hi,

I have been researching how to create a control that combines a text box and a required field validator and have come across two solutions.

The first is from CodeProject and this control inherits from textbox

  public class RequiredTextBox : TextBox {

private RequiredFieldValidator req;

protected override void OnInit(EventArgs e) {
req = new RequiredFieldValidator();
req.ControlToValidate = this.ID;
req.ErrorMessage = "You must enter something."
req.Text = "*";
Controls.Add(req);
}

protected override void Render(HtmlTextWriter w) {
base.Render(w);
req.RenderControl(w);
}
}
The second is from swarren.net and this control is a composite

public class RequiredTextField : Control, INamingContainer {
private TextBox textBox1;

#region Creating your child controls
protected override void CreateChildControls() {
textBox1 = new TextBox();
textBox1.ID = "textBox1";
textBox1.Text = Text;

RequiredFieldValidator validator1 = new RequiredFieldValidator();
validator1.ControlToValidate = "textBox1";
validator1.Text = "*";
validator1.ToolTip = validator1.ErrorMessage = "You must enter your name.";
validator1.Display = ValidatorDisplay.Dynamic;

Controls.Add(textBox1);
Controls.Add(new LiteralControl("&nbsp;"));
Controls.Add(validator1);
}

public override ControlCollection Controls {
get {
EnsureChildControls();
return base.Controls;
}
}
#endregion

#region Text property
public string Text {
get {
if (textBox1 != null) {
return textBox1.Text;
}
string s = (string)ViewState["Text"];
if (s == null) {
return String.Empty;
}
return s;
}
set {
ViewState["Text"] = value;
if (textBox1 != null) {
textBox1.Text = value;
}
}
}
#endregion
}
}</code>The composite control required a lot more code and you also have to create a designer for it and add all the normal textbox control properties with design time attributes so that the control, looks and behaves exactly like the normal textbox. If you inherit from textbox however, you don't have to do any of this.

So are there any problems with the first solution. The reason I ask is that a number of server control professionals use the composite solution. I dont understand though, as the first solution is much simpler
jwyckoff
Asp.Net User
Re: RequiredTextBox - Composite Control Or Inherit From Textbox8/19/2003 1:44:34 PM

0/0

Good question. Though I highly respect Susan Warren and am forever in debt to her for putting up swarren.net when there was *nothing* on ASP.NET back in the Beta days, I think a better code sample is:


public class RequiredTextField : Control, INamingContainer
{
private TextBox textBox1;

protected override void CreateChildControls()
{
textBox1 = new TextBox();
textBox1.ID = "textBox1";

RequiredFieldValidator validator1 = new RequiredFieldValidator();
validator1.ControlToValidate = "textBox1";
validator1.Text = "*";
validator1.ToolTip = validator1.ErrorMessage = "You must enter your name.";
validator1.Display = ValidatorDisplay.Dynamic;

Controls.Add(textBox1);
Controls.Add(new LiteralControl("&nbsp;"));
Controls.Add(validator1);
}

public string Text
{
get {
EnsureChildControls();
return textBox1.Text;
}
set {
EnsureChildControls();
textBox1.Text = value;
}
}
}


It's a tough choice... but I think it comes down to good OO design. If you are looking for the "most right" solution, I think a composite control is better b/c it allow the developer to control what members are on the object. When you are developing controls that are going to be used my many people, the public signature of the class and how it behaves is very important.

if you are looking for a quick win, and you don't care for the "most right" solution b/c it's just you and a couple other developers on your team using the control, go for the inheritance.

Jason
Jason Wyckoff

kimsagro
Asp.Net User
Re: RequiredTextBox - Composite Control Or Inherit From Textbox8/20/2003 12:19:43 AM

0/0

Thanks heaps for your speedy and informative reply.

3 Items, 1 Pages 1 |< << Go >> >|


Free Download:


Web:
RequiredTextBox - Composite Control Or Inherit From Textbox - ASP
TextBox in composite control - ng.asp-net-forum.windows ... TextBox in composite control, > ROOT > NEWSGROUP > Asp.Net Forum ... ComponentModel;. public class RequiredTextBox : CompositeControl. ... Update in composite control Inherits CompositeControl Private m_radAm As Telerik. ...
CompositeControl vs User Controls - which is less hassle now? Oct 20, 2005 ... Now that we have the CompositeControl to inherit from in Asp.Net 2, ... RequiredTextBox : CompositeControl. {. private TextBox textBox; ...
CodeProject: Building an ASP.NET custom web control: a textbox and ... Aug 12, 2002 ... This web control will inherit from the TextBox web control, ... Let's have a look at our code-behind file for our RequiredTextBox control. ... seen how beneficial it can be to create your own composite custom control. ...
OurCurrentFuture.com - All posts tagged 'textbox' 11: public abstract class RequiredInput : CompositeControl .... 49: /// Initializes a new instance of the class. ... We next create a Control that inherits from our abstract RequiredInput class. ...
OurCurrentFuture.com - All posts tagged ... Aug 22, 2008 ... Since each type of input (TextBox, DropDownList, etc) has a slightly different implentation, ... 11: public abstract class RequiredInput : CompositeControl .... 12: public class RequiredTextBox : RequiredInput .... We next create a Control that inherits from our abstract RequiredInput class. ...
CodeProject: ASP.NET Required TextBox (Graphical ... Sep 21, 2006 ... NET custom TextBox control with a built-in RequiredFieldValidator, providing a similar look and feel ... It inherits the BaseValidator and uses some new cool ASP. ... Would a composite Web control be a good work around? ...
CodeProject: Validated TextBox for ASP.NET. Free source code and ... An article on how to make a composite custom control; Author: Dries de Groot; Section: ASP. ... mine does not use INamingContainer and inherits directly from TextBox. ... back does not take place when the required-textbox is not filled. ...
CodeProject: Validated TextBox for ASP.NET. Free source code and ... An article on how to make a composite custom control; Author: Dries de Groot; ... I admit that inheriting directly from textbox does have more advantanges. ... posting back does not take place when the required-textbox is not filled. ...
C custom components, inheriting profiles from data access. [See data access.] ... RequiredTextBox.vb control · CompositeControl class ...




Search This Site:










is there anyway to compile asp.net to work offline?

using hidden fields

simple c# database turotial needed

checking for inappropriate/offending contents entered by user

how to create a batch job

how create e-mail accounts on ms exchange using asp.net ?

createinstance and show the page

page_load not firing after moving page from vb.net to c#

retrieving values

html wysiwyg editor - need advice

how to utilize onclick event with asp:image

tab strip control

store and retrieving encrypted data to ms access db

postback

sending a custom email in html format

preview image

how to know the loop count?

how to

question regarding ending session

differences between vb6 .dll in asp and vb.net dll in asp.net

pre populate text fileds

error with silverlight

retrieving unread email from exchange server

handles.....

viewstate error?

can i disable ie keys?

get host name

page position after postback

[help] cannot store radio button info into ms sql server 2000

licensing question...

  Privacy | Contact Us
All Times Are GMT