开发者

MaskedEditExtender date format issue with calender control

I need to create MaskedEditExtender for Sweden date which uses format "yyyy-MM-dd".

I have the following code below. CalendarExtender doesn't work with current MaskedEditExtender. Also the validation doesn't work properly.

<asp:TextBox ID="txtFSFV"
        MaxLength="100"
        style="width:70px"
        runat="server" />
<asp:HyperLink ID="hplGetCalendar" 
        NavigateUrl="javascript:void(null)" 
        runat="server">
        <img src="~/images/calendar.png" runat="server" />
</asp:HyperLink>
<ajax:CalendarExtender ID="calFSFV"
        Format="yyyy-MM-dd"
        Animated="false"
        PopupButtonID="hplGetCalendar"
        TargetControlID="txtFSFV"
        runat="server" />
<ajax:MaskedEditExtender
        ID="maskedFSFV"
        TargetControlID="txtFSFV" 
        Mask="9999-99-99"
        MessageValidatorTip="true" 
        OnFocusCssClass="MaskedEditFocus" 
        OnInvalidCssClass="MaskedEditError"
        MaskType="Date"
        Century="2000"
        CultureName="sv-SE"
        UserDateFormat="YearMonthDay"
        InputDirection="LeftToRight"
        runat="server"/>
<ajax:MaskedEditValidator ID="MaskedEditValidator1" 
        runat="server" 
        ControlExtender="maskedFSFV" 
        ControlToValidate="txtFSFV" 
        InvalidValueMessage="Date is开发者_StackOverflow invalid"
        IsValidEmpty="True" />

Could anybody tell me how can I create a mask ("yyyy-MM-dd") for sv-SE culture?


Got mine to work using yyyy/MM/dd on the calendar format property, removing the culture from the masked extender and on page load i set the culture using

system.threading.thread.currentthread.currentculture =    
system.globalization.cultureinfo.invariantculture

Hope it helps.


It works when I set mask in code behind (txtFSFV.Mask = "9999/99/99";). So issue seems to be connected to Date separator always ("/") and the CultureInfo "sv-SE" sets in properly to "yyyy-MM-dd"


I battled for hours trying to change the date format on my grid view, I ended up doing the following, create new data set cloning it from the exiting one(which has data already) then formatting my date fields on the newly created dataset. Also remember to set the correct culture using you Global.asax file(see code). Hope this helps

  DataSet ds = new DataSet();
        try
        {
            ds = new DataSet();
            if (filterRateDiary.LoadAll())
            {
                DataView dv = filterRateDiary.DefaultView;
                DataTable dt = dv.Table;
                ds.Tables.Add(dt);

                DataSet ds2 = ds.Clone();
                ds2.Tables[0].Columns["ExpiryDate"].DataType = Type.GetType("System.DateTime");
                ds2.Tables[0].Columns["EffectiveDate"].DataType = Type.GetType("System.DateTime");
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    ds2.Tables[0].ImportRow(row);
                }
                return ds2;
            }

Then in your global.asax file add the following code

   protected void Application_BeginRequest(object sender, EventArgs e)
    {
        CultureInfo newCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
        newCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
        newCulture.DateTimeFormat.LongDatePattern = "yyyy-MM-dd HH:mm:ss.fff";
        newCulture.DateTimeFormat.DateSeparator = "-";
        Thread.CurrentThread.CurrentCulture = newCulture;
    }

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜