简体   繁体   English

如何单击日历并将日期复制到没有回发的文本框,在 asp.net 和 javascript 中?

[英]How do you click a calendar and copy the date to a textbox without postback, in asp.net with javascript?

I have a asp.net page that is used for searching rows of data.我有一个用于搜索数据行的 asp.net 页面。 I want to be able to search a specific date-interval, therefore I have added two calendars (from & to) and a textbox for each one.我希望能够搜索特定的日期间隔,因此我添加了两个日历(从 & 到)和一个文本框。

When a date is selected in the calendar it is displayed in the matching textbox, although this causes a postback, and that is my problem.在日历中选择日期时,它会显示在匹配的文本框中,尽管这会导致回发,这是我的问题。

I have never used javascript before, so I thought I would give that a go, but I almost exclusivly see examples using HTML, which I am not using at all.我以前从未使用过 javascript,所以我想我会给它一个 go,但我几乎只看到了使用 HTML 的示例,我根本没有使用。 I only use ASP.NET web controls such as <asp:Calendar.../> .我只使用 ASP.NET web 控件,例如<asp:Calendar.../>

So this code that follows is just off the top of my head, I know the javascript is totally wrong, but I hope you understand what it is that I want to achieve when you look at it.所以接下来的这段代码就在我的脑海中,我知道 javascript 是完全错误的,但我希望你在看到它时明白我想要实现的目标是什么。

<script type="text/javascript">
    function Calendar1_SelectionChanged() {
        FromDate.Text = Calendar1.SelectedDate.ToString();
    }
</script>    

<asp:Calendar ID="Calendar1" runat="server" 
    onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>

<asp:TextBox ID="FromDate" runat="server"></asp:TextBox>

All answers are greatly appreciated, I have been trying to find an answer to this by googling.非常感谢所有答案,我一直试图通过谷歌搜索找到答案。 but no luck..但没有运气..

EDIT: For this to work on the page, I need to keep the textboxes.编辑:为了在页面上工作,我需要保留文本框。 Or any other way where the date can be edited manually (in DateTime format)或可以手动编辑日期的任何其他方式(以 DateTime 格式)

The problem here is that when the page is actually served to the browser, it's altered from what exactly you have posted there.这里的问题是,当页面实际提供给浏览器时,它与您在此处发布的确切内容不同。 The onselectionchanged event is not a javascript event, it does the postback to call the method Calendar1_SelectionChanged back at the server. onselectionchanged事件不是 javascript 事件,它执行回发以在服务器上调用方法Calendar1_SelectionChanged Your javascript code, although the function appears to have the same name as the onselectionchanged event, is not being run.尽管 function 似乎与onselectionchanged事件同名,但您的 javascript 代码并未运行。

My recommendation is to use a jQuery datepicker .我的建议是使用jQuery datepicker

I'm also not sure your javascript is exactly what it should be to work as you expect it to.我也不确定您的 javascript 是否完全符合您的预期。 It looks like you're using a few C# properties/methods...看起来您正在使用一些 C# 属性/方法...

I would look into the jQuery calendars instead of the asp ones personally.我会亲自查看 jQuery 日历而不是 asp 日历。 This would definitely avoid any postbacks.这肯定会避免任何回发。 http://jqueryui.com/demos/datepicker/ http://jqueryui.com/demos/datepicker/

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

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