DIV增加从code重定向和背景图像在asp.net背后 [英] div adding redirect and background image from code behind in asp.net
问题描述
我在 RUNAT =服务器
。
我想要做的,当用户点击 DIV
重定向到另一页是codebehind。我已经试过如下:
What I want to do is from codebehind when the user clicks on the div
redirect to another page. I've tried the following:
HTML
<div id="logoHead" runat="server"></div>
背后code:
logoHead.Attributes.Add("OnClick", "window.location = MyUrl");
这不工作,我该怎么做呢?
This doesnt work, how can I do this?
推荐答案
你是做什么工作就好了。你的问题是'Attributes.Add'是造成JavaScript来获得HTML EN codeD。这是因为您没有使用ASP.NET特定的控制。你的的onclick
作为渲染:
What you are doing will work just fine. The problem you have is the 'Attributes.Add' is causing the javascript to get HTML encoded. This is because you are not using an ASP.NET specific control. Your onclick
is rendering as:
onclick="window.location='MyUrl'"
要解决这个问题我把,我想添加任何JavaScript,为一个非ASP.NET控件以这种方式,在一个函数来消除任何奇怪的字符,这将是HTML EN codeD。例如:
To get around this I place any javascript that I want to add, to a NON ASP.NET control in this manner, in a function to eliminate any funny characters that will be HTML encoded. Example:
使用Javascript:
Javascript:
function redirect(address) {
window.location = 'http://' + address;
}
ASPX:
<div id="divTesting" runat="server">
Testing
</div>
背后code:
divTesting.Attributes.Add("onclick", "redirect('www.microsoft.com');");
这是只与您要添加 RUNAT =服务器
标签来访问这些服务器端HTML控件的一个问题。如果您打开 DIV
到 ASP:面板
再有就是对 Attribute.Add
键,你可以把任何你在那里想不代理javascript函数。这是一种令人讨厌的'功能',但我怀疑它的目的。
This is only a problem with HTML controls that you are adding a runat='server'
tag to access them server side. If you switch your div
to a asp:Panel
then there is no HTML encoding done on the Attribute.Add
and you can just put whatever you want in there without the proxy javascript function. This is a kind of an annoying 'feature' but I suspect it is intended.
这篇关于DIV增加从code重定向和背景图像在asp.net背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!