Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.net中创建嵌入式图像按钮?_C#_Sql_Asp.net_Database_Imagebutton - Fatal编程技术网

C# 在ASP.net中创建嵌入式图像按钮?

C# 在ASP.net中创建嵌入式图像按钮?,c#,sql,asp.net,database,imagebutton,C#,Sql,Asp.net,Database,Imagebutton,我需要在ASP.net中创建一个按钮,以便嵌入到另一个网页中 基本上,该按钮将在页面加载时查询DB服务器端,然后根据查询的返回更改其图像 有人知道最好的方法吗?你可以看出,我不是ASP.net的专家。最好用C写 在这种情况下,我建议使用IHttpHandler而不是一般的web表单 处理程序更适合此请求,因为它将能够快速响应,并且专门用于处理不一定基于HTML的特定请求。连接起来接受请求、查询数据库并生成您选择的图像非常简单。现在,您还没有提供有关图像来源的详细信息,但让我们看一个简单的请求 要

我需要在ASP.net中创建一个按钮,以便嵌入到另一个网页中

基本上,该按钮将在页面加载时查询DB服务器端,然后根据查询的返回更改其图像


有人知道最好的方法吗?你可以看出,我不是ASP.net的专家。最好用C写

在这种情况下,我建议使用IHttpHandler而不是一般的web表单

处理程序更适合此请求,因为它将能够快速响应,并且专门用于处理不一定基于HTML的特定请求。连接起来接受请求、查询数据库并生成您选择的图像非常简单。现在,您还没有提供有关图像来源的详细信息,但让我们看一个简单的请求

要在webforms web应用程序中开始,请选择一个新的GenericHandler,我们将其命名为DynamicImage.ashx。这将构建我们的初始模板,如下所示

public class DynamicImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
此模板提供了处理我们请求的基础。当请求到达时,Web服务器将执行ProcessRequest方法,该方法作为参数传入HttpContext。从这里,我们可以用它来表达我们的反应

出于参数原因,让我们假设我们正在基于QueryString参数username查询图像,该参数表示数据库中的用户。我已经在实现这一点的步骤中包含了一些基本代码。代码注释

public void ProcessRequest(HttpContext context)
{
    //get our username from the query string
    var username = context.Request.QueryString["username"];

    //clear the response and set the content type headers
    context.Response.Clear();
    context.Response.ContentType = "image/png";

    //if the username is empty then end the response with a 401 not found status code
    if (string.IsNullOrWhiteSpace(username))
    {
        context.Response.StatusCode = 401;
        context.Response.End();
        return;
    }

    //do a db query to validate the user. If not valid do a 401 not found
    bool isValidUser = new UserManager().IsValidUser(username);
    if (!isValidUser)
    {
        context.Response.StatusCode = 401;
        context.Response.End();
        return;
    }

    //get the user image file path from a server directory. If not found end with 401 not found
    string filePath = context.Server.MapPath(string.Format("~/App_Data/userimages/{0}.png", username));
    if (!System.IO.File.Exists(filePath))
    {
        context.Response.StatusCode = 401;
        context.Response.End();
        return;
    }

    //finish the response by transmitting the file
    context.Response.StatusCode = 200;
    context.Response.TransmitFile(filePath);
    context.Response.Flush();
    context.Response.End();
}
要调用此处理程序,只需将映像的src设置为类似于/DynamicImage.ashx?username=johndoe的路径

现在您的要求可能略有不同。例如,您可能正在以字节[]的形式从数据库中检索图像,因此您可能希望使用context.Response.BinaryWrite方法而不是context.Response.TransmitFile方法。此方法将字节[]作为响应流传输


最后,我想让您参考我的另一篇文章,它从客户机的角度讨论如何缓存这些图像。如果您的按钮会频繁生成,这将非常有用

它可以是像

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/arrow-down.gif" OnClick="ImageButton1_Click" />
如果我理解你的要求

要将其置于页面加载下,如下所示:

private void Page_Load()
{
    if(!Page.IsPostBack)
    {
        // perform db processing here
        ImageButton.ImageUrl = "~/arrow-up.gif";
    }
}

这就是所需要的一切。设置ImageUrl行可以放在任何需要的地方。

不确定从何开始。我只是想弄清楚如何制作一个只需一个按钮的简单视图。我可以做的编码肯定。。。但是我不能用我的生命去获得一个模板,它只是一个按钮,这样我就可以点击它并编写数据库连接服务器端+所有其他东西。我相信这个解决方案会起作用。本质上,它查询的状态是可用性的“是”或“否”状态。如果是,则显示一个图像,如果不是,则显示另一个图像。我需要在页面加载时进行DB处理。。。若要更改按钮图像。@willxj:只需将“单击”事件更改为“页面加载”事件。也把它包在里面!IsPostBacknaveen是对的。这是一个简单的问题,把线放在你需要的地方。我已经相应地更新了答案。
private void Page_Load()
{
    if(!Page.IsPostBack)
    {
        // perform db processing here
        ImageButton.ImageUrl = "~/arrow-up.gif";
    }
}