Pages

Ads 468x60px

Labels

Monday, March 11, 2013

Custom Controls 4






 You'll see later that you have to do something to make that happen and here all the set text box 1.txt equal to state drop-down list one.value thought you take so will take the value of the drop-down list and place it into the text box Save it all was run this page will selected value and it works correctly doesn't work faster than the user control that though once it's on the page ASP.net doesn't care with the code came from it still works the same way the real differences this control has a slightly better design time experience.

 Because it appears on the toolbox weakens drag it onto any page and what sincere we treat it like any other server control how does this control to his job let's look at the code before go to Florida here I want to look at properties and methods of this HTML text writer class we might be using is not look at the code yet was look at properties methods of writer the one that's useful is added attribute at attribute adds the specified markup and value to the opening Tagamet element that's useful we had already seen the right method there's the right attribute method.

 This one just writes the markup attribute value to the output stream were ever we are within the output is also you see the right begin tag right begin tag begins to write the output of the beginning of a tag) tag writes the complete and tag out there's right lie which rights out line obviously there's a default tab string the default tab string present a single tab character in case what is a tab into the output of your HTML and there also is the tag right care would outputs the closing angle bracket so should you want to use this writer to piece together the beginning portion of an element that some attributes in the closing right character you would use of this property to do the work so.


Now let's start back and look and see how this control doesn't strong the first thing it does is in the public constructor is called the base constructor and tells it what kind of element were creating this creates the output select tag around all the contents of this control is nice of the constructor accepts a parameter so you have to worry about creating the outermost element if step one was creating the control and calling the base class's constructor then step two involves adding control attributes this control requires some attributes it needs the base class attributes like style and position those things are generally provided by the base web control class we also need a unique ID which is required for data post back remember were try to keep track of the selected state we have survival post back sweetie unique ID to do that if auto post back is true we need an on change attribute which indicates client-side script to run.



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.



Friday, March 1, 2013

Custom Controls 2






 And quick to render HTML content at this point were just slamming out the text of this control will lets you use this control on the page go to add it to my solution a website that's fine that's fine that's fine limit rebuild everything by rebuilding everything I allow Visual Studio to play a fun trip and that is here in design view in the toolbox window once I've built my solution Visual Studio season has this server control available for use on this page it looks like a server control.

So they supply it as an option on the toolbox window I could drag this onto the page and because right now my server control has no text property it displays its ID property in square brackets the class they the server control one as the first instance of that control so as a server control one number one and if I come your change the text to the obligatory hello world the control updates to display that text will lets you run this page and see what's really going output when ASP.net renders the page with ASP.net renders this page we see in the browser for look at the source here a span tag.

 Which contains our control and here's hello world actually the span tag is how ASP.net represents a label is treating this like a label control well that's good enough we see that they've managed to spit out the HTML is required for this control what if I want to say a few have some I can come to my control and here in the override for the render contents method I just need to spit out some more HTML so I need to come out here and add a call to output.adding some way to write out the beginning of the span tag is a right for all begin tag methods courses also a right begin tag method which rights the opening tag with room to put attributes so the right begin tag won't put the closing bracket write full begin tag closes the opening tag of our span element.

 So I need here to figure how I put the span out this needs to be a strain I could just type the word span about 12 I will use HTML text writer tag is I love this because they give us enumeration which is all of the possible HTML elements I want span.2 strings there we go that's the begin tag at the end money the output. Right and tag HTML text writer tag.span.to string there we go save this go back to our page this rebuild everything I was view the page the browser and see what happens it looks the same if we look at the source you see that what we spit out now is a little more complicated the contents of our external span contains an internal span with the text property of our custom control in there so we've created a custom control and modify the HTML.




Tuesday, February 26, 2013

Custom Controls





Introduction

Will need to create user control really just pages embedded within other pages that you can use that technology to create controls like the other controls that are built into the.net framework if you want to create controls the look and feel like the built in one's got right some code does the way around it will take a little more effort but you'll see that the benefits outweigh the effort because you'll have complete control over what you're controlling this in the output page in this section will look at starting with the basic template that Microsoft provides for creating a custom control and then we'll revisit the state list user control and we write it as a custom control to see how much effort is involved will finish by adding some ease-of-use features such as providing a default property a default events for the custom control.



Custom Controls   1

In order to create the new custom control all create a new project this project will be a web project@ASP.net server control all except the default name and location that's fine with me and yes I will not save my changes to that project in the new custom control project there's a class name server control 1.VB first of all note that this thing inherits from the web control class the web control class provide some built-in functionality for creating custom controls here we have a number of attributes that define the behavior of this property but we also have make it clear it user rated we have a text property this control.

 This sample template if we disregard these attributes for now of these attributes will come back and look at those later the text property is pretty simple it has a string tests it gets its value from of view state variable name text this verbal stored in view state so it will survive a round-trip to the server along with this page if S is nothing then we return the name of this control less of the idea of the controls in square brackets otherwise return the text which came from the view state property that's the property getter the property setter this takes the value we want to supply the text property and puts it into the view state variable name text.

 So that's the code that handles getting and setting the text property of this control that's really the only property has there's also down here and override for the render contents method of the web control base class the render contents method gets called with ASP.net needs to draw the contents of the control onto the output page and what is this control do when asked to render its contents does nothing more than take its text property and white it into the output stream the HTML text writer represents the stream into which were writing the HTML output the additional text writer has a couple of tricks up its sleeve that make it easy.





Wednesday, February 20, 2013

User Control 6





 Which if you remember call to find index method figures out which index to go to and sets the drop-down list to show that value will that's all the code there is to make of this state drop-down list control work was review how it works also led to stay here and when I do the selected index changed event of the internal drop-down list raises which causes the user control to raise the selected index changed event which causes this page to refresh itself and placed into this text box the selected value if I type a value here and click set value it sets the value property of the user control to match the value of the text in the text box





Summary
It is unfortunate that the two names for the two types of the controls you can create yourself or so confusing what the user control what's a custom control really a user control is like a little page to drop on to another page so-called page letter something but it's completely unambiguous a user control is a control you create by dropping other controls on wood design surface and vendors adding code custom controls.
 Control you create from scratch is the topic for different section but in terms of user controls To stop here the user before we just created seems to have a lot of capabilities it easy to create user controls but what if you want to package and create controls like the normal server controls what you want to add your controls to the toolbox on the design surface what if you want to use the same control across multiple application domains in multiple applications for that you need custom controls and that's the topic of our next section

Sunday, February 17, 2013

User Control 5




 More thought event handler is a little higher up here we are and it's the selected index changed event would like to expose or raise from this user control what I need to do is declare a public event selected index changed I can call it whatever I want but it certainly is more polite to name it something that's familiar to the user or the consumer of this control and that would be selected index changed every defined event in.net has a well-known or fixed signature in this case.
This event handler needs to receive sender as object and be as event darks five declared my event that way I declare the event that I'm going to raise and here in the selected index changed event of my private drop-down list that is inside the control all of you to do is raise the event back out to whoever's listening that is whoever has consumed this control this is how you raise an event from within a user control you receive the event you care about in this case the selected index changed event of the drop-down list of inside the user control and then.

 You raise an event which corresponds to what you want the consumer of your control to see otherwise they can't receive any events of the drop-down list that's embedded inside your user control this isn't a good example of this because our user control contains only a single wheel control if there were multiple controls this would be more obvious because of their multiple controls how would the user control know how to map internal events to external events and so were required to raise this event to the outside world and I'm raising the selected index change that I just defined here passing me that is the user control and be whatever got passed to me here and those are passed out to the consumer of this control.

 Well that's all the code there is so let's look at the completed page to see how it handles the events of this user control here's the user control and you can see that I will see the events here at all on the properties window but if I go into the code window I should be able to go to state list one and five it selected index changed event all these other events listed here those are events that are exposed by the user control container notice there are no other events of a drop-down list exposed here because I did expose and all these other events, for every user control they all get them for free my selected index changed event I've handled by taking the value of the state list and placing it into the text of the text box of course I also have code that takes the text of the text box and shoves it into the value of the state list when I do that I sent the value is the list.


Friday, February 15, 2013

User Control 4




 It once again and looked to see what's actually rendered in this page the page needs to include a select control with a lot of options and so we need our drop-down list to have these options with the text is the long name and the value is the short name will see about to happen here's our control look at the markup is nothing more than well a control with ASP drop-down list on the ASC X page okay.

 Was look at the code for this thing it's all done in the code in the markup that's all there was a drop-down list and the story here in the class we have an array of well at to Colin array of values short name, long name there a lot of other data structures like reviews don't yell at me it was the simplest using an array and that's why went with I like the array because it's easy to initialize at design time so here is the contents of the array and in the page load event handler we fill the list the load list procedure we go there simply clears out the state drop-down list and loops through every item in the states array and for each one adds a new item to the drop-down list by creating a new list item and retrieving the short name.

 Actually is the long name and the short name because of the list item when you create one you generate the text to be displayed first and the value second this is the text to be displayed this is the value other also control properties I did auto post back text and value was look at those here I have auto post back text and value what is auto post back work with it gets the auto post back property of the drop-down list and it sets the auto post back property of the drop-down list can be easier met the value property gets the value of the selected item of the drop-down list and sets the selected index to be the index corresponding to the value supply remember if I type TX and push it into the value of this drop-down list I need to figure out which index that is defined index procedure was a somewhat brute force the five index procedure just scans the list to find a match once.

 It finds a match he returns the index believe me there are better ways to do this I didn't want to dig into here esoteric means of searching data structures like It as simple as possible so back up here I'll find it there we go the value when you want to set we find the index and stick that in the selected index property of the drop-down list finally there's a read only text property that retrieves the text of the list and that returns the text of the selected item in the drop-down list to the properties really aren't very complicated handling the event requires a little.

Wednesday, February 13, 2013

User Control 3






 That is CX here's stateless.ASC X and here is footer.ASC X would define the layout of this page in a way that we can reuse on multiple pages within the site and we started using a user control now going to look at a more complex user control this user control provides a list of states and how often have you built a page.

 Where you need to provide a list of states for your country I'll be using the United States but I'm sure every developer has done this for his or her own locale this control needs to allow you to support auto post back so that choosing estate will trigger some event automatically it needs to provide text and value properties you can look at one view of the state name and perhaps return a different one either show you the short name and return the long name or show you the long name and return the short name.

 It needs to raise the selected index changed event you can react to the event were the user selects a state offices been done for you were going to look at state list.ASC X in the sample project another data has to come from somewhere and we could use XML file or database but a static data we haven't added ministate store list of states a long time so this is hardcoded is actually provided in the control itself the user control needs to provide some properties for example.

 It needs an auto post back Boolean property it needs a text property and it needs of value property will investigate each of these in the code for the control finally the control needs to raise events will declare the event and then raise it when necessary let's step back and look at the state list user control over the completed.aspx page into the browser so we can investigate the behavior of the drop-down list here's our state drop-down list and it displays a list of the United States the long names if I choose one it's short name appears in this text box there must be some event that occurs my select one and we retrieve.

The value of the drop-down list and place the value in this text box there is also the ability to set the value suffice set this to a state and click set value it sets the value of this list so you can see we can retrieve the value when the user makes a selection and set the value based on some external event while that's pretty much all this thing does not too exciting but it does satisfy a need that is it encapsulates some commonly used functionality lets look at the source code to see how it works here we have state list.ASC X and all view the designer first well it's a drop-down list that spirit not much here but what needs to be the output of this drop-down list and it shows before was run.

Monday, February 11, 2013

User Control 2









So now how do we use this thing already how the page onto which I'd like to place this order hitter the before footer page consists basically of some header information some content and now to use footer I just take it and drag it onto the page went there we go cut with above the right place I wanted to was drug or I do want it which must be down here there we go so we now have the footer dragged onto the page will use it done note that if you're coming from a previous version of Visual Studio you might notice something different user controls in early versions of Visual Studio.
 Just displayed a great blog on the page we drop them off in this version we actually see a design time experience which contains the content of the user control the user control has a task pane which includes two items edit user control and refresh context refers content is useful if you modify the user control somewhere else and edit user control takes you two as you might imagine the designer for the user control itself because on this page I Really modified the user control have to go to its designer when you track the user control onto the page Visual Studio adds that only a reference to the user control.
 Itself but up here a register directive the register directive indicates they were using a user control from some other source and the source areas footer.ASC X there's a tag name we used to refer to this user control and attack prefix every control in ASP.net page that is part of ASP.net and not HTML include some sort of tag prefix like ASP will this user control as a tag prefix of you see one hears the controls tag footer at ID for it every control has to have an idea of play along with ASP.net.
 And is set to run@theserverlikeeveryotherASP.net control so it looks like a normal control it feels like a normal control yet you decided using the ASC X designer here is to prove a point I've gone a little farther on that before footer page there were some header information I might like that on every page as well so I've created a header.ASC X which includes a table and the image of that cute little puppy and the header information and there is my header.ASC X ready to be dropped on a page.
 And I have a page right dropped off completed.ASC X includes header.ASC X and footer is CX and one more user control that will be discussing in the next chunk you can see down here we have our user controls use the header here's the footer and here's the state list will be talking about soon the only constant is defined here is a text box and a button and if we look at the design for the page here's the text box here's the button here's header.

User Controls






Introduction
Him will you have a website which you need to repeatedly use a group of controls that record layout that group of controls on these pages may be copying and pasting from one page to the other that work for one of that group of controls had some code it needed to run in reaction to some user of it in that case what you really need is some way to create your own single control that contains all those controls that.
 You could then drop onto a webpage and have it run some code like a normal control what you can do that you can create a user control that is basically a conglomeration of other controls in addition you can also create a custom control which is a control like those you find a little box in this section will investigate user controls in the next section will investigate creating custom controls.


User Control      1

First are user controls these are really like a little page that you embed within other pages these must have a.ASC X extension and you design them as if they were a normal page you can add code to react to events and then you can treat them as a reusable entity there really are intended for use within a single site however that you can create a custom control a custom control is created from scratch these acts and feel like built-in server controls because they are the same thing as built-in server controls all.
 The server controls that are built in to the toolbox are actually Microsoft provided custom controls these each inherit from a base class and that added members as necessary the result is an assembly which contains the control of a custom controls only job really is to render output just like a standard built-in control its job is to spit out HTML when the page is rendered in this section will begin by looking at creating a simple user control imagine that you want some standard user interface design.
 And you liked implemented across all the pages in your website perhaps your standard menu or footer or hitter on every page you could of course critical master page but that's not the point here let's take a look of example I've already created where I have thought of information in a user control how does get there by right clicked on my project chose add new item and then shows Web user control I'm a bit footer is CX 40 Dundas was canceled for now here is forgot is the X if I look at this markup you'll see that it looks like a page really is very much like a page except that has a control directive instead of a page directive all we have on your right now is a style sheet link and then a horizontal rule and some text for looking design it's not totally overwhelming it's just text okay.



Saturday, February 9, 2013

Health Monitoring : 3







 I need to do is configure web.config open up the web.config file here and I'll go over to the web health monitoring.config this configuration information this health monitoring element of the web.config file let you specify the SQL Web event provider class you can configure this provider and specify the events that you want to monitor in the rules section 2 in this case which is defined in application lifetime events roll specifying all events as well as the provider that is created appear as little bit more to talk about for configuring and using health monitoring the health monitoring section that we just let that contains only two of the elements required for configuration the providers that were going to use in this case a SQL Server and the rules that we want to use there's.

 A third required element called event mappings and this is inherited from the root web.config file the event name value all events that we sign the web.config file refers to one of the event mappings in the root web.config file slots and look at that section or have opened the root web.config file and I scroll down to the health monitoring section. We'll see the built-in event mappings which are named categories of event data based on the event type in a range of that codes the providers listed here are the three built-in providers for sending event data to the Windows event log or to Debbie on my Windows management instrumentation the role specify provider and event mapping and configuration options that you can use to throttle the flow of data and you can use profiles to centralize the configuration of those throttling options by specifying sets of providers rules and event mappings you determine which event data is recorded and where it is recorded.

So scroll down a bit so we have profiles rules and then we have various event mappings settle to find within the root web.config file which means available to all ASP.net applications so sickle look at the view offense page I'll govern the design view and you can see that we just have a simple grid with the SQL data source the SQL data source is configured to read from the ASP net Web event events so can take a look at the source for that and you can see the select statement that is going to be executed in order to read that data and open everything in order by the event in descending order we also delete command defined so that you can delete all existing events in order to clear out the database will use the code behind the page and all we have is within the page unload event is going to clean up all the old events that are contained within the database and to do that all we need to do is call the delete method of the web events data source) run the application and you can see the most recent event data recorded within the database when you close the page then it deletes all of the old events


Summary

If you have a website that has to be highly reliable and scalable monitoring itself with weather events is just what the doctor ordered with performance data in a SQL Server database you can analyze it to your heart's content to find and fix problems before anyone else notices the such as a user who find your site down with the.net is monitoring stuff shouldn't you be to


Health Monitoring : 2




So we dim up a trace context record object which we could do within the forage as well and then look through the collection of trace records and output information about each basically just outputting that category and the message so that had run this application you can see that you get information about all of the events that fired as part of this page as well as the custom trace message that we output from the pages load event procedure instead of displaying the output on the page could also directed to an external source such as a database and in this.

 Way you can create the equivalent of your own trace listener without using system.diagnostics one of the channel providers that you can use to monitor application health saves data to a SQL Server database is a great option because he can send a lot of information and it makes it easily available and has a lot of failover features as well the first step in setting up monitoring is to create the application services database for SQL Server for some services it makes sense to place this data in the app data folder in your application directory that uses SQL express but for event data you probably want to use a single central database as a mentor to look at doing in this example can be pretty tedious to set up a database by yourself.

Say ASP.net provides an ASP.net _ red SQL utility this is part of the.net framework you can use this to create the SQL Server database or to configure options for an existing one and you can find this utility in the Windows/Microsoft.net/framework version number folder on your computer as a couple different ways to run this utility you can run from the command line or you can run it as a Windows wizard application was sickle look at running the wizard the easiest way to run the utility is by using the Visual Studio command prompt this is nice because in early has all the path set up to the various utilities used by the.net framework and Visual Studio rights of the wizard is running out click on the next button and it prompts me for what I wanted to go to configure SQL Server for application services.

But I can also remove the services if I want to take them away from his computer click on next and in this case I'm going to use the SQL express instance on this machine all connected using Windows authentication and only if the default database selection because I will create a new database on click on next and gives me a summary of what is going to do and I'll click on next and I'll take care of configuring the database I think evidence the database is been created or modified right all click out of the wizard and shut down the command prompt and were all set to start saving health information to that database the one other thing.


Health Monitoring




Introduction

ASP.net web events provide a framework that you can plug into in order to monitor your applications help this is another example where ASP.net uses a feature internally and exposes the infrastructure to developers you can use when advanced report on the health of a website and audit you can even develop and race custom events to monitor the things that are important to administrators determine which events.

 They are interested in by using event subscriptions that are controlled by configuration files which can also specify channels through which the events are delivered out of the box channel providers handle saving events to SQL Server sending event reports through email writing events to the event log and forwarding events through Windows management instrumentation.



Health Monitoring   : 1

You had to accept the default rendering and it wasn't easy to redirect output to a persistent store but now ASP.net provides programmatic access to trace output through the system.Web.trace context object using the trace finished event you can hook into trace finished and do whatever you want with the output the trace finished event exposes trace messages as a collection of trace context record objects after all the request information is been gathered by registering a trace context event handler delegate you can iterate through the trace messages.

And write them to response stream a trace context record object encapsulates the trace message category any associated exception and whether the trace record was written by the trace context.warn method in a several properties he can take advantage of the category property gets the user-defined type category for the trace record so like other trace information you can categorize the information that your gathering error info gets an exception if one is associated with the trace record is warning its a value indicating whether the trace record is associated with the trace context.warn method call and finally the message property gets the user-defined.

Trace message that's associated with the object classical look at how you can take advantage of the trace context record object this example shows how to register a trace context event handler to handle the trace finished event this is the code behind the trace context.aspx page in the page load event procedure we have to add a handler for the traced finished event and so forth that we just pass in the address of the on traced finish method and then we go ahead and write some custom information the first thing that the page load event procedure does is it adds a handler to the trace finished .

Event and we pass in the address of the entrees finished method and write out a message to the trace object appears that entrees finished method like pretty much all events within the.net framework he gets a couple of parameters against the Senators object to identify what caused this event or more portly for a purpose here we get this trace context event arcs parameter and this contains a collection of trace records.


Thursday, February 7, 2013

Listeners : 8





So in this example the config file the controls compilation of the component is in the web.config and the important setting here is I have a compiler options attribute that specifies that I want to output trace messages to have compiler options here for both C sharp in VB because you can have code of both types within that Apco directory so by including both have the flexibility of including trace information for any kind of code that I put within that Apco directory that location is special within ASP.net.

Applications right with that the application all set up so go ahead and run until select defaults and executed and I'll click on the webpage trace listener back over to Visual Studio as you can see the trace output is in the output window and I have the system.diagnostics.trace messages included in integrated together with the ASP.net trace information is all integrated together and have a single view of that data will put this back I'm not limited to viewing information the output window I can also included within a page trace.

 The application open up my default.aspx and add the trace attribute save it and I'll run the page again I'll click on the webpage trace listener and I can scroll down here and see that the trace messages from that component are available within the trace information for the page got a nice it gives you a nice unified view of all of the trace information that is generated by the application whether generated by ASP.net or an outside component quantity one of the thing here, scroll down and disable the debug mode by setting the false will save that middle restart the application without debugging all select the webpage trace listener and once again come back and show you that the output window does not include the trace information this is important because a deployed application will not execute in debug mode and so there's no place for that debug information to go but you can still use trace information and output to different listeners and be able to gather it in different ways




Summery

Okay so that's a pretty convoluted code for using tracing features in your application but having consolidated view of everything going on in your application including custom messages that you add to the next can be enormously useful when you need to find a problem for monitoring application best of all their even more ways to monitor your code
 

Sample text

Sample Text

Total Pageviews