C# 将信息传递到ashx页面并确定哪个图像将出现在asp图像控件中
如何将信息传递给处理程序页面?如何指定asp图像控件中显示的图像 Handler1.ashx.cs代码:C# 将信息传递到ashx页面并确定哪个图像将出现在asp图像控件中,c#,asp.net,image,handler,C#,Asp.net,Image,Handler,如何将信息传递给处理程序页面?如何指定asp图像控件中显示的图像 Handler1.ashx.cs代码: public void ProcessRequest(HttpContext context) { int id1 = something //how can I pass a information to a handler page int id2 = somthing 2 // same case
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代码:
<asp:Image ID="Image1" runat="server" />
<asp:Image ID="Image2" runat="server" />
谢谢:两件事:
ProcessRequest
中,需要context.Request.QueryString[“ID”]
来获取要添加到codebehind中的ID查询字符串参数李>
您必须对该值调用int.Parse
或int.TryParse
,因为RedImg
似乎期望一个int。最好的做法是首先从查询字符串中获取值,用string.IsNullOrEmpty
检查该值,如果是,则尽早退出。然后对该值使用int.TryParse
,然后也退出返回false的选项。您可能还应该将context.Response.StatusCode
设置为404或类似的设置,以使其更“合适”HTTP
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();