Link to home
Start Free TrialLog in
Avatar of pratikshahse
pratikshahse

asked on

Javascript Pop Up in ASP.NET

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.
Avatar of mohan_sekar
mohan_sekar
Flag of United States of America image

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
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
Avatar of pratikshahse
pratikshahse

ASKER

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.


"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
I want to submit a form.
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(); "


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.






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?
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.
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
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.
pratiksha: can you post your aspx code as an attachment?
I'm not sure what you mean by you cannot use <script> tag!!
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

ASKER CERTIFIED SOLUTION
Avatar of mohan_sekar
mohan_sekar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot for all your help and answers. Awesome replies.