请教:自写服务器日历控件为任何得不到值?
我写了一个服务器日历控件,客户端已经可以执行,但是当选择一个日期后提交后,Server端总是获不到所选的值,请高手帮忙看看,主要是Text属性,有分奖哦!--------------------编程问答-------------------- 你的类需要继承IPostBackDataHandler接口,比实现RaisePostDataChangedEvent和LoadPostData方法。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//[assembly: TagPrefix("Anilyu.WebCtrls", "Anilyu")]
namespace Anilyu
{
namespace WebCtrls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Calendar runat=\"server\"></{0}:Calendar>")]
[ValidationPropertyAttribute("Text")]
[System.Drawing.ToolboxBitmap("calendar.ico")]
public class Calendar : WebControl, IStateManager
{
private static int _count = 0;
private static string _script = null;
private string _calendarUrl = "Calendar/calendar0.htm", _text = "";
public static int Count
{
get { return _count++; }
}
protected override HtmlTextWriterTag TagKey
{
get { return HtmlTextWriterTag.Input; } //控件輸出後的HTML標記;
}
[Bindable(true),Category("Appearance"),Description("CSS語句")]
public string CssStyle
{
get { String s = (String)ViewState["CssStyle"]; return ((s == null) ? String.Empty : s); }
set { ViewState["CssStyle"] = value; }
}
[Browsable(true), Description("Calendar0.htm文件所在的URL路徑"), DefaultValue("Calendar0.htm"), Category("Appearance")]
public string CalendarUrl
{
get { return _calendarUrl; }
set { _calendarUrl = value; }
}
[Bindable(true),Category("Appearance"),DefaultValue(""),Description("The welcome message text."),Localizable(true)]
public virtual string Text
{
get
{
string s = (string)ViewState["Text"];
return (s == null) ? String.Empty : s;
}
set
{
ViewState["Text"] = value;
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.ID);// "Calendar" + Count.ToString());
writer.AddAttribute("index", Count.ToString());
writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "true");
writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
writer.AddAttribute(HtmlTextWriterAttribute.Type, "input");
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "AnilyuDatePanel(this.id)");
if (CssStyle != String.Empty)
{
writer.AddAttribute(HtmlTextWriterAttribute.Style, CssStyle);
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_script = "<!--IAttributeAccessor by AnilYu--><script language=\"javascript\">var p=document.getElementById(\"datepanel\");window.onload = function(){if (p==null){var box='<iframe id=\"datepanel\" frameborder=\"0\" onmouseout=\"this.style.display=\\'none\\'\" scrolling=\"no\" style=\"display:none;position:absolute;width:200px; height:160px\"></iframe>';var myframe=document.createElement(box);document.body.appendChild(myframe);myframe.src=\"" + _calendarUrl + "\";p=myframe; }} \n function AnilyuDatePanel(){var ele = document.getElementById(arguments[0]);if(datepanel.document.readyState==\"complete\"){datepanel.document.getElementById('pCtrl').value=ele.id;}else{return false;}if (p.style.display=='none'){p.style.display='';var souObj=document.getElementById(arguments[0]);if (souObj==null){return false;}else{var x=y=0, t; y+=parseInt(ele.offsetHeight); for(t=ele; t; t=t.offsetParent){if(t.offsetLeft && t.offsetTop){x += t.offsetLeft;y += t.offsetTop;}};p.style.left=x+\"px\";p.style.top=y+\"px\";}}}</script>";
}
protected override void RenderContents(HtmlTextWriter o)
{
if (_script != null)
{
o.Write(_script);
_script = null;
}
}
}
}
}
具体怎么实现参考MSDN
--------------------编程问答-------------------- 帮顶,没写过,时间都是用toyoutextbox --------------------编程问答-------------------- public class Calendar : WebControl, IStateManager, System.Web.UI.IPostBackEventHandler {
public bool LoadPostData(String postDataKey, NameValueCollection values) {
return(values[this.UniqueID]);
}
public void RaisePostDataChangedEvent() {
}
} --------------------编程问答-------------------- 接分先! --------------------编程问答-------------------- 不知,帮顶 --------------------编程问答-------------------- --------------------编程问答-------------------- 粗看了一下, 你的Text属性好像比较孤立. 日历控件一般是在客户端修改值, 在服务端获取, 你可以采用以下两种方式去
把客户端专到服务端:
1. HtmlHiddenControl传值.
2. 使用Page.ClientScript.RegistOnSubmit(可以名字写得不太准备)方法执行提交前的传值逻辑.
补充:.NET技术 , 组件/控件开发