簡體   English   中英

在客戶端更改ASP.Net HiddenField值不更新

[英]ASP.Net HiddenField value not updating when changed on client-side

我有一個從代碼后面調用的JavaScript函數,它既具有輸入參數,又具有返回值。 我發現其他人也需要類似的功能,並且向他們提供了HiddenField作為可能的解決方案。 所以,這正是我想要做的-使用這樣的控件來模擬函數值返回行為。

問題在於,一旦客戶單擊按鈕,我得到的代碼隱藏的內容就是該特定字段的先前值。 第一次為null時,下一次為“ true”或“ false”(作為字符串),具體取決於客戶端選擇的內容。 好吧,我不能使用以前的值,它必須是“實時”的。

我聽說有人提到AJAX是客戶端和服務器之間進行通信的一種方式,但是我不知道如何實現該解決方案。

我將根據您的要求提供其他信息。

標記(部分):

<body id="body">
    <form id="mainForm" runat="server">
        <asp:ScriptManager runat="server" ID="scriptManager" EnablePageMethods="true">
            <Scripts>
                <asp:ScriptReference Path="/jquery-2.1.0.js" />
                <asp:ScriptReference Path="/Dialog.js" />
            </Scripts>
        </asp:ScriptManager>
        <asp:HiddenField id="hfData" ClientIDMode="Static" runat="server" />
    </form>
</body>

JavaScript:

function deleteDialog(contact) {
    var response = confirm('Are you sure you want to delete the contact ' + contact + '?');
    var hiddenField = $('#hfData')[0]; // fetches the element as expected
    hiddenField.value = response.toString();
    alert(hiddenField.value); // prints correct values
}

后台代碼:

Address addressDelete = addressService.fetchById(contactDelete.address_id);
string contactFullName = String.Join(" ", contactDelete.first_name, contactDelete.last_name);
Page.ClientScript.RegisterStartupScript(this.GetType(), "delete", "deleteDialog('" + contactFullName + "');", true);
Response.Write(hfData.Value); // I've also tried the Request.Form method but to no avail
contactService.delete(contactDelete);
addressService.delete(addressDelete);

嘗試這個

function deleteDialog(contact) {
var response = (confirm('Are you sure you want to delete the contact ' + contact + '?')) ? true : false;
var hiddenField = $("<%= hfData.ClientID %>").value; // fetches the element as expected
/*hiddenField.value = response.toString();*/
alert(hiddenField); // prints correct values
}

或檢查:

  alert(document.getElementById("<%=hfData.ClientID%>").value);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM