Alexander Hristov
Alexander Hristov

Reputation: 311

Setting value in JS, but codebehind doesn't get updated value. ASP.Net

I'm using bootbox prompt to do validation before saving, in the callback function I'm setting a hiddenfield value and then going into a button click event. But hiddenfield in the C# part doesn't get the value I've set in the JS. How should I fix this?

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 ""

Upvotes: 3

Views: 981

Answers (1)

Pranay Rana
Pranay Rana

Reputation: 176896

you have to do like this , means you have to make control runat ="server" and in javascript need to udpate value in control by getting clientid of control

//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];

Upvotes: 2

Related Questions