问题描述:

How can I pass an information to a handler page? And how can I specify which image will be displayed in the asp-image-control?

Handler1.ashx.cs code:

public void ProcessRequest(HttpContext context)

{

int id1 = something //how can I pass a information to a handler page

int id2 = somthing 2 // same case

byte[] IMG = classP.RedImg(id1);

context.Response.ContentType = "image/jpg";

context.Response.BinaryWrite(IMG);

byte[] IMG2 = classP.RedImg(id2);

context.Response.ContentType = "image/jpg";

context.Response.BinaryWrite(IMG2);

}

public bool IsReusable

{

get

{

return false;

}

}

Page.aspx code:

<asp:Image ID="Image1" runat="server" />

<asp:Image ID="Image2" runat="server" />

Page.aspx.cs code behind:

string[] data = classC.ReadClient();

int id1 = Convert.ToInt32(data[0]); //Here is id1 value

int id2 = Convert.ToInt32(data[1]); //Here is id2 value

Image1.ImageUrl = "~/Handler1.ashx?ID=" + id1.ToString();

Image2.ImageUrl = "~/Handler1.ashx?ID=" + id2.ToString();

thanks :]

网友答案:

Two things:

  1. The handler will be called once per request. So returning both images like that will not work.
  2. In ProcessRequest, you need context.Request.QueryString["ID"] to get the ID query string parameter that you're adding in the codebehind.

You'll have to call int.Parse or int.TryParse on that value, since RedImg appears to expect an int. The best course of action there would be to first get the value from the query string, check that with string.IsNullOrEmpty, and bail out early if it is. Then use int.TryParse on the value, and then also bail out of that returns false. You should probably also set context.Response.StatusCode to 404 or something along those lines to make it more "proper" HTTP.

相关阅读:
Top