ASP.NET: Display Web User Controls

I created some web user controls (.ascx files) and deleted them in Pages. User controls contain some text fields that I would like the page to have access directly.

What is the easiest (read: least time) way to display these text fields in user controls on pages containing user controls?

The two parameters that I know call myUserControl.FindControl (id) from Pages (does this even work from the page?) And write properties in user controls to display the TextBox values.

Nothing seems perfect. FindControl () requires the Page to know the identifiers of text fields in user controls, thereby interrupting encapsulation and adding hard-coded strings, and writing many properties in user controls will be very time-consuming, given the number of text fields these users have controls.

Can't these text fields in my user controls be declared public, not protected ?

+4
source share
5 answers

(Noting the obvious comments that what you are describing is essentially the opposite of best practice ...)

If you are using a project such as a web application, you must have a designer.cs file for each UserControl. This contains the declaration of each child control, so you can change the public access modifier.

If you are using a website type project, you should probably convert it to a web application. According to Microsoft (and backed up by experience), the type of website is not intended to be used when you plan to write advanced code that goes beyond a single code.

+4
source

If I need to do this, I will write a public property that provides the controls. However, it is usually possible to rewrite information in such a way that you do not need to expose internal controls. If all you need is a value, create a property that returns a value. If you need client identifiers, perhaps creating a client object that provides values ​​or events will solve the problem. Remember, as soon as you make it public, you sign a contract and changing this contract will almost always hurt.

+2
source

You can set it as a property from the code behind. You can really access these properties from code, not from the ASP.Net developer. This is an example in vb that provides a DropDownList in a user control (and maybe this is not the best practice, but it certainly means that the code is written to display each property of the child controls):

''' <summary> ''' This is the exposed DropDownList for this control. ''' </summary> ''' <value></value> ''' <returns></returns> ''' <remarks></remarks> Public ReadOnly Property DrowDownList() As DropDownList Get Return ddControlList End Get End Property 
+1
source

Why not let the user manage the TextBoxes collection, expose the collection as a property and in its Init () method, just add all your text fields to the collection?

So you have a collection that you can search by ID if you ever need, without relying on any page logic, and you only need to open one property. If you create your own collection class for yourself, you can even program several convenient methods for accessing text fields as you need.

0
source

Derive all web user controls from a common base class and expose this function as a public method (or methods). Your base class can be obtained from UserControl (as usual, as usual .ascx), and your controls in turn receive from it.

Then, even if using reflection seems a little work, you do it only once. If the text fields are dynamic and you want to avoid hardcoding errors, this will be the way to go.

So, if you just need to get the value of the text field by id from the parent, you can add something like the following to your base class:

  public string GetTextboxValue(string id) { string textValue = string.Empty; Control ctl = FindControl(id); if (ctl.GetType() == typeof(TextBox)) textValue = ((TextBox)ctl).Text; return textValue; } 

I would say that you need to go this route compared to the previous proposal to open a TextBox, since you really only want to read the text value from the parent and not subject the entire object to reading / writing.

0
source

All Articles