2015-10-06 101 views
0

因此,我有一个包含产品列表的网页。每个产品旁边都有一个超链接,用户单击该超链接即可生成Word文档并供用户查看。每个产品都有一个ID,尺寸等...在aspx.cs文件中显示弹出消息框 - Visual Studio 2015

我的代码在aspx.cs代码后面执行,try-catch块检查所选产品是否有尺寸。如果产品没有任何尺寸,我希望弹出的消息框出现以提醒用户该产品的尺寸尚未记录,并且无法生成文档。

我的问题是,我该如何做到这一点?我已经尝试了Response.Write(...)函数和Page.ClientScript.RegisterStartupScript(...)函数。这两个版本都可以正常工作,但不会弹出消息。我知道我之前使用过Response.Write()函数,它在VS2013中工作,但在VS2015中似乎不起作用。

以下是我的代码。任何帮助/建议表示赞赏。

//Alert the user to fix the data if the parent sizes are missing from the Sizes column in ProductMart 
    try 
    { 
     //This is set to null to execute Catch block 
     parentSizeList = null; 
     pSizes = parentSizeList.Split(','); 
     foreach (var p in pSizes) 
      parentSizes.Add(p); 
    } 
    catch 
    { 
     Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('TEST');", true); 

     Response.Write("<script type=\"text/javascript\">alert('The Measurement Sizes are missing in the Product Mart for the parent product. " + 
      " Please fix the data for Pack Number " + product.PackNumber + " to print inspection document');</script>"); 
    } 
+0

代码看起来fine.check页面的源代码。是否已将您的javascript添加到 – shreesha

+0

在单击超链接生成代码后,当我执行查看页面源代码时,它似乎不会被添加。 –

+0

或者,您可以在构建要显示的产品列表时检查这些尺寸值,并且不要在无法创建文档的产品旁边显示链接。 – user2638401

回答

0

所以我找到了一种使用UpdatePanel的方法。以下是位于我的.aspx页面中的代码,它使用JavaScript警报显示在后面的代码中定义的消息。我将UpdatePanel添加到我的.aspx页面的最底部。

<asp:UpdatePanel id="UpdatePanel1" class="noSizesUpdatePanel" runat="server"> 
    <ContentTemplate> 
     <script type="text/javascript"> 
      alert('<%=message%>'); 
     </script> 
    </ContentTemplate> 
</asp:UpdatePanel> 

在我后面的代码Page_Load中,我做了一个UpdatePanel1.Visible = false隐藏警告框,直到需要。

我在后面的代码中创建了一个公共属性public string message;

然后在我的try-catch块:

try 
{ 
    //Do stuff 
} 
catch (Exception) 
{ 
    message = "Whatever you want your message to be"; 
    UpdatePanel1.Visible = true; 
}