简体   繁体   中英

Usercontrols asp.net textbox/validator controls

I want to combine a textbox and several validator controls into 1 usercontrol. Is this possible with the intent to keep some fields dynamic like

textbox:cssclass textbox:id textbox:width

I'm asking this because i find myself putting a lot of validator controls for every (same textbox type) field in my form and it's getting kinda messy.

Kind regards, Mark

Build your usercontrol with properties that pass through to the textboxes. So for example, you'd include your ascx like:

<cc1:MyUserControl runat="server" TextBoxWidth="50" 
     TextBoxId="txtID" TextBoxCssClass="class" />

In the code for your user control, simply create these properties:

public int TextBoxWidth { get; set; }
public string TextBoxID { get; set; }
public string TextBoxCssClass { get; set;}

And in the code somewhere, pass the properties through to your textbox control. PreRender would be a good place to do it.

...
  myTxtControl.Width = this.TextBoxWidth;
  myTextControl.ID = this.TextBoxID;
  myTextControl.CssClass = this.TextBoxCssClass;
...

where myTextControl is the textbox that your usercontrol contains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM