简体   繁体   English

asp.net c#中的日历控件

[英]Calendar control in asp.net c#

I have a text box and a calendar in my ASP.NET web application.我的 ASP.NET Web 应用程序中有一个文本框和一个日历。

When I select any date in calendar, I would like the date/month/year of that date to be displayed in the text box.当我在日历中选择任何日期时,我希望该日期的日期/月份/年份显示在文本框中。

in .aspx file在 .aspx 文件中

<form id="form1" runat="server">
<div>
    <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged">
    </asp:Calendar>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
</form>

in .aspx.cs file在 .aspx.cs 文件中

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    TextBox1.Text = Calendar1.SelectedDate.ToString();
}

Always use google before you ask question : http://www.google.co.in/search?q=asp.net+%2B+calander+control+%2B+textbox&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a在提问之前总是使用谷歌: http : //www.google.co.in/search? q=asp.net+%2B+calander+control+%2B+textbox&ie=utf-8&oe=utf-8&aq=t&rls =org。 mozilla:en-US:official&client=firefox-a

check the answer below检查下面的答案

private void Calendar1_SelectionChanged(System.Object sender, System.EventArgs e)
{
    TextBox1.Text = Calendar1.SelectedDate;
}

or或者

use OnClientDateSelectionChanged .使用OnClientDateSelectionChanged Similar example explained here well CalendarExtender Change date with Javascript类似的例子在这里解释得很好CalendarExtender Change date with Javascript

or或者

Calendar Demonstration 日历演示

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <asp:Calendar ID="Calendar1" runat="server" 
        onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:UpdatePanel>

Assuming you already use the onselectionchanged event but not seeing result directly you could use a updatepanel like this假设您已经使用了 onselectionchanged 事件但没有直接看到结果,您可以使用这样的更新面板

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <asp:Calendar ID="Calendar1" runat="server" 
        onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:UpdatePanel>

If you were just looking for the event then it would look alike this如果您只是在寻找事件,那么它看起来像这样

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    TextBox1.Text = Calendar.cal.SelectedDate.ToString();
}

Handle the "SelectionChanged" Event of the calender control and inside the event write this code,处理日历控件的“SelectionChanged”事件并在事件内部编写此代码,

txtbox.Text = Calendar1.SelectedDate;

txtbox.Invalidate();

You could also try looking at the Ajax Toolkit CalendarExtender.您还可以尝试查看 Ajax Toolkit CalendarExtender。 This gives you a text box that when you click in it opens a calendar and the selected date is automatically added to the text box.这为您提供了一个文本框,当您单击它时会打开一个日历,并且所选日期会自动添加到文本框中。

http://www.asp.net/ajax/ajaxcontroltoolkit/samples/calendar/calendar.aspx http://www.asp.net/ajax/ajaxcontroltoolkit/samples/calendar/calendar.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM