asp.net 页面传输数据

来源:互联网 发布:照片换服装软件下载 编辑:程序博客网 时间:2024/06/05 09:14

最近写asp.net mvc的页面传值跳转,自己常用的方法Redirect,在页面往后台传数据用到的

$.get(),$.post(),$.ajax(),$.getJSON(),submit,因为接触的不久,写下来给自己记住.

redirect:

redirect是一个简单的页面跳转方法,方法很简单,也很好用,redirect("url"+参数);

跳转到Test下面的mytest页面并传参数test="test"过去:

           string test="test";

            string parameter = = "&test=" + "test";

            string url = "/Test/mytest?" + parameter;
            Redirect(url);

这样就能跳转到了mytest去;

在mytest中我们要接收到test的方法就通过 Request.QueryString来接收.

在test页面中,我们在index里写上string test = Request.QueryString["test"].ToString();这样我们就可以接收到页面传过来的test数据了

如果你的index页面要求你return一个view回去  那你就直接写return redirect (url);

页面传值给后台  ajax,submit

$.get(),$.post(),$.ajax(),$.getJSON():

ajax从页面给后台传值,这是一个常用的方法,下面就是给newadd.ashx通过post页面传值的一个过程:

页面代码:

$(function () {
                $("#btnadd").click(function () {

 var url = "newsadd.ashx";
                    var postdata = {

                        title:title;
                        id: id,
                       
                    };
                    $.ajax({
                        url: url,
                        dataType: 'json',//数据类型
                        type: "POST",
                        data: postdata,
                        success: function (json) {
                            if (json.status=="ok") {
                                location.href = "new.aspx";//返回正确之后跳转到new页面
                            } else {
                                layer.msg(json.info);
                            }
                        }
                    });

}

})

后台的接收

 

 public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";//详细列表网页格式
            string id = context.Request["id"];

            string title= context.Request["title"];

}

Post&get提交数据:

页面直接往后台post或get提交数据

function  loginSubmit()
        {
            var name = $("input[name='username']").val();
            var pwd = $("input[name='password']").val();
            var code = $("input[name='code']").val();
            var gourl = "/@(JN.Common.ConfigHelper.GetConfigString("AdminPath"))/Login";
            

           $.get('admin/login', {username: name, password: pwd, code: code}, function(data, status) {
           //data为返回对象,status为请求的状态
           //结果为success, error等等,但这里是成功时才能运行的函数
        });
}

string username= Request.QueryString["username"].ToString();

接收


function  loginSubmit()
        {
            var name = $("input[name='username']").val();
            var pwd = $("input[name='password']").val();
            var code = $("input[name='code']").val();
            var gourl = "/@(JN.Common.ConfigHelper.GetConfigString("AdminPath"))/Login";//传入的地址//"admin/Login"
            $.post(gourl, { username: name, password: pwd, code: code }, function (response) {
                if (response.status == "ok") {
                    window.location.href = response.data;
                } else {
                    if (response.status == "errcode") ChangeCheckCode();
                    alert(response.data);
                } 
            });
        }

string username=Request["username"];

接收

$.getJSON("newsdel.ashx?ids=" + str, function (json) {
                     var id = $(this).val();
                var url = 'newsupdateisshow.ashx?id=' + id + '&t=' + new Date().valueOf();//时间戳
                $.getJSON(url, function (json) {
                    layer.msg(json.info);
                })

})

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string id = context.Request.QueryString["id"];
            
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { status = "ok", info = "更新成功!" }));
                  }

接收


        

submit:

表单提交 通常使用在页面提交一整个表单

<form action="xxx.aspx" method="post">

  <button type="submit" btn-sm">提交</button>

后台:

public ActionResult Add(FormCollection form)

然后通过 string username = Tool.FilterSqlHtml(form["username"]);接收数据;

这就是这些天接触到的一些东西,记下来到时候哪天忘记了可以回头看看

0 0