Useful Custom Controls for WebForms
I've been lucky enough to have quite a bit of exposure to Custom Controls of late and thought that I'd make a short note about the usefulness of them.
I had to use several pop-up windows to allow users to edit, search, etc and, on close of the pop-up I needed to be able to do a controlled postback of the opener so that I could reset the state of some UserControls on the page. Initially, I had been just using custom javascripts to send arguments back to the opener and re-painting some of the UI with client script. This method left me short because some of the re-painting actually required a call to the db to get the details of newly inserted records.
The DialogWindow allows me to close a child pop-up with a method that also allows me to send results to the opener like so:
' perform some db operations retVal = SqlHelper.ExecuteScalar(...) ' close the pop-up and send the database results to the opener window Me.Close( retVal.ToString )
The Close() method is appended to the Page by deriving from MetaBuilders.WebControls.DialogPage.
Back in the opener a postback is raised and the postback results can be retrieved in the DialogClosed event handler for the DialogWindow:
Private Sub MyDialog_DialogClosed(ByVal sender As Object, ByVal e As DialogResultEventArgs) Handles MyDialog.DialogClosed ' retreive the database results in the parent Dim r As String = e.Results End Sub
One thing that I did do differently to the documentation for that control is to call the Dialog.Open clientside function directly - as opposed to using a DialogOpenButton. I did this so that I could send QueryString args to the window that I'm opening:
function OpenPage() { var baseUrl = "http:\/\/localhost\/WebSite\/SubForms\/frmPopUp.aspx" var qString = "?mode=edit&tc=2&vc=LP02%20%20&mc=V2&oi=16441&os=8VA00%26Y%232&dt=TABLE_DD_SS" var opts = "width=630,height=550,resizable=yes,status=yes,scrollbars=yes" ; MetaBuilders_DialogWindow_OpenDialog( baseUrl + qString, 'MyDialog', opts ) }
2.) Denis Bauer's DynamicControlsPlaceholder Control
In the same app. (as mentioned above) I had to build an engine that would build dynamic forms based on definitions stored in various storage medium. The benefit in using this control is that it: "behaves like a placeholder but additionally handles recreating dynamic controls on subsequent requests". The control not only handles the re-creation of dynamically added controls on postback but also deals with the ViewState restoration for them as well.