I have a template field that I want to apply a custom format to. I'm starting out by testing with formatting a date. This is what I'm doing.
Code:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim txtDate As TextBox = CType(e.Row.FindControl("txtDate"), TextBox)
Dim lblDate As Label = CType(e.Row.FindControl("lblDate"), Label)
Dim theDate As Date = CDate(DataBinder.Eval(e.Row.DataItem, "TheDate"))
If e.Row.RowIndex = GridView1.EditIndex Then
txtDate.Text = theDate.ToShortDateString()
Else
lblDate.Text = theDate.ToShortDateString()
End If
End If
End Sub
This works for displaying the date. However, on update a null date is passed to my update function.
This will work
Code:
<asp:TemplateField HeaderText="TheDate" SortExpression="TheDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TheDate", "{0:d}") %>'> />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("TheDate", "{0:d}") %>'> />
</ItemTemplate>
</asp:TemplateField>
What is the difference between the two methods? I don't like putting code in my html. How can I get the first approach to work?
Thanks,