我在Default.aspx页面中调用了一个自定义用户控件:
<myPrefix:myTagName ID="category" runat="server" />
自定义用户控件的代码:
namespace WebApplication3.inc.controls {
public partial class myTag : System.Web.UI.UserControl {
public String id;
public String name;
public String value;
public Hashtable attributes;
protected void Page_Load(object sender, EventArgs e) {
if (this.id != null)
this.attributes["id"] = this.id;
if (this.name != null)
this.attributes["name"] = this.name;
if (this.value != null)
this.attributes["value"] = this.value;
}
}
}
在Default.aspx.cs代码中的,我想设置这个标签的值:
this.articleCategory.value = this.currentArticle.Category.ToString();
我在最后一行的'value'处得到此错误:
'System.Web.UI.UserControl'没有 包含'value'的定义 没有延伸方法'价值'接受 类型的第一个参数 'System.Web.UI.UserControl'可以 发现(你错过了使用 指令或程序集引用?)
我见过的所有演示似乎表明这应该有效。我错过了什么吗?
答案 0 :(得分:2)
错误消息表明您的网页类中对控件实例的引用当前为System.Web.UI.UserControl
类型。它必须是WebApplication3.inc.controls.myTag
类型才能访问value
成员。解决此问题的一种方法是将变量的声明移动到代码隐藏文件,而不是让Visual Studio自动生成变量,但这样做的确切方式取决于项目设置以及如何在页面中包含控件。
答案 1 :(得分:0)
这可能是因为您在错误的时间调用它,首先在页面上调用Page_Load
,然后在页面上调用所有子控件。也许您只需要更改控件以在Init
而不是Page_Load