【问题标题】:Add ASP.NET Image from Page Method从页面方法添加 ASP.NET 图像
【发布时间】:2013-06-01 00:13:41
【问题描述】:

我在我的 ASP.NET Web 窗体应用程序后面的代码中向页面方法中的数据发出 Ajax 请求。我的 aspx 页面中有一个面板控件,但我无法从该页面方法及其 ID 中获取控件。我可以从Page_Load 事件中得到它。我该怎么做才能从页面方法中获取面板控件,或者它是不可能的,或者可能是替代方法?

<asp:Panel ID="pnlImages" runat="server"></asp:Panel>

<script type="text/javascript">
    function GetProductId() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GenerateQrCode",
            data: "{'Products':" + JSON.stringify(data) + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(xhr.responseText);
                alert(thrownError);
            },
            success: function (msg) {
                alert('Success');
            }
        });
    }
</script>
[WebMethod]
public static void GenerateQrCode(List<Product> Products)
{
    foreach(var product in Products)
    {
        string qrCodeLocation = products.Where(pid=>pid.ProductId == product.ProductId).Select(s=>s.QrCode).FirstOrDefault().ToString();

        Image image = new Image();
        image.ID = product.ProductId.ToString();
        image.ImageUrl = qrCodeLocation;
        //Cannot get 'pnlImages' here
    }
}

【问题讨论】:

    标签: asp.net image jquery


    【解决方案1】:

    您将无法做到这一点,WebMethod 不会遵循与普通页面方法相同的流程。它甚至不是实例方法,而是静态的。

    您必须在客户端返回信息并使用 javascript 创建图像。

    【讨论】:

    • 谢谢。我把它作为我的第二个选择。相当牵强,但无论如何我可以通过asp.net的Button控件及其OnClick事件将JSON(我的数据)传递给服务器吗?
    • @user2417645:你可以,但我不确定这与这个问题有什么关系。
    • 它与问题无关,但它符合我的目标。你宁愿我再提出一个问题吗?如果我的问题得到解答,我也很乐意这样做。
    • @user2417645:是的,这将是最好的,因为它与此问题无关,如果此问题对您有帮助,请将其标记为已解决。
    • 好的。知道了。感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多