2015-10-06 63 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; 
}