I'm trying to use WDW as a replacement for the Windows Forms Msgbox. I've seen the great videos on WDB from Craig Shoemaker, but his examples are fairly simple, and the WDW display is the last thing done before the end of the routine. In real life you may want to ask a question via a WDW and continue processing based on the answer given in the WDW. Basically I want to use WDW to somehow accomplish what msgbox does in the following simple example.
By using WDW and setting some Session variables and then having a button in the WDW execute the MySub routine all over again from the beginning, I've gotten pretty close to doing what I want. However, it is VERY cumbersome and certainly not what I consider clean code. It's just a kludge.
A few questions about WDW:
1. it appears to be asynchronous. That is, the server code continues to run after the call to display the WDW, so I can't really code in a "test" for a value that is being set in the WDW control. For example, if I set the session variable "DeleteIt" via clicking a button in the WDW I can't do something like this:
because it seems like the "If session" line is being executed immediately after the WDW display and before the user exits the WDW., even if the WDW is set to modal.
So to get around that I'm setting and testing a bunch of flags to basically get me back to the line of code "If session..." when I re-execute the Sub via a button click in the WDW.
So, my question to all you WDW experts is how to do this a better way. What is the best way to continue executing the Sub from the line of code immediately following the WDW display but after the WDW closes. If this can somehow be easily done it would then be easy to simulate the MsgBox functionality.
Hope this is clear. Thanks in advance for any advice.
Hello Ron,As far as I understand your goals this could be done by Opening the window as a modal and handling the buttons events by a different way. Please take a look at the attached from me sample which was tested via Chrome, FF 11 and IE 9. Sample has one button which when pressed pops up the WDW as a modal. In the window you have choice to delete or to add node in WebDataTree. I can offer you another approach by handling ordinary javascript confirm dialog and making a postback after ok is chosen.
Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4
It seems the problem of duplicating the Windows Forms Msgbox functionality with the WebDialogWindow is that, even if the WDW is modal, the code behind continues execution.
This means that the display of the WDW should be the last thing you do before exiting the sub, and then whatever happens (say, click a button) in the WDW should call another sub to continue whatever processing is needed.
Although the above is perfectly fine, I prefer to see my logic in one sub, so I put together a method of how to more closely mimic the MsgBox functionality.
1. you will need 2 variables stored in your session. PopUpResponse, PopUpNextLineToExecute
2. Create a WDW (and make it 'Visible' so you can work with it in the designer) that contains a Label (the label will contain your specific message), and all the buttons you might want to click for any situation. These buttons are predefined so that they are there. Before calling the WDW you will change the visibility of the appropriate buttons so only the ones that apply will be visible in the popup.
3. Create a 'click' event in the code behind for each of the buttons in the WDW. In the button_click event you will set the session variable to the appropriate response, hide the WDW and then re-execute your SUB. Sample code:
Protected Sub btnPopUpNo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPopUpNo.Click
CType(Session("myuserstate"), UserState).PopUpResponse = "NO"
WebDialogWindow1.WindowState = Infragistics.Web.UI.LayoutControls.DialogWindowState.Hidden
Dim ee As Infragistics.WebUI.WebDataInput.ButtonEventArgs
ee = Nothing
btnSaveChanges_Click(Nothing, ee)
End Sub
3. After building the WDW in the designer, set the visibility of the WDW to 'hidden' so you won't see it until it is called.
4. in your code behind file you need to set the session variable PopUpNextLineToExecute with a value so you know where to return to when you re-enter the SUB. Sample code:
If some-terrible-conditon-exists Then
'set the text for the client msgbox popup
lblpopup.Text = "The UserID is a Primary Key. That means it is the main identifier for this record. <br \> Do you really want to change it from '" & OldUserID.Value.Trim & "' to '" & tbUserID.Text.Trim & "'"
'set the buttons you want to see in the client popup
btnPopUpNo.Visible = True
btnPopUpYes.Visible = True
btnPopUpOK.Visible = False
WebDialogWindow1.WindowState = Infragistics.Web.UI.LayoutControls.DialogWindowState.Normal
CType(Session("myuserstate"), UserState).PopUpNextLineToExecute = "PopUp2"
Exit Sub
End If
ContinueFromPopup2:
CType(Session("myuserstate"), UserState).PopUpNextLineToExecute = ""
If CType(Session("myuserstate"), UserState).PopUpResponse.Trim.ToUpper = "NO" Then
tbUserID.Text = OldUserID.Value
GoTo ExitSub
ElseIf CType(Session("myuserstate"), UserState).PopUpResponse.Trim.ToUpper = "YES" Then
ChangingPrimaryKey = True
lblpopup.Text = ""
CType(Session("myuserstate"), UserState).PopUpResponse = ""
Notice in the code that just after displaying the WDW I set my session variable so I know where to return, and then I exit the SUB. Immediately following I have a line label for where I'm going to continue. I then check my PopUpResponse session variable and perform the appropriate actions. I also cleanup and reset my session variables and flags.
4. In the beginning of the SUB I check to see if this is a re-execution of the SUB from a WDW call. Sample Code:
If CType(Session("myuserstate"), UserState).PopUpNextLineToExecute = "PopUp1" Then GoTo ContinueFromPopup1
If CType(Session("myuserstate"), UserState).PopUpNextLineToExecute = "PopUp2" Then GoTo ContinueFromPopup2
That's it. It may appear a bit convoluted, but it really is kind of straight forward if you give it some thought. If you decide to use this technique, you can clearly customize/expand it to meet your specific requirements.
The bottom line is that, to the end user, it looks and behaves just like the MsgBox function, and that's just what I wanted to accomplish. Based on the number of questions in various forums pertaining to a MsgBox function in ASP.Net, it seems like a lot of people might benefit from this technique.
Hello Ron,Thank you for sharing your experience with the whole community. Please let me know if you afe any further questions regarding the issue.