簡體   English   中英

如何在Visual Studio 2010中將值從HTML傳遞到ASPX文件

[英]How to pass value from HTML to a ASPX files in Visual Studio 2010

我的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>

因此,如何將值從單個HTML文件傳遞到ASPX文件,ASPX文件是用於將值從單個HTML文件存儲到數據庫的?

假設用戶選中test1test2nokia的復選框,然后單擊Click me按鈕。 變量samsungapple將被傳輸到ASPX文件並存儲為變量,以后可以存儲到數據庫中。

對於您的信息,我不希望使用URL將變量信息傳遞給aspx文件。 (例如, http : //website.com/info.aspx? var = samsung&var =apple

這是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>

在您的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');
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM