Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在VisualStudio2010中将值从HTML传递到ASPX文件_Javascript_Asp.net_Html_Variables - Fatal编程技术网

Javascript 如何在VisualStudio2010中将值从HTML传递到ASPX文件

Javascript 如何在VisualStudio2010中将值从HTML传递到ASPX文件,javascript,asp.net,html,variables,Javascript,Asp.net,Html,Variables,我的HTML文件中有以下代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript"> function myFunction() { debugger; var checkedvalue = ""; var arrChecks =

我的HTML文件中有以下代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
<script type="text/javascript">

    function myFunction() {
        debugger;
        var checkedvalue = "";
        var arrChecks = document.getElementsByName("theCheckbox");

        for (i = 0; i < arrChecks.length; i++) 
        {
            // if the current state is checked, unchecked and vice-versa
            if (arrChecks[i].checked) {
                arrChecks[i].checked = false;
            } else {
                arrChecks[i].checked = true;
                checkedvalue = checkedvalue + " " + arrChecks[i].getAttribute('value');
            }

        }

        document.getElementById("demo").innerHTML = checkedvalue;
    }


    function makeCheckboxes(str) {
        var a = document.getElementById("blah");
        var arr = str;
        var returnStr = "";
        for (i = 0; i < arr.length; i++) {
            returnStr += '<input type="checkbox" name="theCheckbox" value="' + arr[i] + '" />' + arr[i];
        }
        a.innerHTML = returnStr;
    }

    window.onload = function () {
        var arrt = ["test1", "test2", "apple", "samsung", "nokia"];

        makeCheckboxes(arrt);
    };

</script>
<style type="text/css"></style>
</head>
<body>
   <table border="1">
      <tr>
         <td id="blah"></td>
         <td>checkboxes should appear left of here</td>
         <button onclick="myFunction()">Click me</button>
      </tr>
   </table>

         <p id="demo"></p>
</body>
</html>

函数myFunction(){
调试器;
var checkedvalue=“”;
var arrChecks=document.getElementsByName(“theCheckbox”);
对于(i=0;i

那么,如何将单个HTML文件中的值传递到ASPX文件,该文件是用来将单个HTML文件中的值存储到数据库中的

假设用户选中
test1
test2
nokia
的复选框,然后单击
单击我
按钮。变量
samsung
apple
将被传输到一个ASPX文件中,并作为变量存储,稍后我可以将其存储到数据库中


对于您的信息,我不希望使用URL将变量信息传递到aspx文件。(例如,

在您的HTML文件中,制作一个Javascript标记并将此代码放在上面

$.ajax({
            type: "POST",
            url: "../YourAspxpath/yourAspxFile.aspx",
            data: { param1: "value", param2: "value" },
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            dataType: "html",
            success: function (msg) {
                //do what ever you wnt here in case of success
            },
            error: function (xhr, status, error) {
                alert('Error');
            }
        });

这是一个html页面示例,向aspx页面发送变量,在aspx页面的cs文件中,您可以正常使用来自html页面的变量
在aspx的cs代码中

protected void Page_Load(object sender, EventArgs e)
        {
            string s = Request.Form.Get("param1");
        }
在HTML中

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Untitled Page</title>

    <script src="jquery-1.7.1.min.js" type="text/javascript"></script>

    <script src="jquery.mobile-1.1.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">

        myFunction = function() {
            var txtValue = document.getElementById("mytxt" ).value;
            try {
                $.ajax({
                    type: "POST",
                    url: "Default.aspx",
                    data:"param1=" + txtValue ,
                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                    dataType: "html",
                    success: function(msg) {
                    },
                    error: function(xhr, status, error) {
                        alert('Error');
                    }
                });
            }
            catch(e)
            {
                alert(e);
            }

        };


    </script>


</head>
<body>
    <table >
        <tr>
            <td id="blah">
                <input id="mytxt" type="text" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="button" value="Redirect" onclick="myFunction()" />
            </td>
        </tr>
    </table>
</body>
</html>

无标题页
myFunction=函数(){
var txtValue=document.getElementById(“mytxt”).value;
试一试{
$.ajax({
类型:“POST”,
url:“Default.aspx”,
数据:“param1=“+txtValue,
contentType:“application/x-www-form-urlencoded;charset=utf-8”,
数据类型:“html”,
成功:功能(msg){
},
错误:函数(xhr、状态、错误){
警报(“错误”);
}
});
}
捕获(e)
{
警报(e);
}
};

我可以知道
你的aspxfile.aspx
如何读取变量值吗?现在在aspxfile.cs的页面加载中使用Request.Form(param1),Request.Form(param2)我收到以下错误
非发票成员'System.Web.HttpRequest.Form'不能像方法一样使用。
是不是我必须将所有HTML代码复制到ASPX文件中,以便CS文件能够读取它们?(例如,我必须将HTML文件中的所有代码复制到myaspxfile.aspx中,以便myaspxfile.cs可以运行它们。)它也会给出错误
当前
中不存在名称“param1”
我添加了一个新答案检查它是的,您的第二个答案更好。顺便问一下,我可以知道哪本书教你你提供的代码吗?这不是一本我从搜索体验中得到的代码的书:)但不管怎样,你都可以试试这本书高级javascript“Chuck Easttom”大多数人说它很神奇,但我没有读过:)