I have a web page in ASP + Ext.Net in c# where I create some controls dynamically (text fields, comboboxes, datefields etc) based on an XML. When the user presses a button I have to read the values of those controls and do something with it, so I am using Ext.Net.X.GetCmp to do that. It works just fine for all controls, unless the control is a date control (T=DateField), and unless it has a custom date format (e.g. "dd MMM yyyy"). In that case, it returns a FormatException when I Try to retrieve the control.
A very short example to reproduce the problem:
ASPX:
<%@ Page Language="C#" CodeBehind="Sandbox.aspx.cs" Inherits="MyNamespace.Sandbox" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Multiple DateFields with DateRange Validation - Ext.NET Examples</title>
<link href="/resources/css/examples.css" rel="stylesheet" />
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />
<ext:Window
runat="server"
Width="350"
Title="DateRange"
Icon="Date"
Closable="false"
BodyPadding="5"
Layout="Anchor"
DefaultAnchor="100%">
<Items>
<ext:Button ID="MyTestButton" runat="server" Flex="1" Text="Test" ToolTip="Click here to crash" OnDirectClick="ButtonClicked" />
<ext:FieldContainer
ID="MyFieldContainer"
runat="server"
>
<Items>
<%--HERE I CREATE A DATEFIELD IN CODE BEHIND--%>
</Items>
</ext:FieldContainer>
</Items>
</ext:Window>
</form>
</body>
</html>
Code behind (C#):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
namespace MyNamespace {
public partial class Sandbox : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!this.Page.IsPostBack) {
CreateDateField();
}
}
protected void CreateDateField() {
DateField date = new DateField() {
ID = "MyDateField",
//FieldLabel = "From",
Format = "dd MMM yyyy",
SubmitFormat = "dd/MM/yyyy",
Value = "01 Jun 2014"
};
date.AddTo(MyFieldContainer);
}
[DirectMethod]
public void ButtonClicked(object sender, DirectEventArgs e) {
// Try to retrieve the control.
// Spoiler alert: it will crash
DateField date = Ext.Net.X.GetCmp<DateField>("MyDateField");
// Change the date to see if it worked.
date.Value = "02 Jun 2014";
}
}
}
The error I get is just a System.FormatException: String was not recognized as a valid DateTime. I have tried playing with the SubmitFormat property with no luck. Again, with the default date format it works just fine, but a requirement for the customer is to have their custom date format.
What am I doing wrong? Thank you very much for the help.