【问题标题】:Showing pop up messagebox in aspx.cs file - Visual Studio 2015在 aspx.cs 文件中显示弹出消息框 - Visual Studio 2015
【发布时间】:2015-10-06 15:05:59
【问题描述】:

所以我有一个包含产品列表的网页。每个产品旁边都有一个超链接,用户单击该超链接可生成 Word 文档,供用户查看。每个产品都有一个 ID、尺寸等...

我有在后面的 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>");
    }

【问题讨论】:

  • 代码看起来不错。请检查页面源代码。你的 javascript 添加与否
  • 点击超链接生成代码后做查看页面源码时好像没有添加。
  • 或者,您可以在构建要显示的产品列表时检查这些尺寸值,而不是在无法创建文档的产品旁边显示链接。
  • 同意。我已经考虑过了。这可能是我唯一的选择,因为我似乎仍然找不到解决方案。

标签: c# asp.net visual-studio-2015


【解决方案1】:

所以我找到了一种使用 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多