CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

ASP.NET Web Hosting – 3 Months Free!



Zone: > NEWSGROUP > Asp.Net Forum > windows_hosting.hosting_open_forum Tags:
Item Type: NewsGroup Date Entered: 9/19/2005 11:28:24 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 7 Views: 74 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
8 Items, 1 Pages 1 |< << Go >> >|
Martin Payne
Asp.Net User
IPostBackDataHandler example code not working9/19/2005 11:28:24 PM

0/0

I copied this example code directly out of the help file ...

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Collections
Imports System.Collections.Specialized

Namespace CustomWebFormsControls
   
        <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
        Public Class MyTextBox
        Inherits Control
        Implements IPostBackDataHandler
       
       
        Public Property Text() As String
            Get
                Return CType(Me.ViewState("Text"), String)
            End Get
           
            Set
                Me.ViewState("Text") = value
            End Set
        End Property
       
       
        Public Event TextChanged As EventHandler
       
       
        Public Overridable Shadows Function LoadPostData( _
         postDataKey As String, _
         postCollection As System.Collections.Specialized.NameValueCollection) _
         As Boolean Implements IPostBackDataHandler.LoadPostData
           
            Dim presentValue As String = Text
            Dim postedValue As String = postCollection(postDataKey)
           
            If presentValue Is Nothing Or Not presentValue.Equals(postedValue) Then
                Text = postedValue
                Return True
            End If
           
            Return False
        End Function
       
       
        Public Overridable Shadows Sub RaisePostDataChangedEvent() _
         Implements IPostBackDataHandler.RaisePostDataChangedEvent
       
            OnTextChanged(EventArgs.Empty)
        End Sub
       
       
        Protected Overridable Sub OnTextChanged(e As EventArgs)
            RaiseEvent TextChanged(Me, e)
        End Sub
       
       
        Protected Overrides Sub Render(output As HtmlTextWriter)
            output.Write("<INPUT type= text name = " & Me.UniqueID & _
                " value = " & Me.Text & " >")
        End Sub
       
    End Class
 End Namespace  

I then compiled it and used it in a page as follows...

<%@ Register TagPrefix="myTextBox" Namespace="CustomWebFormsControls" Assembly = "myTextBox" %>
<script runat="server">
 Sub submitBtnClick(sender as Object, e as eventargs)
  lbl.Text = myTB.Text
 End Sub
</script>
<html>
<head>
<title>myRichTextBox</title>
</head>
<body>
<form runat="server">
 
   <myTextBox:MyTextBox id="myTB" runat="server" /><br />
   <asp:button id="btnSubmit" text="Submit" onclick="submitBtnClick" runat="server" /><br />
   <asp:label id="lbl" runat="server" />
 
</form>
</body>
</html>

The textbox and button both render fine, but when I enter some text and click the button I get a server error "System.NullReferenceException: Object reference not set to an instance of an object."!

Have I done something wrong, or is the example wrong?

Martin


"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools."

Douglas Adams
Luis Abreu
Asp.Net User
Re: IPostBackDataHandler example code not working9/20/2005 5:27:43 PM

0/0

Hello.
i'm not a vb.net programmer, so i really don't know how the ctype works. however, i've noticed that it tries to convert the value of ViewState("Text") without checking to see if it has anything in it (ie, it doens't check for nothing before casting it to a string).
--
Regards,
Luis Abreu
email: labreu_at_gmail.com
PT blog: http://weblogs.pontonetpt.com/luisabreu
EN blog:http://msmvps.com/blogs/luisabreu
http://www.pontonetpt.com
http://weblogs.pontonetpt.com/
dannychen
Asp.Net User
Re: IPostBackDataHandler example code not working9/21/2005 10:29:24 PM

0/0

Luis is right.  For a quick test fix, put some text in the box and see if it works.  To fix the code do the following:

object o = ViewState("Text")
if o is Nothing
  return String.Empty
else
  return Ctype(o,String)

--
Danny
disclaimer: Information provided is 'as is' and conveys no warranties or guarantees.
Martin Payne
Asp.Net User
Re: IPostBackDataHandler example code not working9/23/2005 4:52:41 AM

0/0

Okay, now this is confusing.  If I do this -

        Public Property Text() As String
            Get
             Return Me.ViewState("Text")
            End Get
            Set
                Me.ViewState("Text") = value
            End Set
        End Property

When I type something into the textbox and hit the Submit button, I get the NullReference problem.  I get the same problem if I use CType in the Get statement or not.  If I do this -

  Private o As Object        

  Public Property Text() As String
     Get
         o = Me.ViewState("Text")
         If o Is Nothing Then
          return String.Empty
        Else
          return Ctype(o,String)
        End If
     End Get
            
      Set
          Me.ViewState("Text") = value
      End Set
   End Property

It works fine.

The confusing thing is that, I am never hitting the submit button when the textbox is empty.  So, where does this empty string come from and how does checking for Nothing help?

Cheers
Martin

"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools."

Douglas Adams
Luis Abreu
Asp.Net User
Re: IPostBackDataHandler example code not working9/26/2005 2:44:02 PM

0/0

Hello again.

hum, the problem is that you're trying to get the text from the control during a postback. the error occurs during the ctype expression: it only works when there's a value on the viewstate. if you have nothing there, then you'llge tan exception if you try to convert nothing to a string. hope this makes sense.
--
Regards,
Luis Abreu
email: labreu_at_gmail.com
PT blog: http://weblogs.pontonetpt.com/luisabreu
EN blog:http://msmvps.com/blogs/luisabreu
http://www.pontonetpt.com
http://weblogs.pontonetpt.com/
Martin Payne
Asp.Net User
Re: IPostBackDataHandler example code not working9/28/2005 12:24:12 AM

0/0

Hi Luis,

See, that's why I'm so confused.  You're telling me that the problem only occurs if there is nothing in the viewstate for that control.  Yet, the problem occurs when I am populating the textbox but not converting the viewstate to an object.  There should still be a value in there and therefore CType should work but it doesn't?

Cheers
Martin
"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools."

Douglas Adams
Luis Abreu
Asp.Net User
Re: IPostBackDataHandler example code not working10/6/2005 2:22:31 PM

0/0

hello again. sorry for the late reply...

when you write something into the textbox it isn't persisted into the viewstate during that postback. let me explain this better:
1. You open the page on the browser
2. you write something into the textbox
3. you press a button that starts a postback operation...

the text you've entered into the textbox is going to be passed to your loadpostdata method. notice that it is only here that you're going to update the viewstate with this value that you've entered into the client. now if you look at the code, you'll see that you you're trying to get a value from the viewstate before initializing it with the value that comes from the client: it is in this case that you can get a null value and an exception because you're trying to cast null (or nothing, if you prefer vb.net) to a string...this will only happen during the 1st postback of the page since the others that may occur after will already have a value stored in the viewstate (notice that the 1st postback initializaes the viewstate variable you're using to save the text). hope this helps...
--
Regards,
Luis Abreu
email: labreu_at_gmail.com
PT blog: http://weblogs.pontonetpt.com/luisabreu
EN blog:http://msmvps.com/blogs/luisabreu
http://www.pontonetpt.com
http://weblogs.pontonetpt.com/
Martin Payne
Asp.Net User
Re: IPostBackDataHandler example code not working10/12/2005 1:40:49 AM

0/0

The penny drops!  The first line in LoadPostData is

Dim presentValue As String = Text

which does a get on the Text property, which in turn tries to get a value from ViewState that isn't there! Doh!

Thanks for your patience,
Martin
"A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools."

Douglas Adams
8 Items, 1 Pages 1 |< << Go >> >|


Free Download:

Books:
ASP.NET Cookbook Authors: Michael A. Kittel, Michael A. Kittel Geoffrey T. LeBlond, Geoffrey T. LeBlond, Pages: 824, Published: 2004
Pro ASP. Net 3. 5 Server Controls and AJAX Components Authors: Rob Cameron, Dale Michalk, Pages: 740, Published: 2008
Beginning ASP.NET 2.0 AJAX: Written and Tested with the Final 1.0 Release Version of ASP.NET AJAX for ASP:NET 2.0 Authors: Wallace B. McClure, Paul Glavich, Scott Cate, Steve C. Orr, Craig Shoemaker, Steven A. Smith, Jim Zimmerman, Pages: 344, Published: 2007
ASP.NET Unleashed: unleashed Authors: Stephen Walther, Pages: 1488, Published: 2003
Building an ASP.NET Intranet Authors: Kourosh Ardestani, Brian Boyce, Jonathon Walsh, et al., Andy Elmhorst, Matt Gibbs, Pages: 480, Published: 2003
Professional ASP.NET 2.0 Server Control and Component Development Authors: Shahram Khosravi, Pages: 1186, Published: 2006

Web:
IPostBackDataHandler example code not working - ASP.NET Forums Tongue Tied [:S] IPostBackDataHandler example code not working ... Re: IPostBackDataHandler example code not working ...
Re: IPostBackDataHandler not working properly with DataGrid Your code snippet worked like a charm. ... the user control nested in a datagrid . ... Re: IPostBackDataHandler not working properly with DataGrid ...
System.OverflowException during IPostBackDataHandler.LoadPostData method of IPostBackDataHandler) look like? If the code isn't long oner, ..... HtmlEncode not working? Or am I missing something ...
Software/Technology Discussion : IPostBackDataHandler and ... Point #1: Initially both controls were not working. .... the PROPERTIES that are dynamically changed in the program codes but not the data in the control. ...
IPostBackDataHandler Interface (System.Web.UI) The following code example demonstrates a custom text box server control that implements the IPostBackDataHandler interface. The Text property is changed as ...
CheckBox control and IPostBackDataHandler Talk about CheckBox control and IPostBackDataHandler. ... Thanks for your help but that code does not compile neither and says that: ...
Help w/ IPostBackDataHandler - ASP.NET General Reload this Page Help w/ IPostBackDataHandler ... [1] http://www.developersdex. com/gurus/code/584.asp > > Natty Gur, CTO > Dao2Com Ltd. ...
Siderite Zackwehdex's Blog: IPostBackDataHandler, Web controls ... May 23, 2006 ... example, a Label that also adds a hidden input field and handles the ... DataFormatString not working in GridView! Code Geass: Lelouch of ...
IPostBackDataHandler.LoadPostData never called Everything works except that the value entered does is not retained when the. ... #region IPostBackDataHandler *** This code is never called ...
Problem with PostBack handling in a Custom Web Control : postback ... I had once downloaded this code from codeproject which was for building a custom .... Michael, do you have a reference for an IPostBackDataHandler example? ...




Search This Site:










composite controls: validators and the validator summary

cheap web hosting

custom control question, why can i not drag it onto my form?

book opinions

problems bubbling events

properties and methods not recognised

page.registerclientscriptblock problem

need assistance from hp filter experts...

disabling validation in a custom control

object reference not set to an instance of an object

web user control - need to add code

date validation control

control property persistence not working with vs 2005 beta 2

containercontroldesigner for my 2 table cells

any free asp hosts?

dynamic controls

cnfiguring a domain name.

calendar control modification

runtime exception with enumconverter

ftp control development problem

designtimehtml - embedded resource

debugging web custom controls

binding data to a custom control

dropdownlist inharited web custom control

beta 2 web custom sever control

custom server control sample

how to manage: one host, multiple domains on godaddy.com?

create a custom repeater control (with conditional templates)

skittish user control code - runs sometimes

arraylist as datasource for datagrid

  Privacy | Contact Us
All Times Are GMT