3

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 ""
4
  • is it paure asp.net i.e. not asp.net mvc , right ? Commented Dec 8, 2017 at 9:16
  • Please post the aspx page contents and the rendered HTML. My guess is that ASP.NET is changing the Id of the hidden field and you either need to write out the ID of the hidden field or change getElementById to get the element some other way. Commented Dec 8, 2017 at 9:17
  • can you try to read value like this var value = Request.Form[hfName.UniqueID]; as i updated in my answer Commented Dec 8, 2017 at 9:23
  • This is pure ASP.NET not mvc. And Okay @PranayRana I'm testing now. Commented Dec 8, 2017 at 9:28

1 Answer 1

2

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];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.