UserControl:FindControl无法找到内部控件
问题描述:
如果用户控件带有文本框。我在aspx页面上实例化这个用户控件。
现在页面加载事件如下所示:
If have a user control with a textbox. I instantiate this usercontrol on an aspx page.
Now the page load event looks like this:
protected void Page_Load(object sender, EventArgs e)
{
TrialControl tc = new TrialControl(); //This is my Usercontrol
Label labelUctext = (Label)(tc.FindControl("labelText1"));
if (labelUctext != null)
{
if (labelUctext.Text != String.Empty)
{
labelText.Text = labelUctext.Text;
}
}
}
这里labelUctext始终保持NULL。因此它无法找到内部标签。非常基本的问题,但我做错了什么?
Here labelUctext always holds NULL. thus it is not able to find the inner label. Extremely basic question, but what am I doing wrong?
答
将此方法添加到您的usercontrol.cs并调用此方法
喜欢这个
string str = usercontrol.AddProperty;
add this method into your usercontrol.cs and call this method
like this
string str=usercontrol.AddProperty;
public string AddProperty
{
get
{
return labelText1.Text;
}
set
{
labelText1.Text = value;
}
}
foreach(grdroomavail.Rows中的GridViewRow gr)
{
(Literal)tt =((Literal)gr.FindControl(ltrroom)as Literal).Text
}
foreach (GridViewRow gr in grdroomavail.Rows)
{
(Literal)tt=((Literal)gr.FindControl("ltrroom") as Literal).Text
}