I have a gridview and a formview - where I have a couple of date fields and a couple of time fields, respectively defined as "Date" and "Time" in aspx and of type "date" and "time(7)" in MSSQL 2008 r2. In the gridview I only display data but use a FormView to edit as well as Insert new rows. I have formatted these fields both in gridview as well as FromView using
Text='<%#Bind("StartDate","{0:d}") %>'
Text='<%# Bind("StartTIme", "{0:t}") %>'
respectively. I would like to exclude seconds part from the display but that is not a major problem at the moment. The problem is when I click "Edit" link, where first I hide gridview panel and open formview panel and put the form in edit mode. The main problem is: When FormView opens, it does not keep time format of "99:00". It displays only hour and AJAX mask edit extender does not show up its format until the cell is clicked. e.g. if the hour is 8, it displays as 8 and not "08:00" and when the cell is clicked it shows, "80:00"!!! The problem is if someone just changes any other fields on the form, and in the formview does not click into these time fields (because they are not going to change times), it throws error, since the time values read are not in proper format. I will provide code snippets for gridview and formview edit.
gridview item template:
<asp:TemplateFieldHeaderText="Start Time"SortExpression="StartTime">
<ItemTemplate>
<asp:LabelID="lblStartTime"runat="server"Text='<%# Bind("StartTime", "{0:t}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
FormView edit and item templates are essentially identical:
<asp:labelID="lblStartTime"runat="server"Text="Start Time:.................."/>
<asp:TextBoxID="txtStartTime"runat="server"height="20px"Text='<%# Bind("StartTIme", "{0:t}") %>'
ToolTip="Please enter a StartTime in HH:MM format."/>
<asp:MaskedEditExtenderID="MaskedEditExtender1"runat="server"AutoComplete="true"UserTimeFormat="TwentyFourHour"
AcceptAMPM="false"Mask="99:00"MaskType="None"TargetControlID="txtStartTime">
</asp:MaskedEditExtender>
<asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"
ControlToValidate="txtStartTime"Display="Dynamic"
ErrorMessage="StartTime cannot be blank! Please enter a StartTime between hours 6 to 22."/>
<asp:RangeValidatorID="RangeValidator1"runat="server"ControlToValidate="txtStartTime"MinimumValue="05:00"MaximumValue="22:00"
ErrorMessage="StartTime has to be between 06:00 and 22:00!" Display="Dynamic"/>
code behind:
ProtectedSub GridView1_RowCommand(ByVal sender AsObject,ByVal e AsGridViewCommandEventArgs)
lblShow.Text =""
If e.CommandName = "Insert"Then
gvPanel.Visible =False
GridView1.Visible =False
fvPanel.Visible =True
FormView1.Visible =True
FormView1.ChangeMode(FormViewMode.Insert)
GridView1.DataBind()
EndIf
If e.CommandName = "Edit"Then
Dim
iTheIndexNow AsInteger
IfInteger.TryParse(e.CommandArgument.ToString(),
iTheIndexNow) Then
' Set and highlight the selected
GridView1.SelectedIndex = iTheIndexNow
End
If
' Console.WriteLine(iTheIndexNow)
IfNot GridView1.SelectedValue IsNothingThen
FormView1.PageIndex = GridView1.SelectedIndex
FormView1.DataBind()
FormView1.ChangeMode(FormViewMode.Edit)
GridView1.DataBind()
gvPanel.Visible =False
GridView1.Visible =False
fvPanel.Visible =True
FormView1.Visible =True
EndIf
EndIf
EndSub