Debugging the Control

Now that you have created the control, let’s discuss some of the common pitfalls and problems that you can fall into when creating a server-side control.

ViewState

The ViewState dictionary is a name/value pair that enables you to store state information across page requests. It can be thought of as a ScriptingDictionary from VB6. One of the important things to understand about the ViewState dictionary is that it simply is a hash table, just like the ScriptingDictionary. One very important difference, however, is that the ScriptingDictionary was not case-sensitive in terms of the key name used; the ViewState dictionary is.

For example, the following code would not work as you expect it to:

ViewState["MyKey"] == "Hello"; 
Response.Write(ViewState["mykey"]); 

The string "Hello" is stored into a key named MyKey .When referencing this value any subsequent time, you must refer to it as MyKey, not mykey or myKey or any other variation. The key is case-sensitive and must be referred to in the same case each time.

Here’s something else to keep in mind about the ViewState dictionary: It can’t be used across pages via standard links. The ViewState dictionary gets turned into an encoded, hidden form variable on any page. Go ahead and choose View Source from Internet Explorer on any ASP.NET page, and you will see a hidden form field called __VIEWSTATE with a long, encrypted value assigned to it similar to that in Listing 9.7. This is the output from the tab control test page.

Listing 9.7. Sample Output Showing __VIEWSTATE
<!DOCTYPE HTML PUBLIC "-/ /W3C/ /DTD HTML 4.0 Transitional//EN"> 
<HTML> 
  <HEAD> 
    <title>Chapter 9 - Visual Basic>/title> 
  </HEAD> 
  <body> 
    <form name="Form1" method="post" action="page1.aspx" id="Form1"> 
<input type="hidden" name="__VIEWSTATE" 
value="dDwtMTg5MTU0NDI2Njt0PDtsPGk8MT47PjtsPHQ8O2w8aTwxPjs+O2w8dDxwPHA8bD 
xjdXJQYWdlOz47bDwwOz4+Oz47Oz47Pj47Pj47Pg==" /> 

<table width='100%' border><tr><td bgColor='#ff0000'><a id='page1.aspx' href="javascript
:__doPostBack('tab','0')">Page 1</td> 
<td bgColor='#0000ff'><a id='page2.aspx' href="javascript:__doPostBack('tab','1')">Page 2<
/td> 
td bgColor='#0000ff'><a id='page3.aspx' href="javascript:__doPostBack('tab','2')'>Page 3</td> 
</tr></table> 

<input type="hidden" name="__EVENTTARGET" value="" /> 
<input type="hidden" name="__EVENTARGUMENT" value="" /> 
<script language="javascript"> 
<!– 
    function __doPostBack(eventTarget, eventArgument) {
        var theform = document.Form1; 
        theform.__EVENTTARGET.value = eventTarget; 
        theform.__EVENTARGUMENT.value = eventArgument; 
        theform.submit(); 
    } 
// –> 
</script> 
</form> 
    This is PAGE 1. 
  </body> 
</HTML> 

On every form submission, this element is sent to the server, where it is decoded and thrown into the ViewState dictionary. This is how ASP.NET resets default values in text boxes, list boxes, and other elements. In this case, it is how you keep track of our pages, their names, the page you are on, and the page you are moving to.

So, if you are creating some state information using the ViewState dictionary and are expecting to be able to use these values on a new page through a standard <a href></a> link, you will be very disappointed. These will be accessible only through a form submission to the same page. That is why, in this example, the page that you are on is passed via a query string variable because it can’t be tracked the ViewState dictionary. The __VIEWSTATE form variable is not sent to the next page.

Declaring the Control in the Code-Behind File

In the ASP.NET page listed earlier, the tab control is declared in the actual page with the server control tag (< tab:SimpleTabControl . . . />). However, this is not enough to reference the control from your code-behind file. To do this, it is necessary to have a declaration of the tab control in your code-behind file. In C#, this would look like this:

protected Chapter9CS.SimpleTabControl tab; 

In Visual Basic .NET, it would be this:

Protected tab as Chapter9Visual Basic.SimpleTabControl 

The critical thing to remember about the declaration here is that the variable name (tab, in this case) must match the name that you assigned to the control in its name and id parameters on the corresponding ASP.NET page.

If you do not remember to declare your controls in the code-behind file, you will not be able to reference the tab control directly, and your code will either not compile or not run as you had anticipated. Generally, you will see an error message similar to the following when trying to build your application:

The type or namespace name 'tab' could not be found ((are you missing a using 
directive or an assembly reference?) 

If you see a similar error in your future projects, be sure that you have added a declaration of your control in your code-behind file.

Registration of Control on ASP.NET Page

At the top of any ASP.NET page that uses a server control, you must use the Register directive to register that specific control for use in that page. This looks like the following line:

<%@ Register TagPrefix="Tab" Namespace=="Chapter9CS" Assembly=="Chapter9CS" %> 

In this line, you need to specify the TagPrefix, which is the name that will be used to reference the control. In the example, the name Tab is used, so when adding the control to the page, you would write this:

<tab:SimpleTabControl . . . /> 

The other things to specify are the namespace that contains the control, as well as the assembly where the control is located. If these are incorrect, your page will not be displayed. Instead, you will see an error message because the ASP .NET parser will not be capable of finding the control to include in the page. If you forget either of these attributes, the parser will tell you which one is missing. Just add it in, as appropriate, and your control should work fine on that specific page.

runat=server

As with all server-side controls, it is absolutely essential to include the runat=server attribute on the control for it to work properly.We cannot stress enough how important this is.We stress this so much simply because the error message(s) that you will see state nothing about this being the problem.What you will most likely see is a message regarding a reference to the control, or a property or method on the control being null. The message will usually be the following:

Value null was found where an instance of an object was required. 

If you see an error resembling this, the first thing to check is to be sure that you’ve placed the runat=server attribute on the server-side control.

Debugging in Visual Studio .NET

If you are using Visual Studio .NET, consider yourself lucky. You will have unprecedented power in debugging your server-side controls in this IDE.You can set breakpoints on any line in the server-side code or any references to it in your code-behind file. Then, when execution reaches any of these points, your code will break and you can inspect exactly what is happening and see if it is what you expect.

As discussed in Chapter 7,“Visual Studio .NET Debugging Environment,” the Visual Studio .NET IDE provides a lot of flexibility when debugging your code. Figure 9.1 shows a debugging session from the C# project, inspecting the contents of the curPage entry in the ViewState dictionary in the Command window.You can also see the Autos window in the lower-left pane, with a few of the variables that the debugger has automatically decided to add for inspection.

Figure 9.1. Debugging an ASP.NET server control inVisual Studio .NET.


Here in the IDE, you can also trace the order of events to make sure that they are being called in the order that you think they are being called, or to see if they are even being called at all. For example, if you forgot to use the overrides keyword when overriding the Render function on your server control, the function would never be called because it isn’t the “proper” Render function.Within the IDE, you can set a breakpoint on the Render function and see if execution breaks at this spot that it is supposed to.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset