Skip to content Skip to sidebar Skip to footer

Label Inside Updatepanel Will Not Show Value Assigned From Code-behind

I'm at my wits end here. I've defined a user control for a modal that lets users change a password. Inside the modal, the fields are contained in an UpdatePanel, and I'd like to di

Solution 1:

Just add a PostBackTrigger to your update panel:

<Triggers><asp:PostBackTriggerControlID="btnChange" /></Triggers>

OR

As a work around, instead of calling a server side click event you could use an AJAX call which posts to a C# [WebMethod].

1.Replace your showProcessingSpinner() function with this:

functionshowProcessingSpinner() {

    $("#dvChangeUserPasswordBtns").hide();
    $("#dvProcessing").show();
    //$("#<%= btnChange.ClientID %>").click();var id = getQueryStringParamByName("ID");
    alert("Query string parameter value for ID is - " + id);

    $.ajax({
        type: "POST",
        url: "ModalPopupAndUpdatePanel.aspx/ChangePasswordWeb",
        contentType: "application/json;charset=utf-8",
        data: '{id:"' + id + '"}',
        success: function (data) {
            debugger;
            alert('Success Message - ' + data.d.SuccessMessage);
            var successMessage = data.d.SuccessMessage;
            $("#lblChangePasswordSuccess").text(successMessage);
        },
        error: function (errordata) {
            alert('Error.AJAX call failed')
        }
    });
}


functiongetQueryStringParamByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = newRegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) returnnull;
    if (!results[2]) return'';
    returndecodeURIComponent(results[2].replace(/\+/g, " "));
}

2.Create a web method in the code behind called ChangePasswordWeb:

[System.Web.Services.WebMethod]
publicstatic AJAXResponse ChangePasswordWeb(string id)
{
    string retVal = ChangePassword(id);
    var response = new AJAXResponse();

    if (retVal == "Password changed.")
        response.SuccessMessage = retVal;
    else
        response.ErrorMessage = retVal;

    return response;
}

3. In the HTML - had to remove the OnClick event from btnChange and change the ScriptManager control to look like this - <asp:ScriptManager ID="ScriptManagerMain" runat="server" EnableCdn="true" />

Output:

Calling a WebMethod from jQuery using AJAX

Post a Comment for "Label Inside Updatepanel Will Not Show Value Assigned From Code-behind"