在JS中设置值,但代码隐藏不会获得更新值。 ASP.Net

时间:2017-12-08 09:14:01

标签: javascript c# jquery asp.net

我在保存前使用bootbox提示进行验证,在回调函数中我设置了一个隐藏字段值,然后进入按钮点击事件。但是C#部分中的hiddenfield没有得到我在JS中设置的值。我该如何解决这个问题?

JS:

function notePrompt() {
    var protNumber = $("#hfProtNumberGen").val();
    var hfNote = document.getElementById("<%= hfNote.ClientID %>");
    var btnHidden = document.getElementById('btnHidden');
    if (protNumber != "") {
        bootbox.prompt({
            title: "Въведете причина за промяната. Повърдете запазването на информацията.",
            inputType: 'textarea',
            buttons: {
                confirm: {
                    label: "Запази"
                },
                cancel: {
                    label: "Откажи"
                }
            },
            callback: function (result) {
                if (result == null) {
                    hfNote.value = "";
                }
                else {
                    var MaxLenghtResult = result.slice(0, 200);
                    hfNote.value = MaxLenghtResult;
                    if (hfNote.value != "") {
                        setTimeout(function () { btnHidden.click(); }, 1000);
                    }
                }
            }
        });
    }
    else {
        setTimeout(function () { btnHidden.click(); }, 1000);
    }
}

C#:

string Note = hfNote.Value; //always gets ""

1 个答案:

答案 0 :(得分:1)

你必须这样做,意味着你必须控制runat =&#34; server&#34;并且在javascript中需要通过获得控制的clientid来控制价值

//axps file - this seems working for you
<asp:HiddenField ID = "hfName" runat = "server" />

//javascript --- you need to this change 
document.getElementById("<%=hfName.ClientID %>").value = MaxLenghtResult;

//in aspx.cs file 
string note = Request.Form[hfName.UniqueID];