javascript - 如何将 javascript 变量值从 .aspx 页面传递到 .ashx 页面

标签 javascript jquery asp.net vb.net ashx

当用户在 Web 表单中上传文档时,我尝试将一些 JavaScript 变量值从 .aspx 文件传递​​到 .ashx 文件。 Web 表单是一个 .aspx 文件,上传功能位于 .ashx 文件中。所以我需要将变量从 .aspx 文件传递​​到 .ashx 文件。

enter image description here

我能够传递三个变量,但是当我尝试传递第四个变量时,它不起作用。它不会给出任何错误,但只是不传递任何变量。当我调试代码时,我可以看到调试器没有进入上传过程。并且上传按钮也发生了变化

enter image description here

这是我的 .aspx 页面代码。

<%@ Page Title="" Language="VB" MasterPageFile="~/_resx/E4.master" AutoEventWireup="true" CodeFile="new.aspx.vb" Inherits="E4_Jobs_new" ValidateRequest="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
     <script type="text/javascript">
         var id = '<%= ModeID%>', mode = '<%= Mode%>', employer = '<%= Employer.Name %>', jtitle = document.getElementById(<%= txtTitle.ClientID%>);
     </script>

 <asp:RequiredFieldValidator runat="server" ControlToValidate="txtTitle" Display="None" ErrorMessage="xx" ValidationGroup="NewJob" EnableViewState="False" />
     <div class="form-element">
          <input type="text" id="txtTitle" runat="server" maxlength="64" /></div>

          <div class="m-accor-body">
                <ul id="attachmentList">
                    <% For Each additionalDoc As DataRow In Vacancy.Attachemnts.Rows%>
                    <li id="da<%= additionalDoc.Item("id") %>">
                        <span><%= additionalDoc.Item("name") %></span>
                        <span class="rd" data-did="<%= additionalDoc.Item("id")%>"> remove</span>
                    </li>
                    <%Next%>
                </ul>
                <div id="queue"></div>
                <input id="file_upload" name="file_upload" type="file">
           </div>
</asp:Content>

这是 .aspx 文件中的代码,我将变量传递到 .ashx 文件中

$(function () {

        dimNav('vacancy');

        $('#file_upload').uploadify({
            'buttonClass': 'button2',
            'buttonText': 'Select a PDF or DOCX file to upload..',
            'width': 250,
            'fileTypeExts': '*.pdf; *.doc; *.docx',
            'formData': {
                'draftId': id,
                'type': mode,
                'employer': employer,
                'jtitle': jtitle

            },
            'uploadLimit': 6,
            'swf': '/_resx/uploadify.swf',
            'uploader': '/e4/jobs/upload-job-attachments.ashx',
            'onUploadSuccess': function (file, data, response) {
                $('#attachmentList').append('<li id="da' + data + '"><span>' + file.name + '</span><span class="rd" data-did="' + data + '"> remove</span></li>');
            }
        });
    });

前三个变量值的获取方式与第四个不同。在第四个中,我使用 javascript 函数来获取值(因为它是用户给出的输入值。不是已经存储在数据库中的值。)。

这是我在 upload-job-attachments.ashx 文件中检索值的代码

 Public Class upload_job_attachments : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    context.Response.ContentType = "text/plain"
    context.Response.Expires = -1
    Try

        Dim Id As Integer = CInt(context.Request("draftid"))
        Dim type As String = CStr(context.Request("type"))
        Dim empname As String = CStr(context.Request("employer"))
        Dim postedFile As HttpPostedFile = context.Request.Files("Filedata")

          ********** other lines of code ***********

  End Sub
End Class

我能做些什么来克服这个问题。我对高级 JavaScript 功能很弱。感谢您的帮助

<小时/>

添加

整个脚本看起来像

      $(function () {
        dimNav('vacancy');

        var jobTitle = $('#' + '<%= txtTitle.ClientID%>').val();

        $('#file_upload').uploadify({
            'buttonClass': 'button2',
            'buttonText': 'Select a PDF or DOCX file to upload..',
            'width': 250,
            'fileTypeExts': '*.pdf; *.doc; *.docx',
            'formData': {
                'draftId': id,
                'type': mode,
                'employer': employer,
                'jtitle': jobTitle

            },
            'uploadLimit': 6,
            'swf': '/_resx/uploadify.swf',
            'uploader': '/e4/jobs/upload-job-attachments.ashx',
            'onUploadSuccess': function (file, data, response) {
                $('#attachmentList').append('<li id="da' + data + '"><span>' + file.name + '</span><span class="rd" data-did="' + data + '"> remove</span></li>');
                alert($('#' + '<%= txtTitle.ClientID%>').val());
            }
        });

        $('body').on('click', '.rd', function () {
            var el = $(this);
            $.post('delete-job-attachment.ashx?id=' + el.attr('data-did'), '{}', function () {
                $('#da' + el.attr('data-did')).remove();
            });
        });


        $('.price-button').click(function () { $(this).next('.price-list').fadeToggle('slow'); });

        $('.m-lists table tr:nth-child(4) td:nth-child(1)').prepend('<div>Multiple job posting</div>');

        $('.jMedias').change(function () {
            suffleMedias();
        });

        var suffleMedias = function () {
            var mids = [];
            $('.jMedias:checked').each(function () {
                mids.push($(this).val());
            });
            $('.mediaLists').val(mids.toString());
        };


        $('.jType').change(function () {
            suffleJobType();
        });


        $('input:radio.p-option-radio').change(function () {
            var pOption = $(this).val();
            $('.p-option').val(pOption);
        });

        var suffleJobType = function () {
            var type = $('.jType').val();
            if (type == 0) {
                $('#contractLength, #jobHour').slideUp();
            } else if (type == 1) {
                $('#jobHour').slideDown();
                $('#contractLength').slideUp();
            } else if (type == 2) {
                $('#jobHour').slideDown();
                $('#contractLength').slideUp();
            } else if (type == 3) {
                $('#contractLength, #jobHour').slideDown();
            }

        };

        var suffleFeeType = function () {
            var fType = $('.feeType').val();

            if (fType == 0) {
                $('#salaryRateMax, #salaryRateMin, #agencyFee').slideDown();
            } else if (fType == 1) {
                if (parseFloat($('.referrerPercentage option:selected').text()) > 0) {
                 } else {
                    $('#salaryRateMax, #salaryRateMin').slideUp();
                }
                $('#agencyFee').slideDown();
            } else if (fType == 2) {
                $('#agencyFee').slideUp();
                if (parseFloat($('.referrerPercentage option:selected').text()) > 0) {
                 } else {
                    $('#salaryRateMax, #salaryRateMin').slideUp();
                }
            }
        };

        $('.feeType').change(function () {
            suffleFeeType();

        });
        $('.referrerPercentage').change(function () {
            if (parseFloat($('.referrerPercentage option:selected').text()) > 0) {
                $('#salaryRateMax, #salaryRateMin').slideDown();
            } else {
                if ($('.feeType').val() == 1) {
                    $('#salaryRateMax, #salaryRateMin').slideUp();
                }
            }
        });


        $('.calcFee').change(function () {
            CalculateAndDisplayFees();
        });


        $('.rAgency').chosen().change(function () {
            if ($(this).val() != '-1') {
                $('.psls').val('-1').trigger("liszt:updated");
                $('.retained').val('1');
            }
        });

        $('.psls').chosen().change(function () {
            if ($(this).val() != '-1') {
                $('.rAgency').val('-1').trigger("liszt:updated");
                $('.retained').val('0');
            }
        });


        var setPublishOption = function () {
            var p = $('.p-option').val();
            var $radios = $('input:radio.p-option-radio');
            $radios.filter('[value=' + p + ']').attr('checked', true);
        };


        suffleJobType();
        suffleFeeType();
        suffleMedias();
        CalculateAndDisplayFees();
        setPublishOption();
    });

最佳答案

例如,如果 jtitle 字段需要来自输入控件,您可以这样做:

'formData': {
    'jtitle': $("#somecontrol").val()
}

将从控件中获取值。你是这个意思吗?

关于javascript - 如何将 javascript 变量值从 .aspx 页面传递到 .ashx 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36082991/

相关文章:

javascript - 导航菜单下的图标动画

javascript - 如果用户禁用了 JS,浏览器会下载 JS 文件吗?

javascript - Chrome下加载资源失败!目前不工作ajax

jquery - 使用 jQuery 悬停

javascript - 构建多页响应式表单的最佳方式是什么?

c# - SelectedIndexChanged 在动态下拉列表中不起作用

asp.net - 如何将 If...Then 逻辑复制为 Web 控件?

javascript - 如何在点击时删除特定的李列表?

c# - 有没有办法通过jquery将文本发送到aspcontrol

c# - onclick 第二次点击