【问题标题】:How to change visibility of Calendar on textbox click如何在文本框单击上更改日历的可见性
【发布时间】:2016-10-05 06:29:42
【问题描述】:

我想使用TextBoxCalendar 来制作如下所示的日期选择字段,

<asp:TextBox ID="date_selected" Enabled="false" CssClass="date" runat="server" MaxLength="10"></asp:TextBox>
<asp:ImageButton ID="image_calendar" runat="server" Width="32px" ImageAlign="AbsMiddle" Height="32px" ImageUrl="/Images/calendar.png" OnClick="image_calendar_Click"></asp:ImageButton>
<asp:Calendar ID="calendar" Visible="false" runat="server" OnSelectionChanged="calendar_SelectionChanged"></asp:Calendar>

在这里,当我单击ImageButton 时,Calendar 将变为可见,在我单击日期后,TextBox 将使用下面的脚本填充该日期,

protected void image_calendar_Click(object sender, ImageClickEventArgs e)
{
    calendar.Visible = true;
}

protected void calendar_SelectionChanged(object sender, EventArgs e)
{
    date_selected.Text = calendar.SelectedDate.ToShortDateString().ToString();
    calendar.Visible = false;
}

但我想在不使用ImageButton 的情况下执行此操作,并且当我单击TextBox 本身时,应显示Calendar。但是TextBox 没有任何OnClick 功能。有没有其他方法可以在CSS 中使用TextBox:focus 或使用JavaScriptJQuery

CSS 中,我知道我们可以使用属性display: none 和“可见性:隐藏”来隐藏项目的可见性,但是有什么方法可以做到这一点吗?

#date_selected:focus {
    // set visibility of Calendar to "visible"
}

最终代码(编辑)

下面是我使用的最终代码。

ASP.NET

<asp:TextBox ID="date_selected" ReadOnly="true" MaxLength="10" AutoPostBack="true" runat="server"></asp:TextBox>
<asp:Calendar ID="calendar" runat="server" OnSelectionChanged="calendar_SelectionChanged"></asp:Calendar>

C#

protected void calendar_SelectionChanged(object sender, EventArgs e)
{
    date_selected.Text = calendar.SelectedDate.ToShortDateString().ToString();
    calendar.Visible = false;
}

CSS

#calendar {
    visibility: hidden;
    opacity: 0;
    position: absolute;
    z-index : 9999 !important;
}

#date_selected:focus ~ #calendar {
    visibility: visible;
    position: absolute;
    opacity: 1;
}

但是在显示日历之后,即使我点击了任何日期,它也没有在文本框中得到更新?我不知道为什么?不只是在任何日期,如果我单击日历上的任何地方我什至无法更改月份,日历就会消失。也许当我点击日历上的某个地方时,文本框上的焦点消失了,日历也消失了。我该如何解决这个问题?

【问题讨论】:

  • 你在寻找这样的东西吗:$('#date_selected').focus(function() { $('#Calendar ').show(); });
  • 我想是这样..我不太了解 JQuery 我会尝试这个脚本
  • 我应该在标题中添加任何 JQuery 链接或其他内容吗?
  • 是的,您应该在标题中添加&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"&gt;&lt;/script&gt;&lt;script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"&gt;&lt;/script&gt;
  • 它不工作..我可以用其他方法吗?也许在 C# 或其他东西中?

标签: javascript c# jquery css asp.net


【解决方案1】:

要将其置于其他 div 之上,您可以使用 z-index

#date_selected:focus ~ #calendar {
    visibility: visible;
    position: absolute;
    opacity: 1;
    z-index : 9999 !important;
}

另外,通过使用Jquery,你应该把它放在document ready函数中以使其工作

$(document).ready(function () {
     $('#date_selected').focus(function() { $('#Calendar ').show(); });
});

如果要添加日历功能,也可以使用datepicker。它是 Jquery 中一个不错、简单且易于使用的插件。通过使用datepicker,您不必再设置css 和日历。然后你会像这样使用它:

$(document).ready(function () {
     $('#date_selected').datepicker();
});

【讨论】:

  • 谢谢,z-index 正在工作..但是当我点击任何日期时,它没有在文本框中更新,我编辑了我的问题,请检查一下..我确实添加了OnSelctionChanged到日历
  • @ManoUncharted 对您的最佳建议是使用 datepicker 功能。它是一个易于使用的 jquery 插件
  • &lt;script type="text/javascript"&gt; $(document).ready(function () { $('#date_selected').datepicker(); } &lt;/script&gt; 我应该这样添加吗?
  • @ManoUncharted 抱歉,我不确定框架 2.0,但您可以尝试一下。是的,但我只是编辑结束语,忘记添加);
  • 是否可以下载日期选择器文件而不是链接到网络?我可以下载并链接到我系统中的文件,对吗?我在哪里可以下载它们?
【解决方案2】:

您只需使用Ajax Toolkit即可实现上述目标,试试下面的一个

<form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
    <asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
    <cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate" PopupButtonID="txtDate" >
</cc1:CalendarExtender>
    </div>
    </form>

【讨论】:

  • 它说没有找到CalendarExtender标签..有没有其他方法不使用外部工具包或库?
猜你喜欢
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
  • 2015-08-18
相关资源
最近更新 更多