原创控件代码共享--日期选择控件[3]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

function dateChange(pName)
{
 var selYear=eval('document.forms[0].'+pName+'_year');
 var year=selYear.options[selYear.selectedIndex].value;
 if (year!='')
 {
 var selMonth=eval('document.all.'+pName+'_month');
 var month=selMonth.options[selMonth.selectedIndex].value;
 if (month!='')
 {
   var date=new Date(year,month,0);
   var day=date.getDate();
   var selDay=eval('document.all.'+pName+'_day');
   var tmp=1;
   if (selDay.selectedIndex>0)
   {
tmp=selDay.options[selDay.selectedIndex].value;
   }
   selDay.length=day;
   for (i=1;i<10;i++)
   {
selDay.options[i-1].value='0'+i;
selDay.options[i-1].text=i;
   }
   for (i=10;i<=day;i++)
   {
selDay.options[i-1].value=i;
selDay.options[i-1].text=i;
   }
   if (tmp>day)
   {
selDay.selectedIndex=day-1;
   }
   else
   {
selDay.selectedIndex=tmp-1;
   }
 }
 }
}

</script>";
Page.RegisterClientScriptBlock("EnableDays",strJS);
base.OnPreRender(e);
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="writer"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<table border='0' cellpadding='0' cellspacing='0'><tr><td nowrap align='left'>");
int y=0;
int m=0;
int d=0;
string str=Text;
if (str.Length>=4)
{
y=Convert.ToInt32(str.Substring(0,4));
if (str.Length>=7)
{
m=Convert.ToInt32(str.Substring(5,2));
if (str.Length>=10)
{
d=Convert.ToInt32(str.Substring(8,2));
}
}
}
bool isDate=(DateFormat=="YMD");
if (Enabled)
{
bool isNull=IsNull;
writer.Write("<select name='"+this.UniqueID+"_year'");
if (isDate)
{
writer.Write(" onchange=\"dateChange('"+this.UniqueID+"')\"");
}
writer.Write(">");
if (isNull)
{
writer.Write("<option value=''></option>");
}
writer.Write("</select>年");
if (DateFormat.Length>1)
{
writer.Write("<select name='"+this.UniqueID+"_month'");
if (isDate)
{
writer.Write(" onchange=\"dateChange('"+this.UniqueID+"')\"");
}
writer.Write(">");
if (isNull)
{
writer.Write("<option value=''></option>");
}
writer.Write("</select>月");
writer.Write(@"
<script language='javascript'>
InitYear('"+this.UniqueID+"',"+Start.ToString()+","+Length.ToString()+","+y.ToString()+@");
InitMonth('"+this.UniqueID+"',"+m.ToString()+@");
</script>");
if (isDate)
{
writer.Write("<select name='"+this.UniqueID+"_day'>");
if (isNull)
{
writer.Write("<option value=''></option>");
}
writer.Write("</select>日");
writer.Write(@"
<script language='javascript'>
InitDay('"+this.UniqueID+"',"+d.ToString()+@");
</script>");
}
}
}
else
{
if (y==0 || m==0)
{
writer.Write("&nbsp;");
}
else
{
writer.Write(y.ToString()+"年"+m.ToString()+"月");
if (d!=0)
{
writer.Write(d.ToString()+"日");
}
}
}
writer.Write("</td></tr></table>");
}

public event EventHandler TextChanged;   

/// <summary>
/// 当由类实现时,为 ASP.NET 服务器控件处理回发数据。
/// </summary>
/// <param name="postDataKey">控件的主要标识符</param>
/// <param name="postCollection">所有传入名称值的集合</param>
/// <returns>如果服务器控件的状态在回发发生后更改,则为 true;否则为 false。</returns>
public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
String presentValue = Text;
String postedValue = postCollection[postDataKey];

if (presentValue == null || !presentValue.Equals(postedValue))
{
Text = postedValue;
return true;
}

return false;
}

     
/// <summary>
/// 当由类实现时,用信号要求服务器控件对象通知 ASP.NET 应用程序该控件的状态已更改。
/// </summary>
public virtual void RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}
     

本文关键:原创控件代码共享--日期选择控件
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top