我正在尝试这样做:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
trida_kalendar kal1 = new trida_kalendar();
kal1.SetID(5);
Response.Write(kal1.GetID());
trida_kalendar kal2 = new trida_kalendar();
stranka.Controls.Add(kal2.getcal());
}
private class trida_kalendar {
private int pint = -1;
public Calendar getcal() {
Calendar pokus = new Calendar();
pokus.ID = "pokus1";
pokus.Width = 500;
pokus.SelectionChanged += new EventHandler(pokus__SelectionChanged);
return pokus;
}
public void pokus__SelectionChanged(object sender, EventArgs e)
{
// But this doesnt work :-(
// It shows message: Cannot access a non-static member of outer type _Default
// via nested _Default.trida_kalendar
/*
Calendar kolo = (Calendar)stranka.FindControl("kalendar1");
string hh = kolo.SelectedDate.ToShortDateString();
Response.Write("Cosi" + hh);
*/
}
public int GetID() {
return pint;
}
public void SetID(int id) {
pint = id;
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<p>pokus</p>
<asp:PlaceHolder ID="stranka" runat="server" />
</form>
</body>
</html>
但这不起作用:-(它显示消息:
无法访问非静态成员 外部类型_Default通过嵌套 _Default.trida_kalendar
你能帮我吗?
答案 0 :(得分:0)
我找到了解决方案。这是它:
protected void Page_Load(object sender, EventArgs e)
{
Kalendar k2 = new Kalendar();
stranka.Controls.Add(k2.novyKalendar(1));
k2.vybraniDatumu += new EventHandler(udalost__vybraniDatumu);
}
private void udalost__vybraniDatumu(object sender, EventArgs e)
{
Calendar kalendar = (Calendar)stranka.FindControl("kalendar1");
string cislo = kalendar.SelectedDate.ToShortDateString();
Response.Write("Cosi" + cislo);
}
private class Kalendar {
public Calendar novyKalendar(int cid) {
Calendar kalendar = new Calendar();
kalendar.ID = ("kalendar" + cid);
kalendar.Width = 500;
kalendar.SelectionChanged += new EventHandler(kalendar__SelectionChanged);
return kalendar;
}
public EventHandler vybraniDatumu;
private void kalendar__SelectionChanged(object sender, EventArgs e)
{
if (vybraniDatumu != null)
{
vybraniDatumu(sender, e);
}
}
}
}
良好的编码