I've developed a custom control that extends .NET's CompositeControl class. This control overrides the CreateChildControls method to dynamically create its child controls. In order for the page to post back after specific javascript events on the client side, I have implemented a solution involving two hidden controls.
To achieve this functionality, I have included the following code snippet to create the hidden controls:
Protected Overrides Sub CreateChildControls()
hdEventName = New HiddenField()
Controls.Add(hdEventName)
hdEventName.ID = "hdEventName"
hdEventArgs = New HiddenField()
Controls.Add(hdEventArgs)
hdEventArgs.ID = "hdEventValue"
' other controls
' ...
End Sub
Whenever a javascript event is triggered, I set the values of these hidden controls and submit the page using the following script:
hdEventName.value = 'EventName';
hdEventArgs.value = 'arg1,arg2';
document.forms[0].submit();
In the OnLoad method of my control, I encountered an issue where the Value property of the hidden controls (hdEventName and hdEventArgs) appeared empty, even though Page.Request.Form(hdEventName.UniqueID) and Page.Request.Form(hdEventArgs.UniqueID) returned the expected values. The HTML markup also displayed the correct values post-back.
This discrepancy between the Value property of the HtmlInputHidden controls and the actual client-side value prompted me to investigate further. By moving the code that retrieves the hidden fields' values to either the OnPreRender method or implementing a dedicated Event_Handler method as below, I was able to resolve the issue:
Private Sub Event_Handler(ByVal sender As Object, ByVal e As EventArgs)
Handles hdEventName.ValueChanged
' do stuff with hiddens
' ...
' reset the values back
hdEventName.Value = String.Empty
hdEventArgs.Value = String.Empty
End Sub