如何在asp.net日历控件中查找控件
本文关键字:控件 查找 日历 net asp | 更新日期: 2023-09-27 18:12:07
在dayrender事件中添加控件后,是否有办法稍后找到该控件?我试过了
calendar.FindControl("lblSample")
但没有成功。
下面是我的一些代码,以便更清楚:
protected void calSample_DayRender(object sender, DayRenderEventArgs e)
{
Label lblSample = new Label();
lblSample.ID = "lblSample";
lblSample.Text = "Sample";
e.Cell.Controls.Add(lblSample);
}
在day渲染事件和页面完全加载之后,我有一个链接按钮事件,我试图把控制权拿回来
protected void lbtnSave_Click(object sender, EventArgs e)
{
//Not working
Label lblSample = calSample.FindControl(lblSample);
//Also can't get to work, this was using Ross' suggestion and the recursive find function he wrote about. I'm probably just not using it correctly.
Label lblSample = ControlFinder.FindControl<Label>(calSample, "lblSample");
}
这个问题是因为控件在dayrender方法之前没有被添加到页面中——这意味着你不能在post back中获得对它的引用。使用Page.Request.Params集合,OP能够在回发时获取值。
问题是find控件不是递归的,您想要的控件可能在另一个控件中。
下面展示了如何创建一个递归查找控制方法:http://stevesmithblog.com/blog/recursive-findcontrol/
或者,如果你发布日历控件代码,我可能会帮助你多一点。
罗斯这个答案是因为Ross上面的评论告诉我,我可以使用Page.Request.Params来找到我想要的值。这不是最干净的解决方案,但它有效!
如果您在日期渲染事件中向日历控件添加下拉列表
protected void calSample_DayRender(object sender, DayRenderEventArgs e)
{
DropDownList ddlSample = new DropDownList();
ddlSample.ID = "ddlSample";
ddlSample.DataSource = sampleDS;
ddlSample.DataBind();
e.Cell.Controls.Add(ddlSample);
}
你可以像这样得到选中的值,当然我需要做更多的检查来验证下拉列表是否存在,但是你得到的是图片
protected void lbtnSave_Click(object sender, EventArgs e)
{
string sampleID = Page.Request.Params.GetValues("ddlSample")[0];
}