简体   繁体   English

如何在.aspx表单和.ashx处理程序之间传递值

[英]How to pass values between .aspx form and .ashx handler

I want to draw image, based on user input (width, height, etc...) 我想根据用户输入(宽度,高度等)绘制图像

I have my form in .aspx page, but i'm drawing image using ashx handler. 我在.aspx页面中有表单,但是我正在使用ashx处理程序绘制图像。

I've got a code that draws image, but only from pre-assigned values. 我有一个绘制图像的代码,但是只能从预先指定的值中绘制。

Now what i want to do is get values from my .aspx form 现在我要做的是从.aspx表单获取值

.aspx .aspx

<span>Width</span>
<asp:TextBox ID="input_width" Width="125" Text="600" runat="server" ClientIDMode="Static"></asp:TextBox><br/>
<span>Height</span>
<asp:TextBox ID="input_height" Width="125" Text="400" runat="server"></asp:TextBox>

.ashx.cs .ashx.cs

int width = 600;
int height = 400;

Bitmap bmp = new Bitmap(width, height);

Graphics g = Graphics.FromImage((Image)bmp);
g.FillRectangle(Brushes.Red, 0f, 0f, bmp.Width, bmp.Height);

MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Png);

byte[] bajt = ms.ToArray();

context.Response.ContentType = "image/png";
context.Response.BinaryWrite(bajt);
context.Response.Flush();

Already tried this 已经尝试过

string _width = context.Request.QueryString.Get("input_width.Text");

        int __width = Convert.ToInt32(_width);

But the value seems to be null 但该值似乎为空

Some1 please help me? Some1请帮我吗?

Thank you! 谢谢!

UPDATE 更新

<a href="ImageGen.ashx">Press here</a><br />
        <img src="ImageGen.ashx" width="600" height="400"/>

You are not posting (or getting) from your .aspx page to your .ashx handler, as that is not how it works. 您没有从.aspx页发布(或获取)到.ashx处理程序,因为这不是它的工作方式。 This is why context.Request.QueryString.Get("input_width.Text") does not work. 这就是为什么context.Request.QueryString.Get("input_width.Text")不起作用的原因。 Also there is no need for ".Text", just "input_width". 同样,也不需要“ .Text”,只需“ input_width”。

You need to append the parameters to your call to the ashx: 您需要将参数附加到对ashx的调用中:

<img src="ImageGen.ashx?w=<%= input_width.Text %>&h=<%= input_height.Text %>" width="600" height="400"/>

and in your handler 和你的处理程序

string _width = context.Request.QueryString["w"];
int __width = Convert.ToInt32(_width);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM