- //新增一個hidden變數, 前後端pass value可透過此變數(常用手法)
- <input type="hidden" id="abc" runat='server' />
- //新增一個js, 把要傳給後端的參數寫到hidden變數
- <script type="text/javascript">
- function setvalue() {
- var a = document.getElementById("abc");
- a.value = "這是js要給後端的參數";
- }
- </script>
test.aspx.cs(後端)
- Label1.Text = "承接js傳過來的參數是 "+ Request.Params["abc"];
後端CS傳值給前端JS
test.aspx.cs(後端)
- protected void Page_Load(object sender, EventArgs e) {
- form1.InnerHtml = "";
- for (int i = 0; i < 10; i++) {
- form1.InnerHtml += "<button onclick='send(" + i + ")'>click</button>";
- }
- }
test.aspx(前端)
- <script type="text/javascript">
- function send(id) {
- alert(id);
- }
- </script>