簡體   English   中英

如何在.aspx表單和.ashx處理程序之間傳遞值

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

我想根據用戶輸入(寬度,高度等)繪制圖像

我在.aspx頁面中有表單,但是我正在使用ashx處理程序繪制圖像。

我有一個繪制圖像的代碼,但是只能從預先指定的值中繪制。

現在我要做的是從.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

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();

已經嘗試過

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

        int __width = Convert.ToInt32(_width);

但該值似乎為空

Some1請幫我嗎?

謝謝!

更新

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

您沒有從.aspx頁發布(或獲取)到.ashx處理程序,因為這不是它的工作方式。 這就是為什么context.Request.QueryString.Get("input_width.Text")不起作用的原因。 同樣,也不需要“ .Text”,只需“ input_width”。

您需要將參數附加到對ashx的調用中:

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

和你的處理程序

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