日历控件未在再次单击相同日期时关闭 [英] Calendar control not closing on clicking same date again

查看:95
本文介绍了日历控件未在再次单击相同日期时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有一个ASP日历以及一个文本框和一个小图像按钮,我想点击图像按钮,然后从日历中选择日期以显示在文本框中。到目前为止,它的工作正常,我可以选择日期和文本框中的显示。但是,我的问题是,如果我已经选择了一个日期并且它显示在文本框中,如果我再次点击相同的日期,日历将不会自行关闭。我必须选择其他日期来关闭它。如果我再次点击所选日期,我如何使其工作,如果我点击网页上的任何其他地方我也想关闭日历。我没有使用Ajax日历控件,因为我们的服务器上没有Ajax。请帮我解决一下。



这是我的源代码:



 <   asp:Panel     ID   =  pnlinsert    runat   = 服务器 >  
< table >
< tr >
< td >
< asp:标签 ID = lblstart runat = server < span class =code-attribute>文本 = 开始日期 > < / asp:标签 > < / td >
< td >
< asp:TextBox ID = txtstartdate runat = 服务器 > < / asp:TextBox > < / td >
< td >
< asp:ImageButton ID = imgbtnstart runat = server

ImageUr l = 〜/ images / calender.png AlternateText = 点击此处显示日历 OnClick = imgbtnstart_Click 高度 = 25px

< span class =code-attribute>宽度 = 16px / >
< td >

< asp:日历 ID = cdrcalendar runat = 服务器 可见 = false OnSelectionChanged < span class =code-keyword> = cdrcalendar_SelectionChanged > < / asp:日历 >

< / td >


< / tr >
< / table >
< / asp:Panel >







这是我的imgbtnstart_Click和cdrcalendar_SelectionChannged的代码。



  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
txtstartdate.Text = DateTime.Today.ToString( MM-dd-yyyy);
}
}
受保护 void cdrcalendar_SelectionChanged( object sender,EventArgs e)
{
txtstartdate.Text = cdrcalendar.SelectedDate.ToString( MM-dd-yyyy);
cdrcalendar.Visible = false ;


}
受保护 void imgbtnstart_Click(< span class =code-keyword> object sender,ImageClickEventArgs e)
{
cdrcalendar.Visible = true ;
}

解决方案

你在那里绑定,因为当你点击相同的日期时,SelectionChanged事件不会火(值不变)。虽然它不会立即得到支持,但你可以做一些事情,比如处理日历或其容器上的点击事件,但是当用户只是试图浏览这些年份时你真的不想关闭控件。或几个月。好消息是,点击相同的日期无论发生什么都会进行回发,所以它会通过您的页面事件,您可以捕获它并在那里处理它。因此,尽管可能有更好的方法,但将此代码添加到Page_Load可实现目标:

  If  Request.Params(  __ EVENTTARGET IsNot   Nothing   AndAlso  Request.Params(  __ EVENTTARGET)。EndsWith(cdrcalendar.ID)然后 
如果会话( CALENDAR_LASTCLICK)= Request.Params( __ EVENTARGUMENT然后
cdrcalendar.Visible = False
否则
会话( CALENDAR_LASTCLICK)= Request.Params( __ EVENTARGUMENT
结束 如果
结束 如果


< blockquote>我通过在Page_Load事件中添加以下行来解决这个问题



cdrcalendar.SelectedDates.Clear();



现在工作正常。谢谢你们!


I have an ASP calendar on my webpage along with a textbox and a small image button, I want to click the image button and select the date from the calendar to display in the textbox.So far its working fine and I can select the date and display in the textbox. But, my problem is that if I have already selected a date and it is displayed in the textbox,if I click on the same date again,the calendar wont close by itself.I have to select some other date to close it. How do i make it work so that it will close if I click on the selected date again and I also want to close the calendar if I click anywhere else on the webpage as well. I am not using the Ajax calendar control since Ajax is not available on our server. Please help me with a solution.

Here is my source code:

<asp:Panel ID="pnlinsert" runat="server">
   <table>
   <tr>
   <td>
       <asp:Label ID="lblstart" runat="server" Text="Start Date"></asp:Label></td>
       <td>
           <asp:TextBox ID="txtstartdate" runat="server"></asp:TextBox></td>
           <td>
         <asp:ImageButton ID="imgbtnstart" runat="server"

                   ImageUrl="~/images/calender.png" AlternateText="Click here to show calendar" OnClick="imgbtnstart_Click" Height="25px"

                   Width="16px"/>
           <td>

               <asp:Calendar ID="cdrcalendar" runat="server" Visible="false" OnSelectionChanged="cdrcalendar_SelectionChanged"></asp:Calendar>

           </td>


   </tr>
   </table>
   </asp:Panel>




and this is my code for imgbtnstart_Click and cdrcalendar_SelectionChannged.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtstartdate.Text = DateTime.Today.ToString("MM-dd-yyyy");
        }
    }
    protected void cdrcalendar_SelectionChanged(object sender, EventArgs e)
    {
      txtstartdate.Text = cdrcalendar.SelectedDate.ToString("MM-dd-yyyy");
      cdrcalendar.Visible = false;


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

解决方案

You're in a bind there because when you click the same date, the SelectionChanged event doesn't fire (the value isn't changing). Though it doesn't immediately appear to be supported, you could do something like handle a click event on the calendar or its container, but then you don't really want to close the control when the user is just trying to navigate through the years or months. The good news is that clicking on the same date does a postback no matter what, so it's going through your page events, and you can catch it and handle it there. So while there may be a better way to do it, adding this code to your Page_Load accomplishes the goal:

If Request.Params("__EVENTTARGET") IsNot Nothing AndAlso Request.Params("__EVENTTARGET").EndsWith(cdrcalendar.ID) Then
    If Session("CALENDAR_LASTCLICK") = Request.Params("__EVENTARGUMENT") Then
        cdrcalendar.Visible = False
    Else
        Session("CALENDAR_LASTCLICK") = Request.Params("__EVENTARGUMENT")
    End If
End If


I solved this by adding the following line in the Page_Load event

cdrcalendar.SelectedDates.Clear();

Its working fine now.Thanks a lot!


这篇关于日历控件未在再次单击相同日期时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆