Solved

Javascript Pop Up in ASP.NET

Posted on 2009-05-06
15
612 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now