例如,我通过以下方式创建了一个 Web 控件:
<dnwc:TestControl runat="server" CurrentSite="test"></dnwc:TestControl>
我如何在代码后面(TestControl.cs)中获取 CurrentSite 的值?我试图通过这样做来获得价值:
this.Attributes["CurrentSite"]
但该值为空。
解决方案:
由于我无法发布答案,因此我将在此处添加。
我必须在 TestControl.cs 文件中声明该属性:
public string CurrentSite
{
get
{
string s = (string)ViewState["CurrentSite"];
return (s == null) ? "test2" : s;
}
set
{
ViewState["CurrentSite"] = value;
//Retrieve the current site and set it as an attribute in the input tag
inputTag.SetAttribute("data-current-site", value);
}
}
不幸的是,我只能在设置后才能访问该值。在创建实例后设置该值。所以在构造函数被调用之后。最初我需要在构造函数调用的方法中设置值,但现在就可以了。