使用ASP.NET日历控件时如何隐藏周末?
有时,在显示日历时,有必要防止在日期标题中显示周末和周末名称,有没有办法使用?据我所知,您不能,但您可以尝试使用WeekendDayStyle,例如通过将样式设置为display:none。或者,您可以创建从Calendar继承的自定义控件,并覆盖ether Render、OnDayRender或其他内容。我相信您可以处理Day Render事件并隐藏单元格或指定CSS属性以使其不可见或变灰。下面是一个简单的例子,我希望这能有所帮助使用ASP.NET日历控件时如何隐藏周末?,asp.net,calendar,Asp.net,Calendar,有时,在显示日历时,有必要防止在日期标题中显示周末和周末名称,有没有办法使用?据我所知,您不能,但您可以尝试使用WeekendDayStyle,例如通过将样式设置为display:none。或者,您可以创建从Calendar继承的自定义控件,并覆盖ether Render、OnDayRender或其他内容。我相信您可以处理Day Render事件并隐藏单元格或指定CSS属性以使其不可见或变灰。下面是一个简单的例子,我希望这能有所帮助 protected void Calendar_DayRend
protected void Calendar_DayRender(object sender, DayRenderEventArgs e)
{
e.Cell.Visible = False;
// or
// e.Cell.Attributes.Add("class", "Invisible");
// or
// e.Cell.Attributes.Add("style", "display: none");
}
由于提供了控件,因此在不重写控件的情况下无法执行此操作。一种方法是重写和方法,以便在将信息发送回客户端之前从输出中删除信息 以下是控件渲染时的屏幕截图: 下面是一个基本控件替代,演示如何从控件中删除周末列
/*------------------------------------------------------------------------------
* Author - Rob (http://stackoverflow.com/users/1185/rob)
* -----------------------------------------------------------------------------
* Notes
* - This might not be the best way of doing things, so you should test it
* before using it in production code.
* - This control was inspired by Mike Ellison's article on The Code Project
* found here: http://www.codeproject.com/aspnet/MellDataCalendar.asp
* ---------------------------------------------------------------------------*/
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Xml;
namespace DataControls
{
/// <summary>
/// Example of a ASP.NET Calendar control that has been overriden to force
/// the weekend columns to be hidden on demand.
/// </summary>
public class DataCalendar : Calendar
{
private bool _hideWeekend;
private int _saturday;
private int _sunday;
/// <summary>Constructor</summary>
public DataCalendar()
: base()
{
// Default to showing the weekend
this._hideWeekend = false;
// Set the default values for Saturday and Sunday
this.Saturday = 6;
this.Sunday = 0;
}
/// <summary>
/// Indicate if the weekend days should be shown or not, set to true
/// if the weekend should be hidden, false otherwise. This field
/// defaults to false.
/// </summary>
public bool HideWeekend
{
get { return this._hideWeekend; }
set { this._hideWeekend = value; }
}
/// <summary>
/// Override the default index for Saturdays.
/// </summary>
/// <remarks>This option is provided for internationalization options.</remarks>
public int Saturday
{
get { return this._saturday; }
set { this._saturday = value; }
}
/// <summary>
/// Override the default index for Sundays.
/// </summary>
/// <remarks>This option is provided for internationalization options.</remarks>
public int Sunday
{
get { return this._sunday; }
set { this._sunday = value; }
}
/// <summary>
/// Render the day on the calendar with the information provided.
/// </summary>
/// <param name="cell">The cell in the table.</param>
/// <param name="day">The calendar day information</param>
protected override void OnDayRender(TableCell cell, CalendarDay day)
{
// If this is a weekend day and they should be hidden, remove
// them from the output
if (day.IsWeekend && this._hideWeekend)
{
day = null;
cell.Visible = false;
cell.Text = string.Empty;
}
// Call the base render method too
base.OnDayRender(cell, day);
}
/// <summary>
/// Render the calendar to the HTML stream provided.
/// </summary>
/// <param name="html">The output control stream to write to.</param>
protected override void Render(HtmlTextWriter html)
{
// Setup a new HtmlTextWriter that the base class will use to render
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter calendar = new HtmlTextWriter(sw);
// Call the base Calendar's Render method allowing OnDayRender()
// to be executed.
base.Render(calendar);
// Check to see if we need to remove the weekends from the header,
// if we do, then remove the fields and use the new verison for
// the output. Otherwise, just use what was previously generated.
if (this._hideWeekend && this.ShowDayHeader)
{
// Load the XHTML to a XML document for processing
XmlDocument xml = new XmlDocument();
xml.Load(new StringReader(sw.ToString()));
// The Calendar control renders as a table, so navigate to the
// second TR which has the day headers.
XmlElement root = xml.DocumentElement;
XmlNode oldNode = root.SelectNodes("/table/tr")[1];
XmlNode sundayNode = oldNode.ChildNodes[this.Sunday];
XmlNode saturdayNode = oldNode.ChildNodes[this.Saturday];
XmlNode newNode = oldNode;
newNode.RemoveChild(sundayNode);
newNode.RemoveChild(saturdayNode);
root.ReplaceChild(oldNode, newNode);
// Replace the buffer
html.WriteLine(root.OuterXml);
}
else
{
html.WriteLine(sw.ToString());
}
}
}
}
/*------------------------------------------------------------------------------
*作者-罗布(http://stackoverflow.com/users/1185/rob)
* -----------------------------------------------------------------------------
*注释
*-这可能不是做事情的最佳方式,因此您应该测试它
*在生产代码中使用它之前。
*-此控件的灵感来自Mike Ellison关于代码项目的文章
*可在此处找到:http://www.codeproject.com/aspnet/MellDataCalendar.asp
* ---------------------------------------------------------------------------*/
使用制度;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用系统文本;
使用System.IO;
使用System.Xml;
命名空间数据控件
{
///
///已重写为强制的ASP.NET日历控件的示例
///周末专栏将根据需要隐藏。
///
公共类DataCalendar:日历
{
私人布卢·希德温德;
私人国际大学周六;
私人国际星期日;
///建造师
公共数据日历()
:base()
{
//默认显示周末
这个。_hideWeekend=false;
//设置周六和周日的默认值
这个星期六=6;
本周日=0;
}
///
///指示是否应显示周末,设置为true
///如果周末应该隐藏,否则为false。此字段
///默认为false。
///
公共图书馆
{
获取{返回此。_hideWeekend;}
设置{this.\u hideWeekend=value;}
}
///
///覆盖星期六的默认索引。
///
///此选项是为国际化选项提供的。
公共int周六
{
得到{归还这个。_星期六;}
设置{this.\u saturday=value;}
}
///
///覆盖星期日的默认索引。
///
///此选项是为国际化选项提供的。
公共int星期日
{
获取{返回此。_sunday;}
设置{this.\u sunday=value;}
}
///
///使用提供的信息在日历上呈现一天。
///
///桌子上的单元格。
///日历日信息
受保护的覆盖无效OnDayRender(TableCell单元格,CalendarDay)
{
//如果这是一个周末,他们应该隐藏,删除
//从输出中删除它们
如果(day.IsWeekend和this.\u hideWeekend)
{
天=空;
单元格可见=假;
cell.Text=string.Empty;
}
//也调用基本渲染方法
基。OnDayRender(单元,天);
}
///
///将日历呈现为提供的HTML流。
///
///要写入的输出控制流。
受保护的覆盖无效呈现(HtmlTextWriter html)
{
//设置基类将用于渲染的新HtmlTextWriter
StringBuilder sb=新的StringBuilder();
StringWriter sw=新的StringWriter(sb);
HtmlTextWriter日历=新的HtmlTextWriter(软件);
//调用基本日历的Render方法,允许OnDayRender()
//待执行。
base.Render(日历);
//检查是否需要从标题中删除周末,
//如果我们这样做了,那么删除字段并使用新的verison
//否则,只需使用先前生成的内容。
if(this.\u hideWeekend&&this.ShowDayHeader)
{
//将XHTML加载到XML文档以进行处理
XmlDocument xml=新的XmlDocument();
Load(新的StringReader(sw.ToString());
//日历控件呈现为表,因此导航到
//第二个TR具有日标题。
XmlElement root=xml.DocumentElement;
XmlNode oldNode=root.SelectNodes(“/table/tr”)[1];
XmlNode sundayNode=oldNode.ChildNodes[this.Sunday];
XmlNode saturdayNode=oldNode.ChildNodes[this.Saturday];
XmlNode newNode=oldNode;
newNode.RemoveChild(sundayNode);
RemoveChild(saturdayNode);
root.ReplaceChild(oldNode、newNode);
//更换缓冲器
WriteLine(root.OuterXml);
}
其他的
{
WriteLine(sw.ToString());
}
}
}
}
如果您可以使用jQuery解决方案,只需几行代码:
<script type="text/javascript">
$(document).ready(function () {
$('._title').parent().attr('colspan', '5'); // title row initially has a colspan of seven
$('._dayheader:first, ._dayheader:last', $('#<%= Calendar1.ClientID %>')).hide(); // remove first and last cells from day header row
$('._weekendday').hide(); // remove all the cells marked weekends
});
</script>
<asp:Calendar runat="server" ID="Calendar1">
<TitleStyle CssClass="_title" />
<DayHeaderStyle CssClass="_dayheader" />
<WeekendDayStyle CssClass="_weekendday" />
</asp:Calendar>
<script type="text/javascript">
HideWeekEnd();
function HideWeekEnd ()
{
$('._title').parent().attr('colspan', '7');
$('._dayheader:nth-last-child(1) , ._dayheader:nth-last-child(2) ', $('#<%= Calendar1.ClientID %>')).hide(); // remove last two cells from day header row
$('._weekendday').hide(); // remove all the cells marked weekends
}
Sys.Application.add_init(appl_init);
function appl_init() {
var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();
pgRegMgr.add_endRequest(HideWeekEnd);
}
</script>
<style>
.hidden,
#Calendrier tr > th[abbr=Saturday],
#Calendrier tr > th[abbr=Sunday] { display:none; }
#Calendrier tr > th { text-align: center; }
</style>
<asp:Calendar ID="Calendar1" DayNameFormat="Full" runat="server"
WeekendDayStyle-CssClass="hidden" ClientIDMode="Static" >
</asp:Calendar>