Solved

Javascript Pop Up in ASP.NET

Posted on 2009-05-06
15
653 Views
Last Modified: 2012-05-06
I want to a confirm popup box using javascript in my asp.net page using Page.RegisterClientScriptBlock (or similiar) . Here is how I want it to work.

when the user click "submit" button,  it should check that selectedindex on one of the dropdown is > 0. If so show a confirm pop up. If Ok is pressed continue with the next step and if cancel is pressed return. It would be nice if the javascript checks for the selectedindex of drop down rather then doing it via the code. I want all this done in the code behind and not on the html side of the form.
Thanks in advance.
0
Comment
Question by:pratikshahse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
15 Comments
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24320447
In button_click event, write the following (note: the code is in VB.Net)

if dropdown1.selectedIndex <> -1 then
         dim js as String  = " function test1() { if (confirm("Are you sure you want to continue")) alert('you clicked ok'); else alert('you clicked no'); } test1(); "
       Web.UI.ClientScriptManager.RegisterClientScriptBlock(Me.GetType(),"test",js, True)
end if


NOTE: I show an alert box when the users clicks on Yes or No. You change it according to your needs
0
 
LVL 6

Expert Comment

by:M3mph15
ID: 24322394
Hi,

I have foudn it easier to put a <asp:Litertal> control in the <head> section of a page and then just write the Literal's text as the javascript function

HTH
-M3mph15
0
 

Author Comment

by:pratikshahse
ID: 24327316
Mohan,

Your reply is what I was looking for. I have one question though.
you have alert boxes when the user clicks yes or no. Instead I want to continue with the application when user clicks ok and return when user clicks cancel. Is that something we can do from the script itself or is there a way where i can get the value of the button pressed in the code and depending upon what value i get i decide what to do.

Also I am trying to do line breaks in my message by using \r\n and it does not allow me to do that . infact the script fails and I do not see the pop up box. It would be great if you can let me know how to fix that too.


0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24327826
"Instead I want to continue with the application when user clicks ok"  -> What exactly do you want to do? Do you want to navigate to another page? Call a function? Submit the form?

As far as \r\n is concerned, escape them like this \\r\\n
0
 

Author Comment

by:pratikshahse
ID: 24327836
I want to submit a form.
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24328017
dim js as String  = " function test1() { if (confirm("Are you sure you want to continue")) document.forms[0].submit(); else alert('you clicked no'); } test1(); "


0
 

Author Comment

by:pratikshahse
ID: 24328313
Mohan,

One last thing. Actually this is what I want to do.

If 'ok' is pressed, i want to continue with the next line of code which is

 if (ddlFacility.SelectedIndex != 0)
      {

                    string js = function test1() { if (confirm('Are you sure you want to continue')) alert('you clicked ok'); else alert('you clicked no'); } test1(); "
                   ClientScript.RegisterClientScriptBlock(typeof(Page),"test",js,true);
      }

next line of code is
facility = ddlFacility.SelectedValue;
opportunity = ddlOpportunity.SelectedValue;
cycle = ddlCycle.SelectedValue;
then it goees ahead and updates the database and all the other stuff. this all should be done if 'ok' is pressed.

if 'cancel' is pressed, just exit out of that function and take back to the page when the pop up was initiated.


Sorry for asking too many questions but I am pretty new with javascripts and have never worked with them before.






0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24328488
simply solution is to move the script to you aspx page. Any reason why you do not want to have the script on the client side?
0
 

Author Comment

by:pratikshahse
ID: 24328538
i cannot have it on my aspx page is becuase my aspx is content page and it does not allow me to have script on a content page.

how do i call a function from the script?

what i can do is that if 'cancel' is clicked i will call a function which will eventually do a 'return'

thanks for all your replies.
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24328672
you CAN add scripts on a content page!
Add <script></script> tags to your page and move the function from code behind to the aspx.

Here is what you need to do

say, I've a link button and on click of that I want to show a confirm dialog

<asp:LinkButtonid="lnkSubmit" runat="Server" Text="Submit" OnClientClick = "return askConfirmation();" ></asp:LinkButton>

<script type="text/javascript" language="javascript">
function askConfirmation()
{
   if (confirm("Are you sure"))
      return true;
   else
        return false;
}
</script>

If 'ok' is clicked, execution will go to you button_click event on the server side.

It's that simple
0
 

Author Comment

by:pratikshahse
ID: 24329002
can you tell me how to call a function from the javascript. so instead of doing a submit on clicking ok how can i call a function when ok clicked.

for some reason i cannot use javascript by using <script> tag.
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24329065
pratiksha: can you post your aspx code as an attachment?
I'm not sure what you mean by you cannot use <script> tag!!
0
 

Author Comment

by:pratikshahse
ID: 24329371
here you go
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="SCPC.SharePoint.Features.OAI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=83c59cc2ff2b511c" %>
 
<%@ Page MasterPageFile="~/_layouts/application.master" Language="C#" Inherits="SCPC.SharePoint.Features.OAI.SubmitToARC" %>
<asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server">
 
 
    <asp:Label ID="lblError" runat="server" ForeColor="Red" />
    <asp:Label ID="lblMessage" runat="server" />
    <table>        
        <tr>
            <td colspan="2">&nbsp;
            </td>
        </tr>       
        <tr>
            <td>
                <asp:Label ID="lblFacility" Text="Facility:" runat="server" />
            </td>
            <td>
                <asp:DropDownList ID="ddlFacility" runat="server" >                    
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblOpportunity" Text="Opportunity:" runat="server" />
            </td>
            <td>
                <asp:DropDownList ID="ddlOpportunity" runat="server" >
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblCycle" Text="Cycle:" runat="server" />
            </td>
            <td>
                <asp:DropDownList ID="ddlCycle" runat="server" >
                </asp:DropDownList>
            </td>
           
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>            
        </tr>
        <tr>
            <td colspan="2"> 
                <asp:HiddenField ID="previousPage" runat="server" />
                <asp:HiddenField ID="hdnfld" runat="server" />
                <asp:Button ID="btnSubmit" runat="server" Text="Ready for ARC" OnClick="btnSubmit_Click" />&nbsp;&nbsp;
                <asp:Button ID="btnCancel" runat="server" Text="Home" OnClick="btnCancel_Click"/>                
            </td>         
        </tr>
    </table>
</asp:Content>
 
<asp:Content ID="PageTitle" runat="server"
             contentplaceholderid="PlaceHolderPageTitle" >
	Submit Application to the ARC
</asp:Content>
 
<asp:Content ID="PageTitleInTitleArea" runat="server"
             contentplaceholderid="PlaceHolderPageTitleInTitleArea" >
	Submit Application to the ARC
</asp:Content>

Open in new window

0
 
LVL 15

Accepted Solution

by:
mohan_sekar earned 500 total points
ID: 24329485
check it out

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="SCPC.SharePoint.Features.OAI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=83c59cc2ff2b511c" %>
 
<%@ Page MasterPageFile="~/_layouts/application.master" Language="C#" Inherits="SCPC.SharePoint.Features.OAI.SubmitToARC" %>
<asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server">
       <script type="text/javascript">
             function askConfirmation()
            {
               if (confirm("Are you sure"))
                  return true;
               else
                    return false;
            }

       </script>
 
    <asp:Label ID="lblError" runat="server" ForeColor="Red" />
    <asp:Label ID="lblMessage" runat="server" />
    <table>        
        <tr>
            <td colspan="2">&nbsp;
            </td>
        </tr>      
        <tr>
            <td>
                <asp:Label ID="lblFacility" Text="Facility:" runat="server" />
            </td>
            <td>
                <asp:DropDownList ID="ddlFacility" runat="server" >                   
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblOpportunity" Text="Opportunity:" runat="server" />
            </td>
            <td>
                <asp:DropDownList ID="ddlOpportunity" runat="server" >
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblCycle" Text="Cycle:" runat="server" />
            </td>
            <td>
                <asp:DropDownList ID="ddlCycle" runat="server" >
                </asp:DropDownList>
            </td>
           
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>            
        </tr>
        <tr>
            <td colspan="2">
                <asp:HiddenField ID="previousPage" runat="server" />
                <asp:HiddenField ID="hdnfld" runat="server" />
                <asp:Button ID="btnSubmit" runat="server" Text="Ready for ARC" OnClientClick="return askConfirmation();" OnClick="btnSubmit_Click" />&nbsp;&nbsp;
                <asp:Button ID="btnCancel" runat="server" Text="Home" OnClick="btnCancel_Click"/>                
            </td>        
        </tr>
    </table>
</asp:Content>
 
<asp:Content ID="PageTitle" runat="server"
             contentplaceholderid="PlaceHolderPageTitle" >
      Submit Application to the ARC
</asp:Content>
 
<asp:Content ID="PageTitleInTitleArea" runat="server"
             contentplaceholderid="PlaceHolderPageTitleInTitleArea" >
      Submit Application to the ARC
</asp:Content>
0
 

Author Closing Comment

by:pratikshahse
ID: 31578742
Thanks a lot for all your help and answers. Awesome replies.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

632 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