ASP:Hidden field variable from code behind loses value on return from server inside update panel

浪尽此生 提交于 2019-12-11 18:57:06

问题


I have a custom webpart that displays report data. It lives inside a tab control, and inside an update panel so the call back to refresh the report data is async.

On the server, I need to process some data and send back a value for later use. This variable just needs to SIT there and wait for user action, and then a client side javascript will use read the variable and based on the condition of the variable, this javascript will programmatically "click" the button in the update panel.

THE UPDATE PANEL AND REPORT VIEWER ON THE PAGE

<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnUnload="UpdatePanel_Unload">
<ContentTemplate>

<asp:Button ID="btnSendHiddenField" runat="server" style="visibility: hidden; display: none;"  OnClick="btnSendHiddenField_Click"/>

    <rsweb:ReportViewer ID="ReportViewer1" runat="server" 
        Font-Names="Verdana" Font-Size="8pt" Height="383px" 
        InteractiveDeviceInfos="(Collection)" ProcessingMode="Remote" 
        WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="757px" 
        SizeToReportContent="True">
    </rsweb:ReportViewer>

<asp:HiddenField  ID= "hiddenCurrentObjectId"  runat="server"/>

</ContentTemplate>
</asp:UpdatePanel>

THE SERVER CODE THAT PROCESSES THE DATA AND SENDS BACK THE VARIABLE. I HAVE OMITTED THE UNRELATED CODE.

Protected Sub btnSendHiddenField_Click(sender As Object, e As EventArgs) Handles btnSendHiddenField.Click
                Dim parsedObjectId As String = ""
                parsedObjectId = "1000"
                hiddenCurrentObjectId.Value = parsedObjectId
End Sub

In my OnUnload code for the panel. Which has to be there or there to make some other things work in the WebPart. I borrowed this OnUnload code to overcome a previous issue.

MY ONUNLOAD FUNCTION

Protected Sub UpdatePanel_Unload(sender As Object, e As EventArgs)
    Dim methodInfo As MethodInfo = GetType(ScriptManager).GetMethods(BindingFlags.NonPublic Or BindingFlags.Instance).Where(Function(i) i.Name.Equals("System.Web.UI.IScriptManagerInternal.RegisterUpdatePanel")).First()
    methodInfo.Invoke(ScriptManager.GetCurrent(Page), New Object() {TryCast(sender, UpdatePanel)})
End Sub
End Class

THE PROBLEM

The value from the hidden field gets wiped out on the return trip. I think it is getting wiped on the OnUnload function call.

MY QUESTION

What can I do to preserve this hidden field value so the client side javascript can use it when another user generated event occurs?


回答1:


If you are lossing the hidden field value then simplest way it to store it in a ViewState of any Session value.

You can get the value in js as:

function GetSessionValue()
{
   var sessionValue = "<%=Session["ITEM"].ToString()%>";
}


来源:https://stackoverflow.com/questions/20653178/asphidden-field-variable-from-code-behind-loses-value-on-return-from-server-ins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!