ASP.NET Conundrums

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Appendix B.  Common ASP.NET Mistakes


ASP.NET has many peculiarities that can easily cause development woes. Each of the following sections lists the error message typically encountered with a problem, a detailed description, and a possible solution(s). (Note that italicized words in the error messages can be substituted for specific names.)

Problems with Web Forms

Problem: Unexpected end of file looking for </asp:Control> tag

Description: You've forgotten to add the closing tag for an ASP.NET server control.

Solution: All server controls must have corresponding closing tags. Add a closing tag using one of the following sample syntaxes:

 <asp:Label  runat="server" /> <asp:Label  runat="server">  </asp:Label> 

Problem: Literal content ("html") is not allowed within a 'control'

Description: You've tried to use a server control without a closing tag.

Solution: This problem typically occurs with DataList or DataGrid controls because you forgot to include the closing server tag. Without this tag, ASP.NET considers all following HTML code as part of the server control and throws an error. Simply add the closing tag for this control.

Problem: Server control does not display or act as it should.

Description: ASP.NET is not properly interacting with the server control; form data isn't being recognized, display properties are not being displayed, and so on.

Solution: The most likely reason for this problem is the lack of the runat="server" attribute on the server control. Add this attribute to the server control in question.

Problem: Data is not being displayed in a server control as it should.

Description: ASP.NET does not seem to display the data at all or it isn't updating as it should.

Solution: You've forgotten to call the DataBind method.

Problem: An exception has been thrown by the class constructor for System.Drawing.Internal.SystemColorTracker

Description: ASP.NET isn't able to draw the specified control properly (typically a DataGrid).

Solution: Restart the application. This can be done in any of the following ways:

  • Call iisreset from the command prompt.

  • Modify and save either the global.asax or the web.config file.

  • Recompile a business object in the application's \bin directory.

Problem: Your event handling routines are not producing the expected results.

Description: Using Web forms and server controls, you are trying to produce a desired output, but it isn't working as expected. For example, you are trying to modify the value contained in a text box or label, but the value isn't changing or isn't changing to what you intended.

Solution: Recall that the Page_Load event always executes before any event handlers, and that the event handlers execute in no particular order. Your Page_Load event might be modifying or resetting the target value before your event handler has an opportunity to use it. For example, the following code snippet demonstrates this situation:

 sub Page_Load(Sender as Object, e as EventArgs)    tbMessage.Text = "Hello world!" end sub sub HandleSubmit(Sender as Object, e as EventArgs)    Response.Write(tbMessage.Text) end sub ... ... <asp:TextBox  runat="server"    OnTextChanged="HandleSubmit"    AutoPostBack=true /> ... 

When the text in the text box changes, the form is posted. However, the Page_Load method executes first and resets the value in the text box to "Hello World!" no matter what else occurs. Then your event handler displays "Hello World!" rather than the new text.

One solution is to check the Page.IsPostBack property in the Page_Load event to determine whether or not the form was just submitted, and therefore the value should not be reset:

 sub Page_Load(Sender as Object, e as EventArgs)    if not Page.IsPostBack then       tbMessage.Text = "Hello World!"    end if end sub ... 

Another solution is simply to restructure your routines.

Other Problems

Problem: MissingMethodException: Member not found.

Description: You've tried to reference a method or property of an object, typically when it is passed from a method.

Solution: Although there are many potential causes to this problem, a very common one has to do with data type casting. For example, look at the following event handler:

 sub MyHandler(Sender as Object, e as EventArgs)    Response.Write(obj.Text) end sub 

Assuming that an event from a label causes this method to execute, the variable Sender should represent that label, and you should be able to access its Text property. However, you should never rely on what you expect a variable to represent; never assume that an object is what it should be. Instead, you should cast the object accordingly:

 Response.Write(CType(Sender, Label).Text) 

Often, variables of type Object in ASP.NET are early-bound, meaning they are evaluated when the page is compiled (versus when the page is requested). Because of this, ASP.NET balks at your call to the Text property an Object does not have a Text property. When the page is requested, however, the appropriate reference will be set and you will be able to call Text, but by then, it's too late. Always cast your variables accordingly.

Problem: The type object in Assembly name, Version=version, Culture=culture, PublicKeyToken=token is not marked as serializable.

Description: You've tried to persist an object in session or application state.

Solution: Some objects, such as DataSets, are not able to be persisted automatically in a state variable. Try using the ShouldSerializeObject method of the object in question (see Appendixes C and D).

Problem: Type not defined: type

Description: You try to declare an instance of a known object.

Solution: Make sure that you've spelled the type correctly. Also, be sure that the necessary namespaces are imported. Remember that VB.NET classes (such as code-behind forms and business objects) do not automatically import namespaces as do ASP.NET pages.


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net