简体   繁体   English

用户控件中的javascript确认消息框

[英]javascript confirm message box in usercontrol

I have a usercontrol in which i have placed a javascript function which shows the confirm message box. 我有一个用户控件,其中放置了一个显示确认消息框的javascript函数。 That usercontrol is further called on aspx page. 该用户控件在aspx页面上进一步被调用。

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!!! 这让我大吃一惊,因为这被认为是我认为最简单的任务!

I Use Javascript with a page method 我将Javascript与page方法一起使用

<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 : 然后HTML或ASPX应该具有:

<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());
            }

        }

You can see the working demo there: Demo . 您可以在此处看到有效的演示: Demo

And the code from here: code . 还有来自这里的代码: code

Hope this will be helpful to you. 希望这对您有帮助。

以下为我工作,以显示来自用户控件(ascx)的警报框。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM