<i id='wX0AU'><tr id='wX0AU'><dt id='wX0AU'><q id='wX0AU'><span id='wX0AU'><b id='wX0AU'><form id='wX0AU'><ins id='wX0AU'></ins><ul id='wX0AU'></ul><sub id='wX0AU'></sub></form><legend id='wX0AU'></legend><bdo id='wX0AU'><pre id='wX0AU'><center id='wX0AU'></center></pre></bdo></b><th id='wX0AU'></th></span></q></dt></tr></i><div id='wX0AU'><tfoot id='wX0AU'></tfoot><dl id='wX0AU'><fieldset id='wX0AU'></fieldset></dl></div>

    <bdo id='wX0AU'></bdo><ul id='wX0AU'></ul>

  • <tfoot id='wX0AU'></tfoot><legend id='wX0AU'><style id='wX0AU'><dir id='wX0AU'><q id='wX0AU'></q></dir></style></legend>

    <small id='wX0AU'></small><noframes id='wX0AU'>

      1. 使用 ASP.NET 日历控件时如何隐藏周末?

        时间:2023-08-26

        <tfoot id='p7hW1'></tfoot>

        <small id='p7hW1'></small><noframes id='p7hW1'>

          <bdo id='p7hW1'></bdo><ul id='p7hW1'></ul>

              • <legend id='p7hW1'><style id='p7hW1'><dir id='p7hW1'><q id='p7hW1'></q></dir></style></legend>
                  <tbody id='p7hW1'></tbody>

                • <i id='p7hW1'><tr id='p7hW1'><dt id='p7hW1'><q id='p7hW1'><span id='p7hW1'><b id='p7hW1'><form id='p7hW1'><ins id='p7hW1'></ins><ul id='p7hW1'></ul><sub id='p7hW1'></sub></form><legend id='p7hW1'></legend><bdo id='p7hW1'><pre id='p7hW1'><center id='p7hW1'></center></pre></bdo></b><th id='p7hW1'></th></span></q></dt></tr></i><div id='p7hW1'><tfoot id='p7hW1'></tfoot><dl id='p7hW1'><fieldset id='p7hW1'></fieldset></dl></div>
                  本文介绍了使用 ASP.NET 日历控件时如何隐藏周末?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有时在显示日历时需要防止显示周末和周末名称在日期标题中,有没有办法使用 ASP.NET 日历控件?

                  Sometimes when displaying a calendar it is necessary to prevent the weekend days and the weekend names in the day header from showing, is there a way to do this using the ASP.NET Calendar control?

                  推荐答案

                  由于提供了控件,如果不重写控件,就没有办法做到这一点.这样做的一种方法是覆盖 OnDayRender 和 Render 方法在将信息发送回客户端之前从输出中删除信息.

                  As the control is provided, there is no way to do this without overriding the control. One way of doing this is to is to override the OnDayRender and Render methods to remove the information from the output prior to sending it back to the client.

                  以下是渲染时控件外观的屏幕截图:

                  The following is a screen shot of what the control looks like when rendered:

                  以下是一个基本的控件覆盖,演示如何从控件中删除周末日列.

                  The following is a basic control override that demonstrates removing the weekend day columns from the control.

                  /*------------------------------------------------------------------------------
                   * 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());
                              }
                          }
                      }
                  }
                  

                  这篇关于使用 ASP.NET 日历控件时如何隐藏周末?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Google Calendar API,只需知道某人的电子邮件地址就 下一篇:如何使用 C# 从 Windows 10 日历中检索 UWP 中的约会

                  相关文章

                  最新文章

                  <legend id='XoAGs'><style id='XoAGs'><dir id='XoAGs'><q id='XoAGs'></q></dir></style></legend>
                  <tfoot id='XoAGs'></tfoot>
                • <i id='XoAGs'><tr id='XoAGs'><dt id='XoAGs'><q id='XoAGs'><span id='XoAGs'><b id='XoAGs'><form id='XoAGs'><ins id='XoAGs'></ins><ul id='XoAGs'></ul><sub id='XoAGs'></sub></form><legend id='XoAGs'></legend><bdo id='XoAGs'><pre id='XoAGs'><center id='XoAGs'></center></pre></bdo></b><th id='XoAGs'></th></span></q></dt></tr></i><div id='XoAGs'><tfoot id='XoAGs'></tfoot><dl id='XoAGs'><fieldset id='XoAGs'></fieldset></dl></div>

                    • <bdo id='XoAGs'></bdo><ul id='XoAGs'></ul>
                  1. <small id='XoAGs'></small><noframes id='XoAGs'>