[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using the ClientScript.RegisterStartupScript, is there a way to prompt a user for an answer like a vbyesno with a msgbox?

Posted on 2009-02-24
48
Medium Priority
?
2,871 Views
Last Modified: 2013-11-26
I discovered one problem with using the code shown below. Hopefully there's a way it can be adapted to allow for user input.

ClientScript.RegisterStartupScript(Me.GetType(), "alert", "<script>alert('" & message.Replace("'", "\'") & "');</script>")

I have one situation where I want to make sure the user really wants to perform a certain update. I had been doing that with msgbox using the vbyesno prompt.  Is there a way to prompt the user for input using this code?

thanks,

taduh
0
Comment
Question by:taduh
  • 23
  • 20
  • 5
48 Comments
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23723783
is this fired on a button press?
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23723821
if it is add this to the button html
OnClientClick="return confirm('Are you sure you want to do this?');"
or you can do it with the ajax modalpopupextender if you can use ajax
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/
0
 

Author Comment

by:taduh
ID: 23723833
yes
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:taduh
ID: 23724054
Its not quite that simple. I go into the code-behind and have to do some editing and other checking first.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23724318
then you can do it with the modal popup extender
Basically your modal popup would consist of a couple of control. a panel an update panel, your label for your message then a yes and no button.

on your Save button click or whatever click you want to show it. do the editing and checking
then you can call the modalpopup.Show();
which will popup the panel with the message and a yes no.  put code in for yes click or code for a no click



0
 

Author Comment

by:taduh
ID: 23724458
Can you tell me a little bit more about the panels and how they work withe modal popup extender? I'm assuming they're panels on your web page, but they must be hidden so you don't see them all the time?

How does the modalpopup.Show(); know which panel to use?

Sorry ...newbie here.
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23724701
All you need to do is add an OnClientClick to the update button.

OnClientClick="return confirm('Are you really sure you want to do this?')"
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23724762
on the modalpopupextender when you set it up you select which panel you want to use for that extender.
here is a small sample
<asp:Panel ID="pnlDataDefinitionPopup" runat="server"  Width="315px" style="left: -900px; position: absolute; top: -900px" >
    <asp:UpdatePanel ID="upDataDefinition" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
 
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Panel>
<!--This is where you set the modal extender -->
<ajax:modalpopupextender RepositionMode="RepositionOnWindowResizeAndScroll"  
        ID="mpeNewCopy" runat="server" PopupControlID="pnlDataDefinitionPopup"
        TargetControlID="btnCopyNewPopup" BackgroundCssClass="popupBG">
    </ajax:modalpopupextender>
<!--This is button for the target control.this button will probably never be used but is needed for the extender. just call the .Show() from the cs -->
    <asp:Button ID="btnCopyNewPopup" runat="server" Text="KEEP!!!!" style="visibility:hidden;" />

Open in new window

0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23724774
the modal popup extender will hide the panel automatically
the reason for this
style="left: -900px; position: absolute; top: -900px"
is so the panel does not appear on the screen before it is hidden
0
 

Author Comment

by:taduh
ID: 23725979
Okay,

I think this is gonna take a few posts to work through this. In my source view of Default.aspx, its acting like it doesn't recognize ajax - squiggly line under the ajax label. Was ajax something that needed to be enabled in the install or is there something I can do to enable it, now?
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23726035
you can go here and download it.
http://www.codeplex.com/AjaxControlToolkit/Release/ProjectReleases.aspx?ReleaseId=16488
then just add the dll as a reference to you project
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23726050
If you are using asp.net 2.0, you'll need to install the ajax stuff, if you are using 3.5 it comes built in, but you will still need to include the ajax toolkit dll.
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23726065
But as I said before, if you just want a box that pops up and asks to click Ok or Cancel before doing an update, all you have to do is add an OnClientClick="return confirm('Are you Sure?')" to your update button, which is much simpler and has less overhead than Ajax.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23726176
that what i recommended first.
i think this is a scenario something like.... pick something from the list to delete so pick it then you click the delete button. then in the cb  check if it has any associated items. if it does popup the confirmation if not just delete it.
0
 

Author Comment

by:taduh
ID: 23726552
Still can't get it to recognize Ajax even though I dropped the AJAX dll in the website's bin folder and did an Add Reference to that dll.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23726640
check for this on your page make sure its registered with the page
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
also change this
<asp:UpdatePanel ID="upDataDefinition" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
 
        </ContentTemplate>
    </asp:UpdatePanel>
to
<ajax:UpdatePanel ID="upDataDefinition" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
 
        </ContentTemplate>
    </ajax:UpdatePanel>

Open in new window

0
 

Author Comment

by:taduh
ID: 23726681
Made the switch.

Do not have <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %> anywhere on the page

Is that out on the page when it actually adds the refernce to the ajax dll?
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23726715
yeah put it under the main declaration ie this.  It tells the page that it can reference the controls
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MM.aspx.cs" Inherits="" Title="Analysis Summary" %> <---- my sample your will be a little different but it is the very first line then add this
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
0
 

Author Comment

by:taduh
ID: 23726887
Okay, that did the trick!

Now only problem is squiggly line under UpdatePanel. Tells me its not a known element. This can occur if there is a compilation error in the web site or the web.config file is missing. Web,config is definitely there. Is there something that needs to be in web.comfig for AJAX?
0
 

Author Comment

by:taduh
ID: 23727086
If I change the update panels back to

<asp:UpdatePanel

the squiggly lines go away.

Now would I put a Yes and a No button inside the Content template of the Update Panel?
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23727257
yeah, didnt know if they where always <asp: or <ajax: im using 3.5 framework.
no just make the your message box in the content template.  if you change the message or anything from the codebehind.  before you call the .Show() call
upDataDefinition.Update()
0
 

Author Comment

by:taduh
ID: 23733613
Okay, were making progress here, but I'm still kind of confused. Correct me where I'm wrong

1. In the Content Template , I need to put one "Yes" button and one "No" button.
2. Then I put a message box ( a label ) in the Content template asking the user the question I want the response for.
3. If I change that message from the code behind , I'll need to call upDataDefinition.Update()
4. I'm supposing if I do #3, then I'll need to put a subroutine in my code behind called upDataDefinition.Update() where I'll need to place the code that's going to update my message.
5. Then I call modalpopup.Show(); , then I have code in my code behind for both Onclick.Yes event and the Onclick.No event.

Is this even remotely close?

taduh
 
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23734194
yeah. so your sample update panel would look something like this
<asp:Panel ID="pnlMessageBox" runat="server">
        <asp:UpdatePanel ID="upMessageBox" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <table>
                    <tr>
                        <td>
                            <asp:Label ID="lblMessage" runat="server" Text="Label" />
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align:center;">
                            <asp:Button ID="btnYes" runat="server" Text="Button" />
                            <asp:Button ID="btnNo" runat="server" Text="Button" />
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Panel>

Open in new window

0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23734273
then you would do your stuff in the code behind. this is c# but gives you the idea
<!--This is the modal Popup Extender-->
 <ajax:modalpopupextender RepositionMode="RepositionOnWindowResizeAndScroll"  
        ID="mpeUpdate" runat="server" PopupControlID="pnlMessageBox"
        TargetControlID="btnUpdatePopup" BackgroundCssClass="popupBG">
    </ajax:modalpopupextender>
    <asp:Button ID="btnUpdatePopup" runat="server" Text="KEEP!!!!" style="visibility:hidden;" />
 
 
protected void btnUpdate_Click(object sender, EventArgs e)
{
    //Change the message text
    lblMessage.Text="Are you sure you want to update"?
    //Update the content template
    upMessageBox.Update();
    //Show the popup
   mpeUpdate.Show();
}

Open in new window

0
 

Author Comment

by:taduh
ID: 23734352
Okay,

I tried everything I listed in the previous post. When I went to bring up the website, i got the following message:

The control with ID 'upDataDefinition' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23734396
i was gonna post that next that you have to add one to the page put this above the update panel
<asp:ScriptManager id="ScriptManager1" runat="server">
</asp:ScriptManager>

Open in new window

0
 

Author Comment

by:taduh
ID: 23734817
I've got everything setup the way you have it. When I tested this time, it acted like it went ahead with my normal logic, then popped up the the label and Yes and No buttons, but they weren't in any kind of box, they were just hovering transparently over my webpage.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23734895
you have to design what the box looks. like add this style.. it will darken the screen around the box
.popupBG
{
background-color:Gray;
filter:alpha(opacity=70);      
opacity:0.7;
}
this will give you a white background with a border
.popupPanel
{
background-color:White;
border:solid 1px black;
}

then add this to the modal popupextender
BackgroundCssClass="popupBG"

then change your panel to this
<asp:Panel ID="pnlMessageBox" runat="server">
        <asp:UpdatePanel ID="upMessageBox" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
<asp:Panel ID="pnlInnerBox" runat="server" CssClass="popupPanel">
                <table>
                    <tr>
                        <td>
                            <asp:Label ID="lblMessage" runat="server" Text="Label" />
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align:center;">
                            <asp:Button ID="btnYes" runat="server" Text="Button" />
                            <asp:Button ID="btnNo" runat="server" Text="Button" />
                        </td>
                    </tr>
                </table>
 </asp:Panel>
 
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Panel>

Open in new window

0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23734984
it will look like something like this
popup.png
0
 

Author Comment

by:taduh
ID: 23735152
I've attached a code snippet of my aspx source and also a bitmap of what my results are.
 <asp:Panel ID="pnlDataDefPopUp2" runat="server"  Width="315px" style="left: -900px; position: absolute; top: -900px" >
    <asp:UpdatePanel ID="updatedef2" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
         <asp:Panel ID="pnlInnerMsgBox2" runat="server" CssClass="popupPanel">
            <table>
                <tr>
                    <asp:Label ID="LblChgMasterX" runat="server" Text="You are about to modify information for an existing bank exclusion record on the master file. Is this what you really want to do?" CssClass="labelstyle"></asp:Label>
                </tr>
                <tr> 
                    <td style="text-align:center;">             
                        <asp:Button ID="btnchgMBXYes" runat="server" Text="Yes" />
                        <asp:Button ID="btnchgMBXNo" runat="server" Text="No" />
                    </td>
                </tr>          
            </table>
          </asp:Panel>   
        </ContentTemplate>
    </asp:UpdatePanel>  
</asp:Panel>
 
<!--This is where you set the modal extender -->
<ajax:modalpopupextender RepositionMode="RepositionOnWindowResizeAndScroll"
        ID="mpeChgMasterX" runat="server" PopupControlID="pnlDataDefPopup2"
        TargetControlID="btnCopyPopup2" BackgroundCssClass="popupBG">
    </ajax:modalpopupextender>
<!--This is button for the target control.this button will probably never be used but is needed for the extender. just call the .Show() from the cs -->
    <asp:Button ID="btnCopyPopup2" runat="server" Text="KEEP!!!!" style="visibility:hidden;" />

Open in new window

Snap2.bmp
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23735275
for the white background did you add this style to the page
    .popupPanel
{
      background-color:White;
      border:solid 1px black;
}
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23735347
then change the table tag to
<table width="310px">
not sure why it is not sizing right without that
0
 

Author Comment

by:taduh
ID: 23735608
The attached bitmap shows what it looks like now. Better, but not quite there.

Here's my aspx source for the popupBG and popupPanel

}
        .popupBG
        {
        background-color:Gray;
        filter:alpha(opacity=70);      
        opacity:0.7;
        }
        this will give you a white background with a border
        .popupPanel
        {
        background-color:White;
        border:solid 1px black;
        }
         
        </style>

Still, the code is not stopping at the popup and waiting for it. Its running on through like its assuming that a "No" was selected and then putting up the popup. I've attached a code snippet where I call the show popup.
 If MBXExists = True Then
            If MBXMatches = False Then 'Ensure that the intent was really to change the master record...
                txtboxchgmasterYN.Text = "N"
                Call mpeChgMasterX.Show()
                If txtboxchgmasterYN.Text = "Y" Then      '...Update the master record, if so ....
                    Call Update_Master_Bank_Exclusion(RockOn)
                    If RockOn = False Then
                        GoTo OuttaCheck4ExistingMBX
                    End If
                Else         '... otherwise assure the user that the update was aborted.
                    message = "Update on Master File of existing bank exclusion record was aborted. Exclusion will still be added to Order Exclusion table if not already present."
                    msguser(message)
                    RockOn = True
                    GoTo OuttaCheck4ExistingMBX
                End If  'If the entry matches the master rec, set the Master Bank Exclusion DDL to it. 
            ElseIf dropdownExclBanksUniv.SelectedValue <> txtboxExclTRN.Text Then
                Call Set_MBX_DDL_Current()
            End If
        ElseIf MBXExists = False Then ' If we have a new exclusion, insert it into the master file.
            Dim InsertOK As Boolean
            Call Insert_Master_Bank_Exclusion(InsertOK)
            If InsertOK = False Then
                RockOn = False
            End If
        End If

Open in new window

Snap2.bmp
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23735676
you need to have all of the code you want to run before the popup in that method, and then call the .Show().  Then you have to put the code you want to run if yes. in the yes button click.  The code you want to run if its no in the no button click.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23735690
but the modal popup doesnt stop code execution
0
 

Author Comment

by:taduh
ID: 23735832
When you're saying "you need to have all of the code you want to run before the popup in that method"
is method equivalent to subroutine?
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23735903
As mentioned about 30 posts ago, if you want to stop the update execute, add an OnClientClick to the update button and it will prevent execution if they click Cancel on the confirm dialog.

<asp:Button runat="server" ID="Button1" OnClientClick="return confirm('Are you really sure you want to do this?')"/>
0
 

Author Comment

by:taduh
ID: 23735927
If the popup doesn't stop code execution, then the code's going to keep running and the onclick yes or no will always be the last thing to execute, right?

With a normal msgbox with vbyesno, the code stopped execution and the code proceeded based on the user response.

I don't see how this is going to work.
0
 

Author Comment

by:taduh
ID: 23736020
aibusinesssolutions:

Here's my dilemma. I need the user's response, but the point I need it at  is deep within a subroutine, which has been called from another subroutine. So even if the code execution stops with the OnClientClick, the code gets redirected to the OnClientClick code and I've popped out of the code stream I was executing.

With a normal msgbox with vbyesno, the code stopped execution and the code proceeded based on the user response, it didn't throw you into an OnClientClick subroutine.

Surely there's some way to simulate that, isn't there?
0
 

Author Comment

by:taduh
ID: 23736150
The other reason I've been resisting using the OnClientClick added to my update button is that its an Add/Update button. I have to first determine if its an Add or an Update. I only want to confirm if they're changing something on an existing master record.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23736174
method is equivalent to sub
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23736191
so basically run all of your code in the first subroutine until you need a yes or no response.  Then take all of your code you would run if the answer was yes. to the yes button click. and all of the code you would run if it is no to the no button click
0
 

Author Comment

by:taduh
ID: 23736315
I suppose I could do that, but it would be cumbersome. I can't believe there's not some way to stop execution and get a user response without getting thrown out of the codestream you're in.
0
 
LVL 16

Assisted Solution

by:burakiewicz
burakiewicz earned 1600 total points
ID: 23736336
there is no way that i know of to stop execution and wait for a response like in a windows forms app
0
 

Author Comment

by:taduh
ID: 23736663
I appreciate all the help and especially your time, burakiewicz. I want to give you some points without accepting your solution. Do you know of a way to do that?
0
 
LVL 15

Assisted Solution

by:aibusinesssolutions
aibusinesssolutions earned 400 total points
ID: 23736735
You have to realize that web development is building apps for a stateless environment, you can't stop code execution in the middle of a sub routine to get user input, the response buffer has to dump everything to the user before it can accept any new data from the user. It's like two one way streets, a response and a request, and only one can happen at a time.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 23737055
not sure ask the zone advisor, but not sure how to do that
0
 

Accepted Solution

by:
taduh earned 0 total points
ID: 23784157
In order to get around this problem, I did the following:

I messaged the user and asked them to confirm they wanted to make the change by clicking a check box which was hidden on the page, but which I made visible just prior to messaging them. Then I altered my code in the code-behind to reference the checkbox and make the change or abort the change accordingly.

See code snippet attached.



 'If customer already exists on masterfile, confirm desire to update.
        If CustExists = True Then
            If chkboxUpdCust.Visible = True Then    'If Update confirmation checkbox is visible and true 
                If chkboxUpdCust.Checked = True Then 'update the record, then re-hide the checkbox and 
                    Call Update_Customer_Data()       'accompanying label.  
                    Call Reinit_CustChange_Confirm()
                Else   'If chkbox is not checked, hide chkbox & label, abort update & proceed to process order.                                                      
                    message = "Customer Update aborted - Refreshing Customer data, then submitting rest of order."
                    msguser(message)
                    Call Repopulate_Customer1_Data()
                    Call Reinit_CustChange_Confirm()
                    GoTo ProcessOrder
                End If
            Else       'First time through for this order - check for changes to existing customer. 
                Dim CustChanges As Boolean = False
                Call Check_Customer_Changes(CustChanges)
                If CustChanges = True Then  'If changes are found,unhide confirmation chkbox and accompanying
                    Call Setup_CustChange_Confirmation() 'label, ask user if they really want to update,
                    message = "You are about to update an existing customer record; To proceed, check the confirmation checkbox and re-submit the order; Otherwise simply re-submit the order w/o checking the box."
                    msguser(message)                      'and jump out of process.
                    GoTo Outtahere
                End If
            End If
        Else
            Call Insert_New_Customer()
        End If
 
    Sub Setup_CustChange_Confirmation()
 
        chkboxUpdCust.Visible = True
        LblUpdCust.Visible = True
 
 
    End Sub
    Sub Reinit_CustChange_Confirm()
 
        chkboxUpdCust.Visible = False
        LblUpdCust.Visible = False
 
 
    End Sub

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question