在我网页的contentPlaceHolder中包含我的JavaScript代码 [英] include my javascript code in the contentPlaceHolder of my webpage

查看:67
本文介绍了在我网页的contentPlaceHolder中包含我的JavaScript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站有一个母版页,并且在创建新页时需要母版页设置-没关系-但问题是我想在此页中包含javascript代码,而我无法添加它,因为我只能通过可以在此页面上编辑的contentplaceholder来编辑正文,并且在contentplaceholder中我们不能添加javascript代码,因为< script>标签应该在正文之前,现在我也尝试将其包含在正文之前的母版页中,但是它给了我错误,因为在函数中,我使用了一些处理该页面按钮的代码,并且未包含在该代码中母版页...没有人有解决此问题的方法..

my website has a master page, and when creating a new page, it takes the master page setting - and that is OK - but the problem is that i want to include a javascript code to this page and i cant add it because i can edit the body only through contentplaceholder that can be edited on this page, and in the contentplaceholder we cannot add javascript code, because the <script> tag should be before the body, now i tried also to include it in the master page before the body but it gave me errors because in the function i used some code that deals with the buttons of that page and it''s not included in the master page... does anyone has a solution for this problem..

推荐答案

例如,如果您在母版页中添加脚本

For example if you add a script in the master page

<script type="text/javascript">
    function test() {
        alert("It Works");
    }
</script>



然后可以通过其他方式将按钮分配给其他页面中的按钮



Then can assign to a button in other pages like these ways

<asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="test();"/>


或来自
后面的代码


OR from code behind

Button1.Attributes.Add("onclick", "test();");




您可以在 .Master 页面中添加Javascript,例如:
Hi,

You can include Javascript in .Master page like:
<head id="Head1" runat="server">
    <title>My Master Page</title>
   
    <script language="JavaScript" type="text/javascript">
function jsfunction()
{
}
</script>
</head>



或在 Content .aspx 页中进行定义,例如:



or define in Content .aspx page like:

<asp:UpdatePanel ID="GridContainer" runat="server">
        <ContentTemplate>
        </ContentTemplate>
        <Triggers>
        </Triggers>
    </asp:UpdatePanel>
    <script language="JavaScript" type="text/JavaScript">
function myjsfunction()
{
}
     </script>
</asp:Content>



谢谢,
Imdadhusen



Thanks,
Imdadhusen


尝试使用页面. RegisterClientScriptBlock方法 [ ^ ]或 Page.RegisterStartupScript方法 [^ ]

使用它们,您可以根据特定条件(特定于页面)从服务器端将脚本注入到页面中.

有关如何使用它的示例:如何使用RegisterClientScriptBlock& RegisterStartupScript [^ ]
示例2 [
Try using Page.RegisterClientScriptBlock Method [^] OR Page.RegisterStartupScript Method[^]

Using them, you can inject the scripts into a page from server side and based on certain condition (and specific to page.)

For samples on how to use it: How to Use RegisterClientScriptBlock & RegisterStartupScript[^]
Sample 2[^]


这篇关于在我网页的contentPlaceHolder中包含我的JavaScript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆