2011-08-17 30 views
1

我知道有一個類似的問題,但該答案並沒有解決我的問題。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

回答

1

Response.Write會在頁面的HTML之前加入響應,這會導致類似於您提到的問題。

還有其他多種方法可以實現您嘗試實現的功能。例如你可以有一個隱藏的字段hdnAlertMessage並用你想要提醒的消息設置它的值。然後檢查客戶端是否隱藏字段有值,然後提醒該值。

所以,在你的服務器端

hdnAlertMessage.Value = message; 

並在您的客戶端

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

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

或者類似的規定。

編輯:警報(ALERTMSG)提醒(alertMsg.value)

3

這是因爲Response.Write經常呈現在頁面的HTML之前 - 查看頁面的源代碼。

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

相關問題