一个基于JS日历的WebControl日历控件(二)

翻译|其它|编辑:郝浩|2005-08-02 14:07:00.000|阅读 1714 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


接上面所述:
程序代码如下所示

#region 重写方法
protected override void OnLoad(EventArgs e)
{
   base.OnLoad (e);
   string strPath = this.ResourcePath == String.Empty ? "~/calendar/" :
   this.ResourcePath;
   Util.RegisterGlobalClientScript(strPath,"calendar.js",this.Page);
 if (!Page.IsPostBack && this.Visible && this.AutoFillNow && this.DateText ==

 String.Empty)
  {
    //对于自动填充时间要记住当前日期
    this.DateText = DateTime.Today.ToString("yyyy-MM-dd");
  }
}

public void RaisePostDataChangedEvent()
{
   // TODO: 添加 DateTimePicker.RaisePostDataChangedEvent 实现

   this.OnDateChanged(EventArgs.Empty);
}

public bool LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
  {
    // TODO: 添加 DateTimePicker.LoadPostData 实现
    string text1 = this.DateText;
    string text2 = postCollection[postDataKey];
    if (this.ShowTime)
    {
      string text3 = postCollection[postDataKey + "_Hour"];
      this.Hour = text3 == String.Empty ? 0 : Int32.Parse(text3);
      text3 = postCollection[postDataKey + "_Minute"];
      this.Minute = text3 == String.Empty ? 0 : Int32.Parse(text3);
      text3 = postCollection[postDataKey + "_Second"];
      this.Second = text3 == String.Empty ? 0 : Int32.Parse(text3);
     }
if (text1.Length == 0 || text2.Length == 0)
{
   if (!this.AllowNull && text2.Length == 0) throw new
   NullReferenceException("控件" + this.ClientID + "的值不能为空");
   if (text1 != text2)
    {
       this.DateText = text2;
       return true;
    }
}
else if (!DateTime.Parse(text1).Equals(DateTime.Parse(text2)))
  {
   this.DateText = text2;
   return true;
   }
  return false;
}
protected override void EnsureChildControls()
{
   if (!this.ChildControlsCreated)
   {
      base.EnsureChildControls ();
      if (!this.AllowNull)
   {
      RequiredFieldValidator reqVal = new RequiredFieldValidator();
      reqVal.ControlToValidate = this.ID;
      reqVal.ID = this.ID + "_reqVal";
      reqVal.CssClass = "CssError";
      reqVal.ErrorMessage = "*";
      reqVal.Display = ValidatorDisplay.Dynamic;
      this.Controls.Add(reqVal);
    }
 CompareValidator cmpVal = new CompareValidator();
     cmpVal.ID = this.ID + "_cmpVal";
     cmpVal.ControlToValidate = this.ID;
     cmpVal.CssClass = "CssError";
     cmpVal.ErrorMessage = "日期格式错误";
     cmpVal.Display = ValidatorDisplay.Dynamic;
     cmpVal.Type = ValidationDataType.Date;
     cmpVal.Operator = ValidationCompareOperator.DataTypeCheck;
     this.Controls.Add(cmpVal);
if (this.ShowTime)
{
    RangeValidator ragVal = new RangeValidator();
    ragVal.ID = this.ClientID + "_Hour_ragVal";
    ragVal.ControlToValidate = this.ClientID + "_Hour";
    ragVal.CssClass = "CssError";
    ragVal.ErrorMessage = "[0-23]";
    ragVal.Type = ValidationDataType.Integer;
    ragVal.MaximumValue = "23";
    ragVal.MinimumValue = "0";
    ragVal.Display = ValidatorDisplay.Dynamic;
    this.Controls.Add(ragVal);

    ragVal = new RangeValidator();
    ragVal.ID = this.ClientID + "_Minute_ragVal";
    ragVal.ControlToValidate = this.ClientID + "_Minute";
    ragVal.CssClass = "CssError";
    ragVal.ErrorMessage = "[0-59]";
    ragVal.Type = ValidationDataType.Integer;
    ragVal.MaximumValue = "59";
    ragVal.MinimumValue = "0";
    ragVal.Display = ValidatorDisplay.Dynamic;
    this.Controls.Add(ragVal);

    ragVal = new RangeValidator();
    ragVal.ID = this.ClientID + "_Second_ragVal";
    ragVal.ControlToValidate = this.ClientID + "_Second";
    ragVal.CssClass = "CssError";
    ragVal.ErrorMessage = "[0-59]";
    ragVal.Type = ValidationDataType.Integer;
    ragVal.MaximumValue = "59";
    ragVal.MinimumValue = "0";
    ragVal.Display = ValidatorDisplay.Dynamic;
    this.Controls.Add(ragVal);

   //伪控件
    TextBox txt = new TextBox();
    txt.ID = this.ClientID + "_Hour";
    this.Controls.Add(txt);
    txt = new TextBox();
    txt.ID = this.ClientID + "_Minute";
    this.Controls.Add(txt);
    txt = new TextBox();
    txt.ID = this.ClientID + "_Second";
    this.Controls.Add(txt);
   }
  //this.ChildControlsCreated = true;

  }
}
protected override void OnPreRender(EventArgs e)
{
   if ((this.Page != null) && this.Enabled)
    {
     this.Page.RegisterRequiresPostBack(this);
     }
  }
private void RenderThis(HtmlTextWriter writer)
{
   if (this.Page != null)
   {
      this.Page.VerifyRenderingInServerForm(this);
   }
   if (this.Visible)
   {
     writer.Write("<input type=\"text\" name=\"{0}\" id=\"{0}
     \"",this.ClientID);
     writer.Write(" style=\"width:78px\"");
  if (this.Enabled)
  {
    if (this.AutoPostBack)
    writer.Write(" onchange=\"javascript:{0}
    \"",Util.GetClientValidatedPostback(this));
   }
   else writer.Write(" disabled");
   if (this.DateText == String.Empty && this.AutoFillNow && !
     Page.IsPostBack) writer.Write(" value=\"{0}\"",DateTime.Today.ToString("yyyy-MM-dd"));
     else writer.Write(" value=\"{0}\"",this.DateText);
     writer.Write(" >");
     Control ctl;
     ctl = this.Controls[0];
     ctl.RenderControl(writer);
     int nTimeValidateCtlIndex = 1;
  if (!this.AllowNull)
   {
    nTimeValidateCtlIndex++;
    ctl = this.Controls[1];
    ctl.RenderControl(writer);
   }
    if (this.Enabled) writer.Write("<a href=#none class=\"CssCalButton\"
   onclick=\"javascript:calendar(document.all['{0}'])\">日历</a>",this.ClientID);
   if (this.ShowTime)
   {
     writer.Write("&nbsp;<input type=\"text\" name=\"{0}\"

     id=\"{0}\"",this.ClientID + "_Hour");
     writer.Write(" style=\"width:22px\"");
     if (!this.Enabled) writer.Write(" disabled");
     if (this.Hour == 0 && this.AutoFillNow && !Page.IsPostBack)
         writer.Write(" value=\"{0}\"",DateTime.Now.Hour);
     else writer.Write(" value=\"{0}\"",this.Hour);
         writer.Write(" >");
        ctl = this.Controls[nTimeValidateCtlIndex];
        ctl.RenderControl(writer);
        nTimeValidateCtlIndex++;
     writer.Write("<span class=\"{0}\">时</span>",this.CssClass);
     writer.Write("<input type=\"text\" name=\"{0}\" id=\"{0}
      \"",this.ClientID + "_Minute");
     writer.Write(" style=\"width:22px\"");
    if (!this.Enabled) writer.Write(" disabled");
    if (this.Minute == 0 && this.AutoFillNow && !Page.IsPostBack)
       writer.Write(" value=\"{0}\"",DateTime.Now.Minute);
       else writer.Write(" value=\"{0}\"",this.Minute);
        writer.Write(" >");
     ctl = this.Controls[nTimeValidateCtlIndex];
     ctl.RenderControl(writer);
     nTimeValidateCtlIndex++;
    writer.Write("<span class=\"{0}\">分</span>",this.CssClass);
    writer.Write("<input type=\"text\" name=\"{0}\" id=\"{0}
    \"",this.ClientID + "_Second");
    writer.Write(" style=\"width:22px\"");
if (!this.Enabled) writer.Write(" disabled");
if (this.Second == 0 && this.AutoFillNow && !Page.IsPostBack)
   writer.Write(" value=\"{0}\"",DateTime.Now.Second);
   else writer.Write(" value=\"{0}\"",this.Second);
   writer.Write(" >");
   ctl = this.Controls[nTimeValidateCtlIndex];
   ctl.RenderControl(writer);
    writer.Write("<span class=\"{0}\">秒</span>",this.CssClass);
     }
   }
}
protected override void Render(HtmlTextWriter writer)
{
   RenderThis(writer);
   //base.Render (writer);
}

#endregion
}
internal sealed class Util
{
  private Util()
   {
    //
    // TODO: 在此处添加构造函数逻辑
    //

    }
internal static String ResolveClientUrlSimple(string relativeUrl)
{

if (relativeUrl == null)
{
   throw new ArgumentNullException("relativeUrl");
   }
if (relativeUrl.Length == 0) return relativeUrl;
if (relativeUrl.StartsWith("~"))
{
string text1 = HttpContext.Current.Request.ApplicationPath;
  if (text1.Length == 0)
   {
    return relativeUrl;
     }
     return text1+relativeUrl.TrimStart('~');
     }
    return relativeUrl;
}
internal static string GetClientValidatedPostback(Control control)
{
   string text1 = control.Page.GetPostBackEventReference(control);
   return ("{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) " + text1 + "} ");
}
internal static void RegisterGlobalClientScript(string sDefaultPath, string sScriptFile,Page oPage)
{
  string sInstanceId = sScriptFile;
  if(oPage.IsClientScriptBlockRegistered(sInstanceId)) return;
  string sScript = string.Empty;
  sDefaultPath = ResolveClientUrlSimple(sDefaultPath);
try
{
  string sStandardRootClientScriptPath = Path.Combine(sDefaultPath, sScriptFile).Replace("\\", "/");

if(File.Exists(HttpContext.Current.Server.MapPath(sStandardRootClientScriptPath)))
{
   sScript = "<script language=\"javascript\" src=\"" + sStandardRootClientScriptPath + "\"   type=\"text/javascript\"></script>";
    }
}
catch {}

if(sScript == string.Empty)
{
try
  {
    string sAppRootClientScriptPath = Path.Combine(Path.Combine    (HttpContext.Current.Request.ApplicationPath, "js"), sScriptFile).Replace("\\", "/");

if(File.Exists(HttpContext.Current.Server.MapPath(sAppRootClientScriptPath)))
{
   sScript = "<script language=\"javascript\" src=\"" + sAppRootClientScriptPath + "\"  type=\"text/javascript\"></script>";
     }
  }
catch {}
}

// If everything failed, emit our internal script

if(sScript == string.Empty)
{
sScript = DemarcateClientScript(GetResourceContent(Assembly.GetExecutingAssembly().GetName().Name + "." + sScriptFile));
}


oPage.RegisterClientScriptBlock(sInstanceId, sScript);
  }
internal static string GetResourceContent(string sFileName)
{
   Stream oStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(sFileName);
   if (oStream == null) throw new FileNotFoundException("内嵌资源未找到。",sFileName);
   StreamReader oReader = new StreamReader(oStream);
   return oReader.ReadToEnd();
}
 internal static string DemarcateClientScript(string script)
  {
    return DemarcateClientScript(script, null);
   }
internal static string DemarcateClientScript(string script, string title)
{
  StringBuilder result = new StringBuilder();
  result.Append("<script language=\"javascript\" type=\"text/javascript\">\n");
  result.Append("//<![CDATA[\n");
  if (title != null)
   {
    result.Append("* "); result.Append(title); result.Append(" ***/\n");
      }
      result.Append(script);
      result.Append("\n");
      result.Append("//]]>\n");
      result.Append("</script>\n");
      return result.ToString();
     }
   }
}

 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP