ASP.NET Server Controls

Posted August 23, 2002

Chat Date: August 22, 2002

Chat Participants:

  • Donny_Mack, MVP
  • Doug_Seven, MVP
  • Alex Lowe, MVP
  • John Perry, MSFT, Program Manager

Host Guest_JPerry_MSFT
Hi everybody - this is a great turnout already.

Host Guest_JPerry_MSFT
Today's chat is on Server Controls in ASP.NET

Host Guest_JPerry_MSFT
We have some special Hosts today, and I'm going to ask them to introduce themselves:

Host Guest_Donny_Mack_MVP
I’m Donny_Mack, an MCSD, ASP.NET MVP, and have written a couple books about .NET. I'll be doing my best to answer any questions you may have.

Host Guest_Donny_Mack_MVP
If we're unable to answer your questions please email us offline. My email: donny.mack@dotnetjunkies.com

Host Guest_Doug_Seven_MVP
I'm Doug Seven - ASp.NET MVP and co-founder of DotNetJunkies.com.

Host Guest_Doug_Seven_MVP
I (along with Donny Mack) have an ASP.NET Controls book coming out soon that covers custom control development as wellas basic and advanced user control development.

Host Guest_Doug_Seven_MVP
Glad to be here. (doug.seven@dotnetjunkies.com or IM: doug_seven@hotmail.com)

Host Guest_Alex_Lowe_MVP
I'm Alex Lowe - ASP.NET MVP, AspAlliance.com Columnist, AspFriends.com Moderator - alowe@aspalliance.com

Host Guest_JPerry_MSFT
And I'm John Perry, Program manager for Communities for ASP.NET.

Host Guest_JPerry_MSFT
You can input your questions in the Input room, below, and we'll get some answers out to you.

Host Guest_JPerry_MSFT
Let's go!

Host Guest_JPerry_MSFT
Q: I programmed a control that put its entire rendered output into viewstate. Immediately after the line that did that, I used the debug window to verify that the output was indeed in viewstate. However, on a postback the control does NOT have ANYTHINGin that viewstate spot, so I'm wondering what happened. Can you out line the step by step checklist to putting things into and getting things out of viewstate?

Host Guest_Doug_Seven_MVP
A: russnem: Make sure you are creating an instance of the control in the Init event. This will create the instance prior to the Loadevent. In the Load event you can then populate its properties from ViewState.

Host Guest_Doug_Seven_MVP
A: weewww - That's about the best solution, unless you want to create your own DataGrid from scratch.

Host Guest_JPerry_MSFT
Q: I assigned an Onclick event to every cell within the header row and manually tied it to the "__doPostBack" event. Is there better way?

Host Guest_Doug_Seven_MVP
A: weewww - That's about the best solution, unless you want to create your own DataGrid from scratch.

Host Guest_JPerry_MSFT
Please, feel free to post your questions on ASP.NET server controls in the input room...

Host Guest_JPerry_MSFT
Q: russnem : Create an instance of the control in what init event? I don't understand what you mean.

Host Guest_Doug_Seven_MVP
A: russnem - At the end of the Load event, put the data in ViewState using ViewState("MyData") = "Whatever".

Host Guest_Doug_Seven_MVP
A: Then in the Postback, you create the control instance in the Init event. In the following Load event you can get the data byusing String MyData = ViewState("MyData").ToString();

Host Guest_JPerry_MSFT
Q: Mamanze : Usual dynamically added controls structure here, everything is put in place during the Init event. The ViewState is saved correctly, but not retrieved properly. Any ideas what could be causing this problem?

Host Guest_Doug_Seven_MVP
A: Mamanze - This is similar to the previous question. Ensure that the control instances are created in the Init. What error are you getting?

Host Guest_JPerry_MSFT
Q: scott : All of our client’s sites are Hosted on one machine. All the sites use the same base custom server controls which are located in the global assembly cache. Once the projects are built there is no way to get the components to update.

Host Guest_JPerry_MSFT
Q: scott : . ie. If the version of the component changes from 1.2 to 1.3 they do not get the update. Is there a way to include the latest version of the assembly automatically?

Host Guest_Donny_Mack_MVP
A: There is no automatc way to make your application point to the new version. You'll have to make an entry into the web.config file.

Host Guest_Donny_Mack_MVP
A: Specifically, you'll have to use the <BindingRedirect> tag.

Host Guest_JPerry_MSFT
Q: Aylar : In order to get VS.Net to use IntelliSense on my custom SCs what do I need to do?

Host Guest_Doug_Seven_MVP
A: The EditorBrowsableAttribute class can be applied to any property.

Host Guest_Doug_Seven_MVP
A: <EditorBrowsable(EditorBrowsableState.Always)> Public Age As Int32

Host Guest_JPerry_MSFT
Q: roarfred : When creating subcontrols dynamically in a control, do you prefer using Control.Init or Control.CreateChildControls?

Host Guest_Alex_Lowe_MVP
A: roarfred : What are you doing with the subcontrols?

Host Guest_JPerry_MSFT

Q: Mamanze : I'm not getting an error. I can traverse the control tree within the Init handler at the page level and all the controls are initialized and in the proper place. As I said, the ViewState is being saved properly, just not retrieved.

Host Guest_Doug_Seven_MVP
A: Mamanze - What event are you trying to retrieve ViewState in?

Host Guest_JPerry_MSFT
Q: Lori : Is there any advantage to writing a control in C# vs. VB.net?

Host Guest_Donny_Mack_MVP
A: Just like nearly everything else in .NET it doesn't really matter which language you use - it's what language you prefer.

Host Guest_JPerry_MSFT
Q: Jeffrey : What is the best way to insure that data that may have "<"s in them will not mess up a bound datagrid.

Host Guest_Doug_Seven_MVP
A: Jeffrey - Are you trying to render a < in HTML, or is it an HTML tag?

Host Guest_JPerry_MSFT
Q: bryan : uh, and vb.net doesn't support the events property yet.. whilst c# does

Host Guest_Donny_Mack_MVP
A: That's true, but it does handle most everything that 99.99% of developers will use now.

Host Guest_JPerry_MSFT
Q: roarfred : I'm creating subcontrols based on database content. Needs a unknown number of linkbuttons and link them to events...

Host Guest_Alex_Lowe_MVP
A: roarfred : I would use CreateChildControls in most cases; especially if you are creating a composite or templated control.

Host Guest_Alex_Lowe_MVP
A: roarfred :Yes, EnsureChildControls should be called anytime you or your code depends

Host Guest_Alex_Lowe_MVP
A: roarfred : on the child controls being created.

Host Guest_JPerry_MSFT
Q: augustwind : I thought ASP.Net was up to date with NS 6.2....however the JS generated for my Datagrid button column doesn't fire correctly in NS - - any idea why?

Host Guest_JPerry_MSFT
A: Not offhand, but it could be a bug on either end.....

Host Guest_JPerry_MSFT
A: Why don't you send me some more details (JPerry@Microsoft.com) and I'll see if we can find something out - that may take a little longer than this chat :-)

Host Guest_JPerry_MSFT
Q: bryan : I have a custom server control that has a DropDownList called List in it. I got AutoPostBack to work on the list by adding the list to the controls collection, but whenever it posts back, the SelectedIndex = 0.

Host Guest_Doug_Seven_MVP
A: bryan - Are you handling the postback and setting the SelectedIndex property? If not it may be defaulting to 0 (the init value for an int).

Host Guest_JPerry_MSFT

Q: Aylar : What's bascis of adding child controls to my custom SCs? (An online article or a MS-HELP link would be appreciated)

Host Guest_Donny_Mack_MVP
A: Check out an Warren's website https://www.swarren.net - this site has some outstanding articles on building custom server controls. Including how to using child controls.

Host Guest_JPerry_MSFT
Q: Jeffrey : Generally one would HtmlEncode almost any output from a database. So I would want it to be output as a &lt; .

Host Guest_Doug_Seven_MVP
A: You can do a String.Replace("<", "&lt;") on the data before you render it. Or an HttpContext.Current.Server.HtmlEncode(string).

Host Guest_JPerry_MSFT

Q: roarfred : Will EditorBrowseableAttribute make intellisence work in the html-editor or just make the properties of my control (un)available in the property list? If the latter, can I make intellisence work in the html-editor?

Host Guest_Doug_Seven_MVP
A: roarfred - The EditorBrowsableAttribute will make the intellisence in the editor.

Host Guest_JPerry_MSFT

Q: Lori : Is it possible to write a custom server control that would function as a task scheduler? I need task scheduler functionality on a shared server.

Host Guest_Donny_Mack_MVP
A: Please elaborate on what you mean by task scheduler.

Host Guest_JPerry_MSFT
Q: bryan : how can I handle it? and better yet -> why should I have to? shouldn't the handling of it's postback events be encapsulated within it? it's a web control in a server control 8Stormy \

Host Guest_Doug_Seven_MVP
A: If you are creating the DropDownList in the control (rather than inheriting from it), you have to reinstantiate the control on postback, then set its SelectedIndex property.

Host Guest_JPerry_MSFT
Q: elmermorales : I am having problems using StringCollection as a property. At design time, I get the StringCollection editor and I am able to enter data in it but it only keeps it in memory. It shows it to me if I go back to it but it does not add the p

Host Guest_JPerry_MSFT
Q: elmermorales : the source. If I close the page and reopen it, the items are gone. Any idea why?

Host Guest_Alex_Lowe_MVP
A: elmermorales : Did you write a custom designer for the control that is not pushing the changes back through the designer to the control?

Host Guest_JPerry_MSFT

Q: Lori : I want to be able to send routine emails out 1/day, but I can only send 20 at a time - so I will need some type of queuing.

Host Guest_Donny_Mack_MVP
A: You'll need to use a windows service or windows application for that type of functionality. Server controls will only execute when a page is executed that contains that specific control.

Host Guest_Donny_Mack_MVP
A: More information on windows services: ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbconIntroductionToNTServiceApplications.htm

Host Guest_JPerry_MSFT
Q: Jeffrey : If the data is bound. You are suggesting a lot of programming to do what would be a simple bind to innerText on almost any other control.

Host Guest_Doug_Seven_MVP
A: Not really. If the data is not encoded for HTML rendering, then you have to encode it somewhere.

Host Guest_Doug_Seven_MVP
A: String myData = reader["MyData"].ToString().Replace("<", "&lt;");

Host Guest_Doug_Seven_MVP
A: or String myData = HttpContext.Current.Server.HtmlEncode(reader["MyData"].ToString());

Host Guest_JPerry_MSFT
For anyone just joining us, our topic today is ASP.NET server controls. Special Guest

Hosts, The DotNetJunkies; Doug Seven and Donny Mack, along with Alex Lowe.

Host Guest_JPerry_MSFT
Feel free to post your question in the Input room Below

Host Guest_JPerry_MSFT
Q: gforce : how do you pass data to server side controls, from a form with user interaction in a secure way without https??

Host Guest_Doug_Seven_MVP
A: You really can't. Without SSL the form values are passed via HTTP in clear text.

Host Guest_JPerry_MSFT
Q: dave : What is the best way to expose an error string programmatically in script when it is generated in a control. In a component you could have obj.error return after a method call, but here ID.error does not populate until the label control is alrea

Host Guest_Donny_Mack_MVP
A: Can you clafiy a bit. Are you wanting to get an error that occured in a custom control from a web form (Host)

Host Guest_JPerry_MSFT

Q: bryan : ok, so I have a child control in a server control, it's a DropDownList -> how do I handle it's postback so that I can set it's SelectedIndex?

Host Guest_Doug_Seven_MVP
A: If you inherit from the DropDownList you don't have to handle it. Since you are adding the DDL as a child control, you do.

Host Guest_JPerry_MSFT
Q: and like I said, why should I even have to do that? shouldn't it be encapsulated? Sounds like bad architecture to me

Host Guest_JPerry_MSFT
A: dave : Yes, with a custom server control, you could have the control return a label with the error however the idea is to have the text available to the script which is calling the custom control. Action could then be taken in the page based on the retur

Host Guest_Donny_Mack_MVP
A: Then you can create your own custom event in the control ( ErrorHappend ) and create the event handler in the object it's included in ex. webform.

Host Guest_Donny_Mack_MVP
A: Here's more information on creating eventhandlers: ms-help://MS.VSCC/MS.MSDNVS/csref/html/vcwlkEventsTutorial.htm

Host Guest_JPerry_MSFT
Q: bryan: how do I get access to it's postback data then to set it's selected index?

Host Guest_Doug_Seven_MVP
A: bryan - Once you define the DDL instance, you should be able to do this:

Host Guest_Doug_Seven_MVP
A: myDDL = (DropDownList)this.FindControl("myDDL_ID");

Host Guest_Doug_Seven_MVP
A: That should set the Selected Index correctly.

Host Guest_JPerry_MSFT
Q: elmermorales : JPerry_MSFT - Back to my StringCollection question, I am not using a custom editor. I am trying to use the standard SC editor. Any idea?

Host Guest_Alex_Lowe_MVP
A: elmermorales :Do you have a property that returns the collection (you need to)? And, is that

Host Guest_Alex_Lowe_MVP
A: elmermorales: collection a default inner property and named default property on the parseChildren

Host Guest_Alex_Lowe_MVP
A: elmermorales: attribute? If yes to question 1, then you might be getting PROPERTY="(Collection)" on

Host Guest_Alex_Lowe_MVP
A: elmermorales: your server control's HTML then you need to make the collection a default inner

Host Guest_Alex_Lowe_MVP
A: elmermorales: property and named default propert on the parseChildren attribute.

Host Guest_JPerry_MSFT
Q: Lori :Do you have any tips for enhancing the performance or reducing the file size of the server controls?

Host Guest_Donny_Mack_MVP
A: The best thing you can do if you really need to use them is to turn: EnableViewState off. Other than that, I HIGHLY recommend only using server controls if you have to.

Host Guest_Donny_Mack_MVP
Essentially, don't use them just because they're there.

Host Guest_JPerry_MSFT
Q: gforce : is it possible to extend/override the .net control communictions between the client/server controls to implement your own security protocol for passing control events/data

Host Guest_JPerry_MSFT
A: Since the communication there is HTTP, there's really no way of altering that process simply.

Host Guest_JPerry_MSFT
Q: Lori : Can you explain that more? I am currently using server controls for any function that I commonly use in my code. Is that a performance drain? How?

Host Guest_Donny_Mack_MVP
A: Here is some further reading for performance tuning that will explain in more detail: https://www.dotnetjunkies.com/quickstart/aspplus/

Host Guest_Donny_Mack_MVP
A: click on performance tuning tips on the left menu

Host Guest_JPerry_MSFT
Q: nicewalker : Is there a best way to register a event handler for a subcontrol which like a control in the DataGrid control? because I cant see these events of the subcontrol on the property view at design time.

Host Guest_Alex_Lowe_MVP
Q: nicewalker : Check out https://msdn.microsoft.com/library/en-us/cpguide/html/cpconcompositecontrolsample.asp which builds a composite control with subcontrol with their own events.

Host Guest_JPerry_MSFT
Q: gforce : so what your saying is the data transmission between client/server for server side controls is in-secure and open to hacking, if you need secure data transmission between client/server then you must use HTTPS

Host Guest_JPerry_MSFT
A: Any HTTP transmission is like that. This is why a site which needs security will use HTTPS.

Host Guest_JPerry_MSFT
Well, that's going to wrap up our chat today....

Host Guest_Donny_Mack_MVP
Thank you all - please visit DotNetJunkies.com for answers to more of your questions!

Host Guest_JPerry_MSFT
This has been a great chat - Glad you could make it :-)

Host Guest_Doug_Seven_MVP
Thanks all....we'll be around the Internet...track us down if you need us!

Host Guest_Donny_Mack_MVP
Yes, we know :-)

Host Guest_JPerry_MSFT
Join us Sept 12th for a chat on ASP.NET Security.

Top of PageTop of Page