Search code examples
c#filehandler

Assign hidden field value in ashx page?


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"/>

Solution

  • (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.