Building Server Controls

Posted April 8, 2002

Chat Date: March 21, 2002

Chat Participants:

  • Rob Howard, Program Manager
  • John Perry, Program Manager
  • Susan Warren, Program Manager

JPerry_MS
Welcome to today's ASP.NET chat on Building Server Controls. I will ask the hosts to introduce themselves.

RobertHoward
Hi, my name is Rob Howard. I'm a program manager on the ASP.NET team.

SWarren_MS
Hi, I'm Susan Warren. I'm a program manager on the ASP.NET team for pages and controls.

JPerry_MS
And...I am John Perry, Program Manger for the Microsoft Online Communities. Glad you all could make it today!

JPerry_MS
The hosts will make an effort to answer as many questions as they can. There are times where a question may be asked that the host does not have an answer for. We will encourage you to post any of these questions in our newsgroups.

JPerry_MS
Let's get started! Fire away with your questions for our hosts.

SWarren_MS
Q: Can custom server controls be deployed the same way as the default asp.net controls without having to register them on every page?

SWarren_MS
A: In v1, you do have to register them on every page. We're looking at ways to make this easier in a future version, for example registering them once for an entire application in the web.config file.

JPerry_MS
Great questions so far....answers are on the way....

JPerry_MS
Q: Avi : JPerry_MS, are there more controls that MS publishes on the web (like MSDN?)

JPerry_MS
A: There are many Third party controls available.

RobertHoward
Q: Is there any way to make user control properties appear in the control properties page in the web form designer?

RobertHoward
A: Yes, by default they all appear. But using custom attributes you can have more granular control over how those properties are displayed in the designer. For more details check out the white papers on the Controls tab at www.asp.net.

RobertHoward
By using custom attributes...

RobertHoward
Q: Where is the best online resource to learn about developing asp.net server controls?

RobertHoward
A: A great place to start is the control gallery at www.asp.net. We point to several key white papers, as well as having many controls with source that you can download. Some great server control books will be coming out shortly...

JPerry_MS
Q: EugeneDog : Is there any chance of having the source to the IE Web Controls made available as an example of writing controls?

JPerry_MS
A: It's not currently in our plans.

SWarren_MS
Q: Can we start off with a clear description of the differences between "Server Control" "User Control" "Web Control".. they all seem the same to me with the exception of "Mobile Code" which runs on a client

SWarren_MS
A: Great question. A server control is a component that is created by your page on the server at runtime. It has properties, methods and events, just like a VB6 windows control if you know that.

SWarren_MS
The difference is that a server control renders it self to the browser as HTML, script, WML, etc. A "Server Control" and "User Control" work exactly the same

SWarren_MS
when the page is executed. The real difference is at design time -- you author a User Control just like a page.

RobertHoward
Q: Follow-up to the additional controls: Sure, there are 2rd-party controls, but they cost money - is MS planning any additional controls (or are there any addl available now)?

RobertHoward
A: Yes, there are several posted by both Susan and I as well as several others...

RobertHoward
Q: Robert: are you referring to the controls at asp.net?

RobertHoward
A: Yes

SWarren_MS
Q: Can a custom control that is being used in multiple projects, such as a control that consumes a web service, be deployed in such a way that it can be updated without having to rebuild each project that references it?

SWarren_MS
A: yep, this is one of the big advantages of custom (precompiled) controls over user controls. You can install a custom control to the Global Assembly Cache on the server so that every application on the server can share it.

SWarren_MS
It also makes updating the code easier. To avoid updating references, you can keep the version number static.

RobertHoward
Q: Follow up: the properties that I added to my user control can be set in the tag attributes, but do not appear in the control properties dialog in VS.NET.

RobertHoward
A: To be clear, only compiled controls have their properties available in the designer. This is one of the limitations of user controls. Didn't fully read the question last time :)

SWarren_MS
Q: can a DataGrid work with an array for a "database"?

SWarren_MS
A: yes, you can bind a DataGrid to a string array for example

SWarren_MS
Q: Where is the best online resource to learn about developing asp.net server controls?

SWarren_MS
A: I'd like to add some additional links to this:

SWarren_MS
https://msdn.microsoft.com/library/en-us/cpguide/html/cpcondevelopingwebformscontrols.asp

SWarren_MS
https://www.swarren.net -- this is my personal site :) I have a couple of control building Zips up there with lots of sample code in C# and VB

RobertHoward
Q: Are the Validators considered a Server Control?

RobertHoward
A: Yes, the validation controls are server controls.

SWarren_MS
Q: There are some major stumbling blocks associated with postback events, a simple example would be a datagrid with paging, there is no way to bookmark a specific page because of the form posts generated by asp.net. Is there any way around this problem?

SWarren_MS
A: Your control could retain the bookmark of the last page viewed in ViewState.

SWarren_MS
Q: I noticed that the ASP.NET Controls have an extremely efficient way of storing ViewState. Do the controls encode their own viewstate values using some special algorithm? ViewState for my controls is always much larger when I try to duplicate a control

SWarren_MS
A: There are a couple of factors that affect viewstate size. One is that the classes that serialize ViewState are optimized around some simple types like String and Integer. If you are serializing more complex types the result will be both slower

SWarren_MS
performance and bigger viewstate size. Second, you should only stored the non-initial state of the control in viewstate, never property values that are always set when the control is initialized.

SWarren_MS
If you are creating your control by building up a collection of child controls, there's a trick here: first create the child control, next set the initial state values on it THEN add it the your control's Controls

SWarren_MS
collection. Properties you set before adding the control to the Controls collection are not added to ViewState.

JPerry_MS
Q: Scott : Is ViewState maintained over multiple browser sessions?

JPerry_MS
A: No, per instance of the control.

RobertHoward
Q: Are server controls always compiled? Whereas UserControls are basically "include" files.. (trying to understand this)

RobertHoward
A: Yes, server controls are compiled. User Controls, however, should not be thought of as include files. They are also compiled behind the scenes and an instance of the user control is created (and rendered) within the page. User Controls are completely

RobertHoward
...encapsulated v include files.

RobertHoward
Q: Is there a way to turnoff IE history so that Web Forms behave more like an application?

RobertHoward
A: If you use the following output cache directive <%@ OutputCache Duration="0" VaryByParams="none" Location="None" %> we'll

RobertHoward
A: ...send an http expires tag of -1 which tells the client not to cache the response

SWarren_MS
Q: (re viewstate) Like the DropDownList using triplets and object arrays to store the items

SWarren_MS
A: Viewstate size is directly affected by the number of objects you put into it, in addition to the values in the objects of course. So a two-dimensional string array will be smaller in viewstate than

SWarren_MS
two string arrays. The triplets/object arrays are tricks we use to take advantage of this fact. You can also write serializers for your own types. You might want to check out my viewstate column

SWarren_MS
for a more detailed discussion (sorry can't find the url just now -- John is looking now).

JPerry_MS
that would be https://msdn2.microsoft.com/en-us/library/ms972427.aspx

SWarren_MS
Q: The real difference is at design time -- you author a User Control just like a page. Then how is a Server Control written? As strictly a CSharp .cs file?

SWarren_MS
A: yes you author a custom control as a class file in any .NET language. Guest_IgorKo has returned.

SWarren_MS
Q: I have a custom control and have built a designer (different namespace), in the same solution I have a web project that uses this control. If I update the designer, this is not reflected in the web project: I have to close and reopen the solution.

SWarren_MS
A: The trick is in how you set up your VS.NET project. First, DO NOT author in the control in a web application -- it should be in a separate Web Control Library application. This produces a DLL, not a web application. Second,

SWarren_MS
add a Web Application project to the *same* solution. This will serve as your test harness for your control. Finally, you need to establish a project-to-project reference from the web application to the control library application. The

SWarren_MS
easy way to do this is to add your control to the toolbox and drop one on a web page -- VS.NET will take care of setting up the reference. Now changes to your control designer will be reflected immediately in your test page. One more thing:

SWarren_MS
There are a couple of properties of the control that are stored on the toolbox itself: the path to the dll, the Toolbox Data and the icon image. If any of these change you'll need to re-add the control to the toolbox.

SWarren_MS
Q: Is there a particular reason the components of many of the controls have been sealed? Such as ListItem and ListItemCollection? It would be awfully nice to inherit from them.

SWarren_MS
A: We agree. This was an unfortunate oversight in V1 and we're discussing now how we might fix this going forward.

SWarren_MS
Q: rather than send the HTMl from a control to the browser, can it be saved into a variable, e.g., strVar = myDataGrid.toString

SWarren_MS
A: Sure. In a custom control you control in code what gets sent to the browser, but other code can run in that same method. For example, you might increment a perf counter. I'd just caution you to think about the performance implication of adding to th

SWarren_MS
Q: If viewstate is destroyed how can this be used for the bookmarking problem?

SWarren_MS
A: Sorry scott, I think I misunderstood your scenario (I thought your bookmark was scoped to the users interaction with the page for a given session.) Perhaps you could explain the scenario more fully?

SWarren_MS
Q: lets say I have a website that I want to have a template (like a nav. tree on the left, standard header @ the top) much like MSDN, however, I want my designers to be able to edit the whole site, w/o messing things up, I was thinking of using a (ctd)

SWarren_MS
A: I think this is beyond the scope of what you would normally want to do in a control. Have you thought about creating a base Page class that does this?

SWarren_MS
Q: I really like .net because when I use the attribs of web controls, it works on NN4, however, is there a way to make it so that when I use the cssclass attrib it does the same thing

SWarren_MS
A: CssClass just renders as class= on the rendered tag. If you css class is written to work in NN4, then this is a great way to get nice cross-browser appearance for an application. See the IBuySpy samples on www.asp.net -- they make extensive use of Cs

SWarren_MS
Q: Why do the asp.net server controls use a naming convention more like the forms in vb than styles in IE? eg. "ForeColor=''" and not "color:" to change the font color

SWarren_MS
A: This was hotly debated when we designed the controls, but the bottom line was that HTML was quite inconsistent in it's naming of properties between elements. We ended up choosing one consistent scheme that we hope is intuitive to the majority of users

SWarren_MS
Q: Is the Validator Server Control extendable and modifiable? If so where is this done?

SWarren_MS
A: yes, you can derive from the System.Web.UI.Webcontrols.BaseValidator control. It's pretty easy and there are some examples on the web. (Don't have a specific url handy but you could do a search)

SWarren_MS
Q: thanks swarren but following up : I have three projects in the solution, myObj, myObjDesigner and myObjTestApp. However, if I change the behaviour of one of the verbs in the designer, it is NOT relfected in the .aspx file in myObjTestApp.

SWarren_MS
A: hmmm hard to answer in this forum. Can I suggest you post this question to the newsgroups?

RobertHoward
Q: Are there plans to allow .NET code to run in the browser like a Java Applet?

RobertHoward
A: Yes, you can do this today. If the client has .NET installed, you can host Windows Forms applications in the browser. We've got an example in the Quick Starts for Windows Forms.

SWarren_MS
Q: How do we build our Controls in such a way as to make them available for validation by the standard validators? I get "This Control Cannot Be Validated" when I try.

SWarren_MS
A: There's an Attribute called ValidationProperty. You can use to specify one property on your control with this attribute, and it makes the control compatible with the validator architecture.

SWarren_MS
For example, our TextBox control has this attribute set as follows: [ ValidationProperty("Text") ] public class TextBox ...

RobertHoward
Q: ...Chat Client etc.. like Java does. And does the client need to have the entire 18meg Framework on their computer to run this?

RobertHoward
A: You definitely could, I don't think anyone has taken the time to build it yet - you could be the first (and post it at www.asp.net!).

RobertHoward
A: Note, there is a chat control in the asp.net control gallery - not sure how it works. Yes, if you used a hosted Windows Forms control

RobertHoward
A: in the browser, it would require that the client has the redist version of the .NET Framework, ~20mb

RobertHoward
Q: How does one send an image with a custom control (like a graph or something) if there is not, MS should make a way, it would be really nice

RobertHoward
A: You would need to make a custom image generator that the control could reference through an image tag. I've got a sample of this at

RobertHoward
A: www.gotdotnet.com/team/rhoward - look for the code to the tips and tricks talk. You'll want the BarGraph.aspx example.

RobertHoward
Q: Can server controls be run in the any browser and any version?

RobertHoward
A: Server controls run server side, they emit markup, e.g. such as HTML. There are no browser dependencies.

SWarren_MS
Q: from Scott: Using a datagrid that has events added for paging, there is no way for a user to add an Internet Explorer bookmark ...

SWarren_MS
A: thanks for the clarification. If bookmarking is a requirement, you may wish to think about authoring the page in a different way. Instead of enabling the DataGrid's built-in paging ui, write your own custom paging

SWarren_MS
ui and pass the page parameter on the querystring. That would allow users to bookmark a specific page.

RobertHoward
Q: Robert it is too old... The chat control is dated 1996...

RobertHoward
A: I'll have to look into that....

SWarren_MS
Q: Is there an example other than in JAVA to create a simple CHAT room control source code?

SWarren_MS
A: I'm not sure, but I think www.eraserver.net might have one. But what a great opportunity to build a great new control if they don't have one :) No java needed!

JPerry_MS
validator link on it's way.....

SWarren_MS
Q: SWarren_MS: Could you find me the link resources of deriving from the BaseValidator control... that would be very helpful.

SWarren_MS
A: John is working on this right now :)

JPerry_MS
Here's the MSDN Link -

JPerry_MS
https://msdn.microsoft.com/library/en-us/cpguide/html/cpconvalidatorcontrolsamples.asp?frame=true

JPerry_MS
This has been a GREAT chat. Thank you to everyone. Unfortunately, it is just about time to go.

SWarren_MS
Q: How does one send an image with a custom control (like a graph or something) if there is not, MS should make a way, it would be really nice

SWarren_MS
A: The way to understand this problem is to remember what the browser needs to see an <img src="someimage.gif"> tag. This is what your control should render. But

SWarren_MS
you could also create a separate page that actually rendered the image using the System.Drawing classes in .NET.

SWarren_MS
Well our time is up unfortunately -- thanks everyone for the great questions!

JPerry_MS
Just to put some links up here....

JPerry_MS
https://msdn.microsoft.com/library/en-us/cpguide/html/cpcondevelopingwebformscontrols.asp

JPerry_MS
https://www.swarren.net

JPerry_MS
https://www.asp.net

JPerry_MS
newsgroups: https://communities2.microsoft.com/home/default.aspx?lid=28001275

Top of PageTop of Page