没有调用ASP.NET日历OnSelectionChanged处理程序 [英] ASP.NET Calendar OnSelectionChanged handler not getting called

查看:39
本文介绍了没有调用ASP.NET日历OnSelectionChanged处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以告诉我为什么在日历控件中单击某天时为什么未执行名为"MyCalendar_SelectionChanged"的事件处理程序吗?这是来自示例ASP.NET MVC2应用程序的简单aspx代码:

Can some please tell me why the event handler named "MyCalendar_SelectionChanged" is not getting executed when I click on a day within calendar control? This is the simple aspx code from a sample ASP.NET MVC2 application:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<script runat="server">
private void MyCalendar_SelectionChanged (object sender, System.EventArgs e) 
{
    //lbl1.Text = Calendar1.SelectedDate.ToString();
    Console.WriteLine("test");
} 
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  <form id="Form1" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    </p>

    <div>
    <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="MyCalendar_SelectionChanged" />     

    </div>    
</form>    
</asp:Content>

推荐答案

< asp:Calendar runat ="server" .../> 是一个依赖于ViewState和PostBack的服务器控件这是ASP.NET MVC中不再存在的概念,因此您不应该使用任何服务器控件.因为没有回发和视图回调,所以不会触发您的函数.另外,还要确保从表单中删除 runat ="server" 标记,并使用HTML帮助器来生成表单.

<asp:Calendar runat="server" ... /> is a server control that relies on ViewState and PostBack which are notions that no longer exist in ASP.NET MVC so you shouldn't be using any server controls. Because there are no postbacks and view callbacks your function won't be triggered. Also make sure you remove the runat="server" tag from your form and use HTML helpers to generate forms.

要在ASP.NET MVC中实现日历,您可以签出 jQuery UI datepicker .

To implement a calendar in ASP.NET MVC you may checkout the jQuery UI datepicker.

因此,您可以按照以下步骤进行操作:

So here's how you might proceed:

与往常一样,您从MVC中的M(odel)开始,它将代表您愿意显示的信息(在您的情况下为日期):

As always you start with the M(odel) in MVC which will represent the information you are willing to show (in your case a date):

public class MyViewModel
{
    public DateTime Date { get; set; }
}

然后您进入MVC中的C(ontroller):

Then you get to the C(ontroller) in MVC:

public class HomeController: Controller
{
    // used to render the view
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Date = DateTime.Now
        };
        return View(model);
    }

    // will be called when the form is submitted
    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

然后在MVC中添加V(iew):

Then the V(iew) in MVC:

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>" 
%>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm()) { %>
        <%= Html.EditorFor(x => x.Date) %>
        <input type="submit" value="OK" />
    <% } %>
</asp:Content>

然后您可以拥有一个单独的javascript文件,在其中将jquery和jquery UI脚本包括到页面后,将日期选择器附加到该文件中:

and then you could have a separate javascript file in which you would attach the datepicker after having included jquery and jquery UI scripts to your page:

$(function() {
    $('#Date').datepicker();
});

,如果您希望表单在用户选择值时自动提交:

and if you wanted the form to automatically submit when the user picks a value:

$(function() {
    $('#Date').datepicker({
        onSelect: function(dateText, inst) { 
            $('form').trigger('submit');
        }
    });
});

从此处开始使用MVC的好地方: http://asp.net/mvc

A good place to start with MVC is here: http://asp.net/mvc

这篇关于没有调用ASP.NET日历OnSelectionChanged处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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