简体   繁体   English

Javascript 警告破坏了我的 asp.NET Web 应用程序的布局

[英]Javascript Alert ruining the layout of my asp.NET Web Application

I know there is a similar question, but that answer did not solve my problem.我知道有一个类似的问题,但那个答案并没有解决我的问题。

This is how my application looks normally:这是我的应用程序的正常外观:

在此处输入图像描述

And this is how it looks after a Javascript alert (width of menu items gets screwed, the purple bar on the left has an extra part added to it on top):这就是它在 Javascript 警报后的样子(菜单项的宽度被搞砸了,左边的紫色条在顶部添加了一个额外的部分):

在此处输入图像描述

This is the code I am using to call an alert:这是我用来调用警报的代码:

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

Did anyone ever had this problem, I had something similar even with the default asp.NET design.有没有人遇到过这个问题,即使使用默认的 asp.NET 设计,我也有类似的情况。 How can I fix this?我怎样才能解决这个问题? I am using IE 7 by the way, it's ok on Firefox.顺便说一句,我使用的是 IE 7,在 Firefox 上没问题。

Update:更新:

Fixed the issue with the purple bar, removed a margin.修复了紫色条的问题,删除了边距。 Still figuring out the menu width issue.仍在解决菜单宽度问题。

This is my CSS:这是我的 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;
}

Update For @Sassyboy: @Sassyboy 的更新:

Front-End:前端:

<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>

and c# I add this:和 c# 我添加了这个:

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

This is becuase Response.Write often is rendered before the HTML of the page - look at the source of your page.这是因为 Response.Write 通常在页面的 HTML 之前呈现 - 查看页面的来源。

There are better ways of adding Javascript to the page, look up ClientScriptManager .有更好的方法将 Javascript 添加到页面,查找ClientScriptManager

Response.Write will add to response before the HTML of the page and this leads to issues similar to what you mentioned. Response.Write 将在页面的 HTML 之前添加到响应中,这会导致与您提到的类似的问题。

There are multiple other ways in which the functionality you are trying to achieve can be achieved.还有多种其他方式可以实现您尝试实现的功能。 eg you could have a hidden field hdnAlertMessage and set its value with the message you want to alert.例如,您可以有一个隐藏字段 hdnAlertMessage 并使用您想要提醒的消息设置它的值。 And then check on the client side if the hidden field has a value then alert that value.然后在客户端检查隐藏字段是否有值,然后警告该值。

So on your server side所以在你的服务器端

hdnAlertMessage.Value = message;

And on your client side在您的客户端

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

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

Or something along those lines.或类似的规定。

Edited: alert(alertMsg) to alert(alertMsg.value)编辑:警报(alertMsg)到警报(alertMsg.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM