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:
ProcessRequest, you needcontext.Request.QueryString["ID"]to get the ID query string parameter that you’re adding in the codebehind.You’ll have to call
int.Parseorint.TryParseon that value, sinceRedImgappears to expect an int. The best course of action there would be to first get the value from the query string, check that withstring.IsNullOrEmpty, and bail out early if it is. Then useint.TryParseon the value, and then also bail out of that returns false. You should probably also setcontext.Response.StatusCodeto 404 or something along those lines to make it more “proper” HTTP.