Solved

Javascript Pop Up in ASP.NET

Posted on 2009-05-06
15
638 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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