开发者

Get selected date of ajax calendar extender control

开发者 https://www.devze.com 2023-04-02 10:12 出处:网络
I have used Ajax Calendar extender control (from here) in my asp.net 3.5 application. My Question: How can i get the selected date from the Ajax calendar extender control in code behind file?

I have used Ajax Calendar extender control (from here) in my asp.net 3.5 application.

My Question: How can i get the selected date from the Ajax calendar extender control in code behind file?

Say for example i am selecting 01/01/2011 from calendar, then i need this date in code behind, as i need to check for null values.

let me know for any query.

Please guide. Thanks!

Question updated with code

 &nbsp; <asp:Labe开发者_StackOverflow中文版l ID="lblStartDate" runat="server" Text="<%$ Resources:Resource, lblStartDate %>" CssClass="boldlabelText"></asp:Label>
                                        &nbsp;<asp:TextBox ID="txtStartDate" runat="server" ReadOnly="true" MaxLength="10"></asp:TextBox>
                                        <asp:ImageButton runat="Server" ID="imgStartDate" ImageUrl="~/Images/Calender.png" AlternateText="Click to show calendar" />
                                        <ajax:CalendarExtender ID="CalStartDate" runat="server" TargetControlID="txtStartDate" Format="yyyy-MM-dd" PopupButtonID="imgStartDate">
                                        </ajax:CalendarExtender>

Code-Behind (.cs)

if (txtStartDate.Text.Equals(string.Empty))  // The text value always comes null
        {
            lblStartDateM.Visible = true; 
            txtStartDate.BackColor = Color.FromArgb(255, 255, 235);
            blnIsValid = false;
        }


Dont set the property ReadOnly="true" on your TextBox.

From Joteke's Blog

If TextBox's ReadOnly property is "true", postback data won't be loaded e.g it essentially means TextBox being readonly from server-side standpoint (client-side changes will be ignored). If you want TB to be readonly in the "old manner" use

TextBox1.Attributes.Add("readonly","readonly") 

as that won't affect server-side functionality.


You can also access the ReadOnly text box content via Request.Form collections:

Request.Form[txtStartDate.UniqueID]

has the same effect as

txtStartDate.text

Ref.: http://www.aspsnippets.com/Articles/ASP.Net-AJAX-CalendarExtender---Get-selected-date-from-ReadOnly-TextBox.aspx


Try this code. I have used this piece of code in my website and it is working fine. On button click event, I am able to get the date value entered in the textbox using .text property of textbox.

<asp:TextBox ID="txtDateFrom" runat="server" Width="70px"></asp:TextBox>
<ajax:CalendarExtender ID="CalendarExtender1" runat="server" CssClass="MyCalendar" Format="MM/dd/yyyy" TargetControlID="txtDateFrom" Enabled="True"></ajax:CalendarExtender>

<ajax:MaskedEditExtender ID="MaskedEditExtender1" runat="server" AcceptNegative="Left" DisplayMoney="Left" ErrorTooltipEnabled="True" InputDirection="RightToLeft" Mask="99/99/9999" MaskType="Date" TargetControlID="txtTranDateFrom" CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True"></ajax:MaskedEditExtender>


Use the text property from your textbox that is "extended" by your calenderextender


Unfortunately setting the ReadOnly attribute in a textbox control on the Page_Load() event is useless if the textbox is not detected by intellisense or is embedded inside a GridView for example.

So to avoid all of that headache, I simply turned the TextBox control into a "psuedo" label control and still able to use AjaxControlToolKit CalendarExtender on the "label"

Put onKeyPress = "javascript: return false;" onPaste = "javascript: return false;" in your textbox. That way, even the textbox is enabled, the user will not be able to modify the data, and you're forcing them to use the Calendar control, which is really what you want. We should not be giving users the ability to type in dates.

0

精彩评论

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