Pages

Ads 468x60px

Labels

Sunday, March 3, 2013

Custom Controls 3






 That outputs Will reinvestigate our state drop-down list treating it as a custom control this time our user control version of the state drop-down list control was fine but what if you want a custom control what if you wanted to appear the toolbox like other controls what you want compiled into a separate assemblies was easy to deploy like other controls are sample includes a state drop-down list custom control in order to use this you to add the state drop-down list control the toolbox attitude to page set.

 Its auto post back property to true add code to do something with the selected value better rendered this control you have to remember that because it's a custom control it has to render all of its own HTML you need to end up with HTML that looks something like this that is we the select element a name property and ID property ASP.net requires the ID property then we need option elements for each option in the list and one of them is selected so we need the selected attribute for the one that is selected along with value and text information.

 For each item I like will we created the user control for the custom control we have to write code bit by bit output exactly this HTML to make this happen were going to use the HTML text writer class which you've already seen it in action the render and render contents methods of the base web control class are passed out HTML text writer object it's a stream into which you have to write the HTML output this class provides many methods and properties for creating HTML content it also contains several enumerations containing constants as well your party see that because we use the constants for creating a span element I've loaded the state list solution which includes our state drop-down list web control.

 You can see here we are class that inherits from the web control class before we investigate this code I'd like to try it out to do that I'll add to my solution a new website cultures ASP.net websites in the filesystem the name doesn't really matter to me saw click okay let's build this thing there we go and show this in design view and now in the toolbox there's a state drop-down list Dragon instance of it onto the page there is the state drop-down list that me set this is the start of project tries to run this where person five we make sure the auto post back property on this control is set to true there we go is for the text box on here as well there we go and now I'd like to react to an event of this control saw go to the events list and choose the selected index changed event actually I can make it easier I just double-click on the control and this goes automatically to the selected index changed event of this control and that requires some effort.



0 comments:

Post a Comment

 

Sample text

Sample Text

Total Pageviews