我有以下代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DurationPicker.ascx.cs" Inherits="Permias.DurationPicker" %>
<%@ Register TagPrefix="Club" Namespace="ClubSite" %>
<div class="controlblock">
<table>
<tr>
<td>
Start Date:
</td>
<td>
<Club:DatePicker ID="dp1" runat="server" />
</td>
<td>
<Club:TimePicker ID="tp1" runat="server" />
</td>
</tr>
<tr>
<td>
End Date:
</td>
<td>
<Club:DatePicker ID="dp2" runat="server" />
</td>
<td>
<Club:TimePicker ID="tp2" runat="server" />
</td>
</tr>
</table>
</div>
在我的代码中我有
public System.DateTime startDateTime
{
get
{
return dp1.SelectedDate.Add(tp1.SelectedTime.TimeOfDay);
}
set
{
dp1.SelectedDate = value;
tp1.SelectedTime = value;
}
}
然而dp1用红色加下划线,这意味着它找不到dp1 ..为什么会这样?
答案 0 :(得分:1)
您需要@Register指令中的更多信息。这个控件在哪里?汇编引用或src引用。
答案 1 :(得分:0)
dp1
。您是否依靠设计师在代码背后创建一个字段?如果是这样,也许它没有在DurationPicker.ascx.designer.cs
文件中创建dp1引用。你需要这样的一行:
protected ClubSite.DatePicker dp1;
答案 2 :(得分:0)
这些似乎都没有解决我的问题......
在我的解决方案中,我有一个App_Code文件夹,里面有DataPicker.cs,它位于名称空间ClubSite
下面namespace ClubSite
{
public static class commonScript
{
public static void WritePopupRoutines(System.Web.UI.Page Page)
{
StringBuilder sb = new StringBuilder();
sb = new StringBuilder();
sb.AppendLine("var __popup_panel;");
sb.AppendLine("function __popup_clear() {");
sb.AppendLine(" if (__popup_panel != null ) ");
sb.AppendLine(" {");
sb.AppendLine(" document.getElementById(__popup_panel).style.display='none';");
sb.AppendLine(" __popup_panel=null;");
sb.AppendLine(" }");
sb.AppendLine("}");
sb.AppendLine("function __popup_losefocus(panel)");
sb.AppendLine("{");
sb.AppendLine(" if (!panel.contains(document.activeElement))");
sb.AppendLine(" {");
sb.AppendLine(" panel.style.display='none';");
sb.AppendLine(" }");
sb.AppendLine("}");
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "PopupRoutines", sb.ToString(), true);
}
}
public class DatePicker : WebControl, INamingContainer
{
private Calendar _innerCal;
private TextBox _innerTbx;
private string errorText = null;
private bool _panelvisible = false;
public DatePicker() : base(HtmlTextWriterTag.Div)
{
}
public System.DateTime SelectedDate
{
get
{
EnsureChildControls();
System.DateTime d=System.DateTime.Now;
try
{
d = System.DateTime.Parse(_innerTbx.Text);
errorText = null;
_innerCal.SelectedDate = d;
}
catch
{
errorText = "Date needs to be specified as mm/dd/yyyy";
}
return d;
}
set
{
EnsureChildControls();
_innerCal.SelectedDate = value;
_innerTbx.Text = value.ToShortDateString();
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
_innerTbx = new TextBox();
this.Controls.Add(_innerTbx);
_innerCal = new Calendar();
_innerCal.SelectionChanged += new System.EventHandler(_innerCal_SelectionChanged);
_innerCal.VisibleMonthChanged += new MonthChangedEventHandler(_innerCal_MonthChanged);
Controls.Add(_innerCal);
}
protected override System.Web.UI.HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
if (this.Width.IsEmpty)
{
this.Width = new Unit(150);
}
base.AddAttributesToRender(writer);
}
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
_innerTbx.Attributes.Add("Align", "AbsMiddle");
_innerTbx.Width = new Unit(100);
_innerTbx.RenderControl(writer);
string innerid = this.UniqueID + "_inner";
writer.AddAttribute("Align", "AbsMiddle");
writer.AddAttribute("src", "images/dropdownbtn.gif");
writer.AddAttribute("onClick", "__datepicker_showpopup('" + innerid + "')");
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
if (errorText != null)
{
writer.AddStyleAttribute("color", "red");
writer.AddStyleAttribute("display", "block");
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.Write(errorText);
writer.RenderEndTag();
}
writer.AddStyleAttribute("position", "relative");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.AddStyleAttribute("position", "absolute");
writer.AddStyleAttribute("left", "0px");
writer.AddStyleAttribute("top", "0px");
writer.AddStyleAttribute("z-index", "100");
string panelvisible = _panelvisible ? "block" : panelvisible = "none";
writer.AddStyleAttribute("display", panelvisible);
writer.AddStyleAttribute("background-color", "white");
writer.AddAttribute("id", innerid);
writer.AddAttribute("onfocusout", "__popup_losefocus(this)");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
_innerCal.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
}
protected override void OnPreRender(System.EventArgs e)
{
base.OnPreRender(e);
commonScript.WritePopupRoutines(Page);
StringBuilder sb = new StringBuilder();
if (_panelvisible)
{
sb.AppendLine("__popup_panel = '" + this.UniqueID + "_inner';");
}
sb.AppendLine("function __datepicker_showpopup(name)");
sb.AppendLine("{");
sb.AppendLine(" if (__popup_panel != null)");
sb.AppendLine(" {");
sb.AppendLine(" document.getElementById(__popup_panel).style.display='none';");
sb.AppendLine(" }");
sb.AppendLine(" __popup_panel=name;");
sb.AppendLine(" var panel=document.getElementById(__popup_panel);");
sb.AppendLine(" panel.style.display='block';");
sb.AppendLine(" var links=panel.getElementsByTagName('A');");
sb.AppendLine(" links[0].focus();");
sb.AppendLine(" window.event.cancelBubble=true;");
sb.AppendLine("}");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "popup", sb.ToString(), true);
Page.MaintainScrollPositionOnPostBack = true;
}
private void _innerCal_SelectionChanged(object sender, System.EventArgs e)
{
EnsureChildControls();
_innerTbx.Text = _innerCal.SelectedDate.ToShortDateString();
}
private void _innerCal_MonthChanged(object sender, MonthChangedEventArgs e)
{
_panelvisible = true;
}
}
}