我创建了一个用户控件(自定义日历 - 但没关系)。
另外,我有一个按钮控件,我的目标是在点击按钮时将日历(用户控件)添加到页面。页面中的日历数量不受限制,我不想只向页面添加一个日历。
protected void addCalendar_Click(object sender, EventArgs e)
{
// clicked on button, should add the user control.
}
用户控件已在页面上注册:
<%@ Register src="CalendarUserControl.ascx" tagname="CalendarUserControl" tagprefix="uc" %>
每个日历都应附加到以下div(此div在aspx页面中定义):
<div id="calendars-holder">
// first calendar appended
// second calendar appended
// and so on..
</div>
答案 0 :(得分:0)
protected int countCalendars;
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["countCalendars"] != null)
countCalendars = (int) ViewState["countCalendars"];
// init
if (!IsPostBack)
countCalendars = 0;
// register existing user-controls; won't occur before onclick button
// so ViewState["countCalendar"] will be exist.
for (int i = 0; i < countCalendars; i++)
addCalendar(i);
}
// onclick button
protected void addCalender_Click(object sender, EventArgs e)
{
addCalendar(countCalendars++);
ViewState["countCalendars"] = countCalendars;
}
// Adding a new User-Control
protected void addCalendar(int idNumber)
{
CalculatorUserControl Calendar = LoadControl("~/CalendarUserControl.ascx") as CalendarUserControl;
Calendar.ID = "CalculatorUserControl" + idNumber.ToString();
PlaceHolder1.Controls.Add(Calendar);
}