Element-Enhancing Javascript in ASP.NET Core Countries

I had a little problem and worked a bit, but struggled to come up with a final answer / fix.

Basically, I have javascript (created by a third party) that does some whizzbang elements for page elements to make them look beautiful. This code works fine on separate pages (i.e. there is no wizard), however, when I try to apply effects to the content page inside the wizard, this will not work.

In short, I have a main page containing the main script link. All pages will use the script, but the parameters passed to it will be different for content pages.

Master page script link

<script src="scripts.js" language="javascript" type="text/javascript" />

Single page

<script>
    MakePretty("elementID");
</script>

As you can see, I need a link on every page (therefore, it is in the main page), but the actual elements that I want to "MakePretty" will vary depending on the content.

Content Pages

Now, due to the fact that the content page does not have an element <head>, I used the following code to add it to the master pages <head>:

HtmlGenericControl ctl = new HtmlGenericControl("script");
ctl.Attributes.Add("language", "javascript");
ctl.InnerHtml = @"MakePretty(""elementID"")";
Master.Page.Header.Controls.Add(ctl);

Now this one does not work . However, if I replace something simple, like alert("HI!"), everything works fine. Thus, the code is added OK, it just does not always execute, depending on what it does.

, , , content content Load , , , javascript / ?

, , - javascript, .

javascript, ?

/ , .

:

  • RegisterStartupScript .., , .
  • , MasterPage, , .

, - , , , , /.

+3
5

ContentPlaceHolder , asp: Content , , script . .

, , , -, . "elementID", , "ctl00_MainContentPlaceHolder_elementID". firebug , , .

+5

javascript? -)

- - body:

<script type="text/javascript">
  window.onload = function(){
    MakePretty("elementID");
  }
</script>

, script -tag :

<script type="text/javascript" src="myScript.js"></script>
+2

jQuery ? - :

$(document).ready(function(){
  $("input[type='text'], input[type='radio'], input[type='checkbox'], select, textarea").each(function(){
    MakePretty(this);
  });
});

, , , ( DOM ). jQuery , (.. , div).

MakePretty, , .

+1

-, , . Control.ClientID, script.

0

- html, .NET , .

, javascript CSS , -, , aspx. , javascript , :

  • (this.ID = "myPrefix";)
  • HTML ( : "myPrefix_myDiv" )
  • Any HTML element in your content owner id will have a prefix with an additional prefix (i.e. myPrefix_ContentPlaceHolderId1_myDiv)

Please let me know if I can clarify anything. Hope this helps!

0
source

Source: https://habr.com/ru/post/1697948/


All Articles