.net控件
Asp.net 自带的Ajax Extensions中得ScriptManage和 UpdatePanel可以一起实现局部刷新,提高速度和节省网络流量
前台代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="change"
AutoPostBack="True"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="1000"
ontick="Timer1_Tick">
</asp:Timer>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
后台代码:
namespace AjaxTest
{
public partial class ScriptManageAjax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript ", "alert( '对不起,账号和密码错误 ') ", true);
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = string.Format("当前选择的值是:{0},时间是:{1}", this.DropDownList1.SelectedItem.Text, DateTime.Now.ToString());
this.Label1.Text = str;
}
protected void Button2_Click(object sender, EventArgs e)
{
string str = string.Format("当前选择的值是:{0},时间是:{1}", this.DropDownList1.SelectedItem.Text, DateTime.Now.ToString());
this.Label1.Text = str;
Response.Write("xx");
}
}
}
说明:讲需要实现局部刷新的内容,写在UpdatePanel1标签中。
注意:
使用ASP.NET AJAX 开发程序时候 我们以往经常使用 response.write();不能使用。
使用ScriptManager来实现JS注册即可。
代码示例:ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript ", "alert( '对不起,账号和密码错误 ') ", true);
此功能的做法
首先介绍功能:1.可以点击按钮进入编辑页面进行编辑功能,进行设置 2.页面中能够现实某日的工作内容
前台aspx页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<div style="WIDTH: 100%" align="center">
<asp:Calendar ID="calSchedule" runat="server" Font-Size="X-Small" OnDayRender="calSchedule_DayRender" BorderStyle="none" ShowGridLines="True" PrevMonthText="<img src=Images/left_arrow.gif/>上一月" NextMonthText="下一月<img src=Images/right_arrow.gif/>" DayNameFormat="Full" BorderColor="#999999" BackColor="transparent" Height="92%" Width="100%" OnSelectionChanged="calSchedule_SelectionChanged">
<TodayDayStyle BackColor="Transparent" BorderStyle="Solid" BorderWidth="2px" BorderColor="CornflowerBlue"></TodayDayStyle>
<DayStyle HorizontalAlign="Left" Font-Bold="True" VerticalAlign="Top"></DayStyle>
<WeekendDayStyle BackColor="White" ForeColor="Red" BorderColor="White"></WeekendDayStyle>
<NextPrevStyle ForeColor="#223999" CssClass="td"></NextPrevStyle>
<DayHeaderStyle BackColor="#BFE6F9" Height="10px" Font-Size="Smaller"></DayHeaderStyle>
<TitleStyle BackColor="#66CCFF" Height="10px" CssClass="headcenter"></TitleStyle>
</asp:Calendar>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
cs页面
using MyOffice.BLL;
using MyOffice.Models;
using MyOffice.Utils;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void calSchedule_DayRender(object sender, DayRenderEventArgs e)
{
MyOffice.Models.User user = (MyOffice.Models.User)Session["Login"];
//自定义显示内容
CalendarDay calDay = e.Day;
//获取表示呈现在空间中的单元格
TableCell tc = e.Cell;
CNDate dt = ChinaDate.getChinaDate(calDay.Date);//农历转换对象
if (calDay.IsOtherMonth)
{
tc.Controls.Clear();
}
else
{
try
{
HyperLink ahyperLink = new HyperLink();
ahyperLink.ImageUrl = "~/Images/add_Schedule.gif";
ahyperLink.ToolTip = "新增个人日程";
ahyperLink.NavigateUrl = "aaa.aspx?userId=" + user.UserId + "&today=" + calDay.Date.ToShortDateString();//获取到人员等等
tc.Controls.Add(new LiteralControl(" " + " " + " "));
tc.Controls.Add(ahyperLink);
tc.Controls.Add(new LiteralControl("<br>" + dt.cnStrMonth + "月" + dt.cnStrDay));//添加农历日期
tc.Controls.Add(new LiteralControl("<br>" + dt.cnSolarTerm));//添加农历节气
tc.Controls.Add(new LiteralControl("<br>" + dt.cnFtvs));//添加節日
IList<Schedule> schedules = ScheduleManager.SearchSchedule(calDay.Date.ToShortDateString(), -1, user.UserName, false);
if (schedules != null)
{
string str = null;
foreach (Schedule schedule in schedules)
{
HtmlAnchor ha = new HtmlAnchor();
ha.HRef = "bbb.aspx?userId=" + user.UserId + "&scheduleId=" + schedule.ScheduleId + "&today=" + calDay.Date.ToShortDateString();
try
{
str = schedule.Title.Substring(0, 3);
}
catch
{
str = schedule.Title.ToString();
}
ha.InnerText = "@" + schedule.BeginTime.Hour.ToString() + ":" + schedule.BeginTime.Minute.ToString() + str + "...";
tc.Controls.Add(new LiteralControl(" " + " " + " "));
tc.Controls.Add(new LiteralControl("<br>"));
tc.Controls.Add(ha);
}
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
e.Cell.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#fff7ce';cursor='hand';";
e.Cell.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#ffffff'";
}
}
}