Javascript functions inside ASP.NET User Control

后端 未结 3 1174
逝去的感伤
逝去的感伤 2020-12-09 11:03

I created ASP.NET user control with javascript function :

<%@ Control Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"TestControl.ascx.cs\" Inherits         


        
3条回答
  •  眼角桃花
    2020-12-09 11:21

    You need to register your scripts with ClientScriptManager - this way they can be registered once, regardless of how often the control has been added to the page:

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;
    
    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 = "alert('Hello World');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    }
    

提交回复
热议问题