【问题标题】:how to pass the values from ASPX page to genric handler using query string如何使用查询字符串将值从 ASPX 页面传递到通用处理程序
【发布时间】:2016-03-09 00:26:04
【问题描述】:
 string ID = context.Request.QueryString["BudgetId"] == null ? null : context.Request.QueryString["BudgetId"].ToString();
            context.Response.ContentType = "text/Plain";
            string Budgetid = context.Request["ID"];
            string Country = context.Request["ddlcountry"];
            string State = context.Request["ddlstate"];
            string City = context.Request["ddlcity"];
            string Location = context.Request["txtlocation"];

上面我正在获取来自 ASPX 页面但 ID 来自 ASPX 页面的所有控制 IDS 如何在通用处理程序中传递国家、州、城市值

【问题讨论】:

  • 如何传递查询字符串?放代码
  • 这里所有的控制值都应该来自aspx,就像我选择了国家ID,相应的州城市应该在budgetID的帮助下去通用处理程序
  • 此时你不能因为注册的处理程序已经完成处理。
  • 将此控件选定值传递给处理程序的任何其他替代方法
  • 请看我的回答。您需要在 web config 的 handlers 部分注册您的处理程序,然后通过处理程序中的 Context 处理请求。

标签: asp.net generic-handler


【解决方案1】:

处理程序响应正在发出的请求。您可能想要检查处理程序内部而不是页面上的上下文。这样您就不必在 ASPX 页面中处理上述内容。完成后,您可以向会话添加一些值,以表明您处理了请求。

【讨论】:

    【解决方案2】:

    这是我连接到通用处理程序的 ASP.NET 示例实现。

    我的 WebForm1.aspx.cs 代码

    public partial class WebForm1 : System.Web.UI.Page
        {
            public string myCurrentUser = "MyUser";
            public string myCurrentUserID = "1";
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
        }
    

    示例 ASP.NET 页面。

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    
        </div>
        </form>
    </body>
    </html>
    
    <!--note that you need to have an updated jquery version here..-->
    <script src="jquery.min.js" type="text/javascript"></script>
    
    <script type="text/javascript">
    
        $(document).ready(function () {
            var PassValues = "?User=" + "<%=myCurrentUser%>" + "&ID=" + "<%=myCurrentUserID%>"
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Handler1.ashx" + PassValues,
                success: function (data) {
                    alert(data);
                },
                error: function (err) {
                    var str = err;
                }
            });
        });
    
    </script>
    

    注意:确保 Handler 的 $.AJAX URL PATH 正确

    我的 Handler1.ashx.cs 代码

    public class Handler1 : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("Hello User <" + context.Request.Params["User"] + "> with ID <" + context.Request.Params["ID"] + ">");
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2014-09-02
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多