Javascript Alert破坏我的asp.NET Web应用程序的布局

问题描述:

我知道有一个类似的问题,但该答案并没有解决我的问题。Javascript Alert破坏我的asp.NET Web应用程序的布局

这是我的应用程序的外观正常:

enter image description here

,这是它的外观JavaScript警告后(菜单项宽度被拧紧,左边的紫色栏增加了一个额外的部分它在上面):

enter image description here

这是我使用打电话报警代码:

private void Alert(string msg) 
    { 
     Response.Write("<script language = 'javascript'>window.alert('" + msg + "')</script>"); 
    } 

有没有人有过这个问题,即使是默认的asp.NET设计,我也有类似的东西。我怎样才能解决这个问题?顺便说一句,我使用的是IE 7,在Firefox上可以。

更新:

修正了紫色条问题,除去保证金。仍然找出菜单宽度问题。

这是我的CSS:

#menu 
{ 
    position: absolute; 
    left: 78%; 
    top: 108px; 
    width: 170px !important; 
} 

div.menu 
{ 
    padding: 4px 0px 4px 8px; 
} 

div.menu ul 
{ 
    list-style: none; 
    margin: 0px; 
    padding: 0px; 
    width: auto; 
} 

div.menu ul li a, div.menu ul li a:visited 
{ 
    background-color: #FFF; /*680840*/ 
    border: 1px #4e667d solid; 
    height: 20px; 
    width: 140px; 
    color: #000; /*FFF*/ 
    display: block; 
    line-height: 1.35em; 
    padding: 4px 20px; 
    text-decoration: none; 
    white-space: nowrap; 
} 

div.menu ul li a:hover 
{ 
    background-color: #680840; 
    color: #FFF; 
    text-decoration: none; 
} 

.selectedMenu 
{ 
    background-color: #680840; 
    color: #FFF; 
    text-decoration: none; 
} 

div.menu ul li a:active 
{ 
    background-color: #680840; 
    color: #cfdbe6; 
    text-decoration: none; 
} 

更新对于@Sassyboy:

前端:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <asp:HiddenField Value="" ID="errorMessageHidden" runat="server"/> 
    <script type="text/javascript"> 
     var alertMsg = document.getElementById('errorMessageHidden'); 
     if (alertMsg != null) alert(alertMsg); 
    </script> 

和C#我补充一点:

errorMessageHidden.Value = "Failed to Select - Test"; 
+0

看起来像头空格(或别的东西)是影响整个页面。你可以包括更多的上下文,也许在JS小提琴或链接到演示? – 2011-08-17 12:57:56

+2

具有高于``或DTD声明的元素给出了未定义的行为,这就是你所得到的。如何不将元素插入到Response流的开头。 – 2011-08-17 12:59:04

+0

@马特 - Sry基因没有链接的网页,而不能使用JS小提琴,因为页本身是好的,当警报方法是从我的C#文件称它是一种产生问题。 – 2011-08-17 13:02:01

Response.Write会在页面的HTML之前加入响应,这会导致类似于您提到的问题。

还有其他多种方法可以实现您尝试实现的功能。例如你可以有一个隐藏的字段hdnAlertMessage并用你想要提醒的消息设置它的值。然后检查客户端是否隐藏字段有值,然后提醒该值。

所以,在你的服务器端

hdnAlertMessage.Value = message; 

并在您的客户端

var alertMsg = document.getElementById('hdnAlertMessage'); 

if (alertMsg != null) 
    alert(alertMsg.value); 

或者类似的规定。

编辑:警报(ALERTMSG)提醒(alertMsg.value)

这是因为Response.Write经常呈现在页面的HTML之前 - 查看页面的源代码。

有添加JavaScript页面,查找ClientScriptManager的更好的方法。