简体   繁体   English

客户端数据到服务器端

[英]Client side data to server side

I'm trying to use a JavaScript popup box (prompt) to get some user input on my website, and then do some more actions on the server-side based on what the user does. 我试图使用JavaScript弹出框(提示)在我的网站上获得一些用户输入,然后根据用户的行为在服务器端执行更多操作。

The popup box is, for lack of words, popping up. 由于缺少单词,会弹出一个弹出框。

The following is the code that I have tried to use for this: 以下是我尝试用于此的代码:

<div>
    <asp:HiddenField ID="hidden" runat="server" />
</div>
<script>
    function userInput() {
        var reason = prompt("Enter reason for deleting:", "");
        //User pressed okay but didn't type anything
        while (reason == "") {
            //Keeps cycling until reason given or cancel is hit
            reason = prompt("Enter reason for deleting:", "");
        }
        if (reason != "" && reason != "Code:CancelDelete") {
            //User typed something and hit okay
            document.getElementById('hidden').innerHTML = reason.toString();
            $('#deleteReason').val(reason.toString());
            $("#hidden").val(reason.toString());
        }
        else {
            //User hits cancel
            document.getElementById('hidden').nodeValue = "Code:CancelDelete";
        }
    }
</script>

The while loop in the script works for what I need it to do. 脚本中的while循环适用于我需要做的事情。 The problem from what I can tell is trying to set the value of the HiddenField. 我所知道的问题是试图设置HiddenField的值。 I have tried the following ways: 我尝试了以下方法:

  • innerHTML 的innerHTML
  • innerText 的innerText
  • nodeValue 的nodeValue

While looking into this, I have seen .value used a lot and have tried it myself but when I go to type document.getElementById('hidden').value = , there is no popup option or description for .value . 在查看此内容时,我看到.value经常使用并亲自尝试过,但是当我键入document.getElementById('hidden')。value =时.value没有弹出选项或描述。

I have tested the server side code and so I know that works. 我已经测试了服务器端代码,所以我知道这可行。 It all comes down to getting the user input. 归结为获得用户输入。 Either way, here is an excerpt from the c# code: 无论哪种方式,以下都是C#代码的摘录:

string deleteReason = hidden.Value;
//string deleteReason = test.InnerHtml.ToString();
if (deleteReason.Equals("Code:CancelDelete"))
{

}
else if (!deleteReason.Equals("Code:CancelDelete") && !deleteReason.Equals(""))
{

More or less at a loss on this one. 或多或少对此不知所措。

Update 1: Here is the html code generated on the client side browser(Firefox) for the hidden field: 更新1:这是在客户端浏览器(Firefox)上为隐藏字段生成的html代码:

<input name="ctl00$IndividualPageContent$hidden" 
id="IndividualPageContent_hidden" type="hidden">

When you type an element ID on webform, the asp.net gives it a unique ID based on some things (Your form, your repeater, etc...) 当您在Webform上键入元素ID时,asp.net会根据某些因素(您的表单,中继器等)为其提供唯一ID。

If you want to use jQuery with this ID, you can use the ClientId prop. 如果要使用具有此ID的jQuery,可以使用ClientId属性。

Something like this: 像这样:

if (reason != "" && reason != "Code:CancelDelete") {
            //If your server id= "hidden"
            ele = $("#<%= hidden.ClientID %>");
            ele.html() = reason.toString();
            ...
        }

Another option is to add the static ID to your server element, and then your code will work as is. 另一个选择是将静态ID添加到您的服务器元素,然后您的代码将按原样工作。 (the html will be rendered with ID = hidden) (HTML将以ID =隐藏的形式呈现)

ClientIDMode="static" 的ClientIDMode = “静态”

<div>
    <asp:HiddenField ID="hidden" runat="server" ClientIDMode="static"/>
</div>

You are trying to set the value of an element with id 'hidden', but that's not the id of your hidden input. 您正在尝试设置ID为“ hidden”的元素的值,但这不是隐藏输入的ID。

The correct id is 'IndividualPageContent_hidden'. 正确的ID为“ IndividualPageContent_hidden”。

Set the value like this instead: 像这样设置值:

document.getElementById('IndividualPageContent_hidden').value = 'Your value here';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM