Abid Ali
Abid Ali

Reputation: 1747

javascript confirm message box in usercontrol

I have a usercontrol in which i have placed a javascript function which shows the confirm message box. That usercontrol is further called on aspx page.

the function i wrote is :

<script>    function CreatePopup() {
    return confirm("Do you confirm?");
}
</script>

and then i called the function on the button :

 <asp:Button ID="btnAuthenticate" runat="server" Text="Authenticate" 
          OnClientClick="return CreatePopup();"
          OnClick="btnAuthenticate_Click" />

What happens is this code is not working.. is there any problem with my function here or do we have certain limitations to look after when adding any scripts in usercontrol? This is blowing me off as this was supposed to be the most simpliest task i thought to be!!!

Upvotes: 0

Views: 1467

Answers (3)

Pabitra Dash
Pabitra Dash

Reputation: 1513

The following worked for me to display a alert box from user control (ascx).

ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('I am from the Server');", true);

Upvotes: 0

Johann Combrink
Johann Combrink

Reputation: 703

I Use Javascript with a page method

<script>
function Delete(id) {
            if (confirm("Delete Record?")) 
            {
                PageMethods.DeleteSomething(id, onsuccessDel, onfailDel);
            }
        }

        function onsuccessDel(msg) { window.alert('Did IT!'); }
        function onfailDel(err) {window.alert('Some Error Occured!'); }
</script>

And then the HTML or ASPX should have :

<asp:ScriptManager ID="scriptMgr" runat="server" ViewStateMode="Disabled" EnablePageMethods="true" />

 <a href="javascript:void(0)" onclick="Delete(1)" title="Delete">Delete</a>

The Server side code :

 [WebMethod]
        public static void DeleteSomething(int id)
        {
          try{
                 //some code c#
             }
            catch (Exception ex)
            {
               throw new Exception(ex.toString());
            }

        }

Upvotes: 1

Code Lღver
Code Lღver

Reputation: 15603

You can see the working demo there: Demo.

And the code from here: code.

Hope this will be helpful to you.

Upvotes: 0

Related Questions