在 UserControl (ASP.NET) 中时 URL 未得到解析
如果我向派生类 testA
传递一个包含 Hyperlink
的 PlaceHolder
,其 url 开头为 波浪号,它可以正确解决它。 但是,当我通过 testB
(除了继承自 System.Web.UI.UserControl
) 相同的 PlaceHolder
它 从字面上呈现它(不 转换/解决'~')
有什么想法吗?
public class testA : System.Web.UI.Control
{
public System.Web.UI.WebControls.PlaceHolder plc { get; set; }
protected override void OnLoad(EventArgs e)
{
if (plc != null)
this.Controls.Add(plc);
base.OnLoad(e);
}
}
public class testB : System.Web.UI.UserControl
{
public System.Web.UI.WebControls.PlaceHolder plc { get; set; }
protected override void OnLoad(EventArgs e)
{
if (plc != null)
this.Controls.Add(plc);
base.OnLoad(e);
}
}
这是 ASP.NET
If I pass the derived class testA
a PlaceHolder
that contains a Hyperlink
, with a url that starts with
a tilde, it resolves it correctly.
However, when I pass testB
(identical apart from it inherits fromSystem.Web.UI.UserControl
) the same PlaceHolder
It
renders it literally (doesn't
transform / resolve the '~')
Any ideas?
public class testA : System.Web.UI.Control
{
public System.Web.UI.WebControls.PlaceHolder plc { get; set; }
protected override void OnLoad(EventArgs e)
{
if (plc != null)
this.Controls.Add(plc);
base.OnLoad(e);
}
}
public class testB : System.Web.UI.UserControl
{
public System.Web.UI.WebControls.PlaceHolder plc { get; set; }
protected override void OnLoad(EventArgs e)
{
if (plc != null)
this.Controls.Add(plc);
base.OnLoad(e);
}
}
This is ASP.NET
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您继承
System.Web.UI.UserControl
并且不将您的控件与ascx
文件关联时,您的控件TemplateSourceVirtualDirectory
将不会被设置,这是ResolveClientUrl
方法所必需的 - 如果其为 null 或为空,则将按原样返回 url。要解决您的问题,只需设置
AppRelativeTemplateSourceDirectory
:When you inherit from
System.Web.UI.UserControl
and do not associate your control with anascx
file then your controlTemplateSourceVirtualDirectory
will not be set, this is required by theResolveClientUrl
method - if its null or empty the url will be returned AS IS.To solve your problem, just set
AppRelativeTemplateSourceDirectory
:UserControl 通常与
ascx
文件定义其标记。此类控件应使用 TemplateControl.LoadControl() 实例化 在将它们添加到页面之前,以便执行事件追赶。我怀疑事件追赶不会发生,因为您没有调用
LoadControl()
,因此Hyperlink
的NavigateUrl
永远不会获取有机会得到妥善解决。A UserControl is normally associated with an
ascx
file that defines its markup. Such controls should be instantiated using TemplateControl.LoadControl() before they're added to the page, in order to perform event catch-up.I suspect that event catch-up does not take place since you don't call
LoadControl()
, so theHyperlink
'sNavigateUrl
never gets a chance to be properly resolved.