Javascript functions inside ASP.NET User Control

后端 未结 3 1163
逝去的感伤
逝去的感伤 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:15

    You need to use this.id

    $(document).ready(function () {
        load_v<%= this.ID %>    
    });
    
    function load_v<%= this.ID %>(fromclick) {
        alert('anything');
    }
    

    So that even if you need two or more same controls in the same page they will have different ids. Hope this Helps! cheers :)

    0 讨论(0)
  • 2020-12-09 11:18

    I found a solution in another site which allows you to use external file

    if (!Page.ClientScript.IsClientScriptIncludeRegistered("key"))
    
    {
    
       string url = ResolveClientUrl("~/Scripts/file.js");
    
       Page.ClientScript.RegisterClientScriptInclude("key", url);
    
    }
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题