2

I need to assign filename name variable to hidden field value in ashx page, how can I assign value to hidden field in ashx page?

.ashx page

public void ProcessRequest(HttpContext context)
{
    var file = context.Request.Files[0];
  //here i need to pass this file name in hidden field value
}

This is aspx page where hidden filed present

  <asp:HiddenField ID="hdnFileName" runat="server"/>
Oded
  • 489,969
  • 99
  • 883
  • 1,009
user1527989
  • 139
  • 4
  • 14

2 Answers2

1

(Unless I'm very much mistaken..) ASHX is a webservice, not some code-behind. If you want to get the value of that field, you need to post your form to the corresponding URL of the .ASHX file, or use AJAX.

If you want to return data, I advise you to use AJAX.

EDIT: According to MSDN, my statement is correct. .ASHX is ment for HttpHandlers that do not have a UI.

Generic Web handler (*.ashx) The default HTTP handler for all Web handlers that do not have a UI and that include the @ WebHandler directive.

Example of how to post with AJAX:

$(function(){
    $.ajax({
            url:'location of your ashx goes here',
            type: 'post',
            success: function(data){
            $("#hdnFileName").val(data);
     }
};

Your ASHX would return the data:

public string ProcessRequest(HttpContext context)
{
    var file = context.Request.Files[0];
  //here i need to pass this file name in hidden field value
    return fileName;
}

note: see also https://stackoverflow.com/a/8758614/690178 for uploading files using AJAX.

Community
  • 1
  • 1
Yoeri
  • 2,249
  • 18
  • 33
  • 1
    if you explain it by using code measn it will be help full,how can i send filename from ashx page to ajax – user1527989 Jul 23 '12 at 19:43
  • 2
    updated my answer with some examples. Note these are of the top of my head, but they show the general idea. – Yoeri Jul 23 '12 at 19:49
0

An ASHX is just a raw ASP.NET web handler file. Which means that you implement an IHttpHandler interface that defines a property IsReusable and a method ProcessRequest that gets an HttpRequest and HttpReponse passed in an HttpContext argument. A typical ASHX implementation looks somehow like this:

public class Handler : IHttpHandler
{
    public void ProcessRequest (HttpContext context) 
    {
        // Access the raw HttpRequest and HttpResponse via context
    }

    public bool IsReusable 
    {
        get 
        {
            return false; // Define if ASP.NET may reuse instance for subsequent requests
        }
    }
}

So you don't create a hidden field in a handler file that misses any HTML or view abstractions. What you could do is to write raw HTML output to the response as string and declare a hidden field via

<input type="hidden" name="somename" />

I would not recommend doing this in an ASHX handler. If you need HTML output take a look at ASPX Pages or ASCX Controls.

saintedlama
  • 6,838
  • 1
  • 28
  • 46