Null Dates in Webforms, How?


Author
Message
Cyrus Welch
Cyrus Welch
StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)
Group: Forum Members
Posts: 20, Visits: 82
How do I properly use null dates in a webforms application?  When working with null dates I get data binding errors on postbacks.  We use nulls to at least have a nominally empty date field but can't seem to get it to work in webforms.  I searched mutiple times and can't find any references to what the best way to do it so that customers won't see a date when entering data if the field is not supposed to have a date in it yet.  I've tried the return alternate using #1/1/1800# and then that shows in the textbox on the screen, I could live with the return alternate concept if it just didn't show in textboxes and listviews.

Is there an easy way to do this or do I need to not bind the data and handle it myself? (which is what I'm trying to avoid).

Replies
Cyrus Welch
Cyrus Welch
StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)
Group: Forum Members
Posts: 20, Visits: 82
Greg McGuffey (01/04/2008)
Ben,

What event should you handle (override) in the inherited textbox to handle this issue?

Yeah, what he said!  I'm willing to do that, but I do need to know which event would be best to do this.  I can't have the users seeing the 1/1/1800 value.  I know it may take a little more work to handle the situation for the .net gridview which we use because we get paging and such.

I guess what your saying is that the value involved both ways must be a valid date or there are problems?  I can live with that I guess as long as I can figure out how to inherit the textbox and change it both ways on the fly.

StrataFrame Team
S
StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
You'll want to create your own property on the TextBox, say "Value" or "MyText" (something other than Text since it's already taken).  Make that property a DateTime property and that property will then be used for your binding (so, bind the business object to that property, not the Text property. 

In the Get of that property, if the Text is empty, then return 1/1/1800, if the text has a date, then use DateTime.Parse() to get the DateTime value from the Text and return it. 

Then, in the Set of the property, if the value is 1/1/1800, set the text to String.Empty.  Otherwise, set the Text to value.ToString("MM/dd/yyyy") (where the MM/dd/yyyy is the format you want the dates to appear in). 

Luckally, WebForms binding is much simpler than WinForms binding, so you won't have to worry about creating all of those changed and changing events to get the data to bind properly.

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (2.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
So, in WinForms, you'd also need to add the changing/changed events to get this to work. Anything else to get to work with a WinForms control?
StrataFrame Team
S
StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)StrataFrame Developer (3.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Ah, for WinForms, when you add a bindable property, you also have to add a "Changed" event that corresponds to the bindable property and it has to have the same signature as EventHandler:

Public Event MyTextChanged As System.EventHandler '-- Your property is named "MyText"

You then raise this event when the property changes so that the binding knows to copy the new data back to the data source.  The "Changing" event isn't needed by the binding, but sometimes it's nice to have; I would only add it when you need it.

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (2.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Got it, thanks!
Cyrus Welch
Cyrus Welch
StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)
Group: Forum Members
Posts: 20, Visits: 82
Ben Chase (01/11/2008)
You'll want to create your own property on the TextBox, say "Value" or "MyText" (something other than Text since it's already taken).  Make that property a DateTime property and that property will then be used for your binding (so, bind the business object to that property, not the Text property. 

In the Get of that property, if the Text is empty, then return 1/1/1800, if the text has a date, then use DateTime.Parse() to get the DateTime value from the Text and return it. 

Then, in the Set of the property, if the value is 1/1/1800, set the text to String.Empty.  Otherwise, set the Text to value.ToString("MM/dd/yyyy") (where the MM/dd/yyyy is the format you want the dates to appear in). 

Luckally, WebForms binding is much simpler than WinForms binding, so you won't have to worry about creating all of those changed and changing events to get the data to bind properly.

Ok, I know this may rather basic, but I can't figure out how to easily subclass the control to do this.  Sometimes I wonder why I'm doing this, as subclassing a control like this was so easy to do in VFP.  Do I actually need to create a composite control in it's own assembly to be able to do this?  I just want to create a user control and be done, but I can't find any references to how to do this and be able to actually use the control like a normal control.

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (2.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
You just create a class that is named whatever you like and inherit that class from the control you are sub classing:



' VB

Public Class MyTextbox

Inherits Microfour.StrataFrame.UI.Windows.Forms.Textbox

' code...

End Class



// C#

class MyTextbox : Microfour.StrataFrame.UI.Windows.Forms.TextBox {

// code...

}




This will then available in the toolbox under the project you put it in (each project that has controls, user controls or components will be a heading in the toolbox).
Trent Taylor
Trent Taylor
StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Sometimes I wonder why I'm doing this, as subclassing a control like this was so easy to do in VFP.

As a former VFP developer...it is actually MUCH easier in .NET once you understand the structure of .NET.  Greg's sample is spot on...just create a class, then inherit whatever base class that you would like.  You can then override base functionality (which is much better then VFP ever was in this area, especially as it relates to events).  For example, if I inherit a TextBox and want to change the logic (or add to) of the TextChanged event I could do this:

Public Class MyTextBox
    Inherits MicroFour.StrataFrame.UI.Windows.Forms.TextBox

    Protected Overrides Sub OnTextChanged(ByVal sender As Object, ByVal e As EventArgs)
       MyBase.OnTextChanged(sender, e)

       '-- Add your logic here (or above the base if that is what your require....You can also skip the base logic altogether
       '    if that is in fact what you are trying to accomplish.
    End Sub
End Class


Cyrus Welch
Cyrus Welch
StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)
Group: Forum Members
Posts: 20, Visits: 82
This is all well and good, but all the examples seem to be for windows forms and don't seem to work for webforms.  All I want to do it subclass the control and be able to place it in my webform in place of the normal control.  I can't seem to do anything with a control created this way.  Am I missing something or is VS2005 broken on both my desktop and laptop?
Trent Taylor
Trent Taylor
StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Cyrus,

This is no different for web than it is for Windows.  The only difference is that you will want to create a class library that your web site or web application references to house all of your custom (or inherited) controls.  Here is how you would subclass a web textbox:

Public Class MyWebTextBox
    Inherits MicroFour.StrataFrame.UI.Web.TextBox
    Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
        MyBase.OnTextChanged(e)
    End Sub
End Class

Cyrus Welch
Cyrus Welch
StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)
Group: Forum Members
Posts: 20, Visits: 82
Trent L. Taylor (01/31/2008)
Cyrus,

This is no different for web than it is for Windows.  The only difference is that you will want to create a class library that your web site or web application references to house all of your custom (or inherited) controls.  Here is how you would subclass a web textbox:

Public Class MyWebTextBox
    Inherits MicroFour.StrataFrame.UI.Web.TextBox
    Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
        MyBase.OnTextChanged(e)
    End Sub
End Class

Ok, I've tried that without any success.  Does it have to be in it's own assembly to work?  When I create the subclass for the textbox I can't find any way to put it on a webform and actually use it.  This is where things are geting stuck.  Once I have created the subclassed control how do I use it, since I'm guessing that I have the subclassed control now, but I can't put on anything and use it.

Trent Taylor
Trent Taylor
StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Cyrus,

Once you create the class, make sure that your web application has a reference to the assembly that houses the class you created.  You can also turn on the AutoPopulateToolbox option for it to show in your toolbox...or just type in the name of the namespace and class when defining the object.

To turn on the AutoPopulation of the toolbox click Tools->Options->Windows Forms Designer->General->AutoToolboxPopulate .

I know that it says "Windows Forms" but this populates the Web controls as well.

Cyrus Welch
Cyrus Welch
StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)StrataFrame Beginner (38 reputation)
Group: Forum Members
Posts: 20, Visits: 82
Trent L. Taylor (02/05/2008)
Cyrus,

Once you create the class, make sure that your web application has a reference to the assembly that houses the class you created.  You can also turn on the AutoPopulateToolbox option for it to show in your toolbox...or just type in the name of the namespace and class when defining the object.

To turn on the AutoPopulation of the toolbox click Tools->Options->Windows Forms Designer->General->AutoToolboxPopulate .

I know that it says "Windows Forms" but this populates the Web controls as well.

Just to clarify what I am going to try here, I do need to create a seperate assembly containing any subclassed controls I want to use and then I can reference them and use the controls?  I think I can work with that.  I have some other questions on whether I can do some things or not but I'll start another thread for that.

Trent Taylor
Trent Taylor
StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)StrataFrame Developer (8.7K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
I do need to create a seperate assembly containing any subclassed controls I want to use and then I can reference them and use the controls?

Yup...this is exactly what you should do.  This is the very same thing that we do for all of our applications as well as our web applications.  So just create a class library, add all of your subclassed controls, then you can use them instead of the standard controls.  This is good programming standards...good job Smile

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Threaded View
Threaded View
Cyrus Welch - 16 Years Ago
Peter Jones - 16 Years Ago
StrataFrame Team - 16 Years Ago
Greg McGuffey - 16 Years Ago
Cyrus Welch - 16 Years Ago
                         You'll want to create your own property on the TextBox, say "Value" or...
StrataFrame Team - 16 Years Ago
                             So, in WinForms, you'd also need to add the changing/changed events to...
Greg McGuffey - 16 Years Ago
                                 Ah, for WinForms, when you add a bindable property, you also have to...
StrataFrame Team - 16 Years Ago
                                     Got it, thanks!
Greg McGuffey - 16 Years Ago
                             [quote][b]Ben Chase (01/11/2008)[/b][hr]You'll want to create your own...
Cyrus Welch - 16 Years Ago
                                 You just create a class that is named whatever you like and inherit...
Greg McGuffey - 16 Years Ago
                                     [quote]Sometimes I wonder why I'm doing this, as subclassing a control...
Trent L. Taylor - 16 Years Ago
                                         This is all well and good, but all the examples seem to be for windows...
Cyrus Welch - 16 Years Ago
                                             Cyrus, This is no different for web than it is for Windows. The only...
Trent L. Taylor - 16 Years Ago
                                                 [quote][b]Trent L. Taylor (01/31/2008)[/b][hr]Cyrus, This is no...
Cyrus Welch - 16 Years Ago
                                                     Cyrus, Once you create the class, make sure that your web application...
Trent L. Taylor - 16 Years Ago
                                                         [quote][b]Trent L. Taylor (02/05/2008)[/b][hr]Cyrus, Once you create...
Cyrus Welch - 16 Years Ago
                                                             [quote]I do need to create a seperate assembly containing any...
Trent L. Taylor - 16 Years Ago

Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search