[英]Javascript Alert ruining the layout of my asp.NET Web Application
我知道有一個類似的問題,但那個答案並沒有解決我的問題。
這是我的應用程序的正常外觀:
這就是它在 Javascript 警報后的樣子(菜單項的寬度被搞砸了,左邊的紫色條在頂部添加了一個額外的部分):
這是我用來調用警報的代碼:
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";
這是因為 Response.Write 通常在頁面的 HTML 之前呈現 - 查看頁面的來源。
有更好的方法將 Javascript 添加到頁面,查找ClientScriptManager
。
Response.Write 將在頁面的 HTML 之前添加到響應中,這會導致與您提到的類似的問題。
還有多種其他方式可以實現您嘗試實現的功能。 例如,您可以有一個隱藏字段 hdnAlertMessage 並使用您想要提醒的消息設置它的值。 然后在客戶端檢查隱藏字段是否有值,然后警告該值。
所以在你的服務器端
hdnAlertMessage.Value = message;
在您的客戶端
var alertMsg = document.getElementById('hdnAlertMessage');
if (alertMsg != null)
alert(alertMsg.value);
或類似的規定。
編輯:警報(alertMsg)到警報(alertMsg.value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.