How do you find a control in the EditItemTemplate of a GridView?

S

Sean

I am using a GridView to display and edit rows of data. I am having
problems with one of my GridViews that has a Calendar control in it. If
the data coming back from the DB doesn't have a date for this field, I
get a 'Specified Cast is Invalid' message when the empty string is
being bound to the SelectedValue of the Calendar control. That makes
sense.

What I need to do (I think), is somehow intercept the process so that
if I have a DB date that is null, I can tell the Calendar control to
use it's default value of '1/1/0001 12:00:00 AM' for the SelectedValue
property when going into edit mode.

In the RowEditing event I was able to determine that the Label control
(ItemTemplate) for the date column was an empty string. I did this
using FindControl, but I cannot seem to figure out how to find the
Calendar control (EditItemTemplate) so that I can tell it to use the
default date instead of trying to bind the empty string.

Any ideas?
 
S

Sean

For those interested, I finally have a solution. A colleague of mine
helped me out with some ideas. The problem was the <%#
Bind("dateColumn") %> statement on SelectedValue property of the
Calendar control that I had defined in my EditItemTemplate. I needed to
intercept the NULL date value at that point in order to handle things
in the code behind.

Instead of doing two way data binding, I inserted a call to a protected
function in the code behind, where I was doing the bind. ( ex: <%#
GetDateValue( "dateColumn" ) %> ). In that function I would determine
if I had a NULL date and set an internal date variable to
DateTime.MinValue and return it to the Calendar control so it would
display and not give me the Cast invalid error message.

Because I was no longer doing two way data binding I now had to handle
getting any new date value the user might have selected for sending
back in an update operation.

In the GridView RowUpdating event I used FindControl to get a reference
to the Calendar control, so that I could grab the SelectedDate value
and store it back into my internal DateTime variable. Now that I had
the new value stored off, I then was able to use the ObjectDataSource
Updating event to insert into the InputParameters collection the value
stored in my internal DateTime variable.

All is well!
 
Joined
Aug 17, 2006
Messages
1
Reaction score
0
I too am experiencing the same problem.

Could you tell me how you checked for the NULL values in your dataset?

Would you share your code?

Thanks,

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top