Multiple WebResources output to html when using recursive server controls
I have created a server control which uses rec开发者_如何学运维ursion to create a menu. It essentially outputs nested lists with anchors inside them.
Now, this works perfectly, however, since the control is calling itself to render the child menus, it is also rendering multiple identical WebResource.axd javascript file references in .
Any idea how to stop this from happening? I just want the one reference in my tag.
I ended up solving my own problem by making a private constructor of the form:
private CustomControl(int level)
{
this._Level = level + 1;
}
The public constructor looked like:
public CustomControl() : base()
{
_Level = 1;
}
Then, when outputting the client scripts in the OnInit method, I checked whether _Level == 1, only outputting the client scripts if _Level == 1, like so:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if(_Level == 1)
{
// Add client scripts to this.Page.Header.Controls
}
}
I'm not sure if this was the best way, but it worked for me.
精彩评论