简体   繁体   English

asp.net 中的动态 Javascript

[英]dynamic Javascript in asp.net

well.出色地。 The problem i'm facing is i have my own custom control and i do a query, get records and dynamically add html controls to the page based on the data.我面临的问题是我有自己的自定义控件,我进行查询、获取记录并根据数据动态地将 html 控件添加到页面。

Now there's the problem of adding some dynamic javascript现在有添加一些动态javascript的问题

I do this with the help of literal controls.我在文字控件的帮助下做到了这一点。

<asp:Literal runat="server" ID="latEventToolTipJqueryScripts"></asp:Literal>

This works like a charm这就像一个魅力

<script language="javascript" type="text/javascript">
// <![CDATA[
    Sys.Application.add_load(WireEvents_<%=this.ID%>); // fix wiring for .NET ajax updatepanel
    $(WireEvents_<%=this.ID%>); // handle page load wiring

    function WireEvents_<%=this.ID%>() {
        <asp:Literal runat="server" ID="latEventToolTipJqueryScripts"></asp:Literal>
    }

// ]]>
</script>

I add the literal text dynamically from code behind.我从后面的代码中动态添加文字文本。

However, when placing the control in an updatepanel, the postbacks don't update the script.但是,将控件放在更新面板中时,回发不会更新脚本。

EDIT: The Sys.Application.add_load rewires the necessary functions just fine with the updatepanel.编辑: Sys.Application.add_load使用更新面板重新连接必要的功能。 The problem is that the script that needs to be in place of the literal, doesn't update when in an updatepanel.问题是需要代替文字的脚本在更新面板中时不会更新。

I've tried the ClientScript.RegisterStartupScript but it has the same effect as with the literal control trick.我试过 ClientScript.RegisterStartupScript 但它与文字控制技巧具有相同的效果。 Any help?有什么帮助吗?

---------------------------SOLVED (tnx to Pranay Rana)---------------------------------- ---------------------------------------已解决(tnx 到 Pranay Rana)---------------- ------------------

Got rid of the literal in the ascx side.摆脱了 ascx 方面的文字。 as well as the Sys.Application.add_load以及Sys.Application.add_load

now it's only in the code behind.现在它只在后面的代码中。 The thing that was throwing me off was the JQuery thing.让我失望的是 JQuery 的事情。

this.strBuilderJS.Append( "<script language='javascript' type='text/javascript'>" +
                                "$(WireEvents_" + this.ID + ");" + 
                                "function WireEvents_" + this.ID + "(){"+
                                "    alert('stuff');");

this.strBuilderJS.Append(       "}</script>");

and then接着

ScriptManager.RegisterStartupScript(this, this.GetType(), "strBuilderJS", strBuilderJS.ToString(), false);

Make use of ScriptManager.RegisterStartupScript() to register your script...may resolve problem...使用ScriptManager.RegisterStartupScript()注册您的脚本...可以解决问题...

Check this resolve your problem: Add JavaScript programmatically using RegisterStartupScript during an Asynchronous postback检查此解决您的问题:在异步回发期间使用 RegisterStartupScript 以编程方式添加 JavaScript

I would recommend on a different approach.我会推荐一种不同的方法。 To create a dynamic javascript for any language.为任何语言创建动态 javascript。 I would create a reference to a dynamic file.我会创建对动态文件的引用。 For this example I will use .NET对于这个例子,我将使用 .NET

  1. Create a test.aspx page and add this script:创建一个 test.aspx 页面并添加此脚本:

    <script type="text/javscript" src="js/foo.aspx"></script>

  2. Make sure your page response with the right content type.确保您的页面响应内容类型正确。 So for this instance I would add this on page load code behind for your foo.aspx:因此,对于这种情况,我将在您的 foo.aspx 后面的页面加载代码中添加此代码:

    Response.ContentType = "application/javascript";

  3. On the foo.aspx html view add your javascript code.在 foo.aspx html 视图上添加您的 javascript 代码。

    alert("<%=DateTime.Now%>");

  4. Browse your test.aspx page and you should see an alert with the current date浏览您的 test.aspx 页面,您应该会看到带有当前日期的警报

You should be able to move forward from here.你应该可以从这里继续前进。 The goal is to separate the javascript file from the static page.目标是将 javascript 文件与 static 页面分开。

Happy Coding.快乐编码。

How to change text of ASP Button control on anchor tag click如何在锚标记单击时更改 ASP Button 控件的文本

We have an ASP Button below and we want to change text when WE click on anchor tag我们在下面有一个 ASP 按钮,当我们点击锚标记时我们想要更改文本

<asp:Button ID="btn_Add" runat="server" CssClass="button2" onclick="btn_Add_Click"/>

We have following two anchor tags我们有以下两个锚标签

<a href = "javascript:void(0)" class="toplink" onclick = "changeText1();">Add New</a>

<a href = "javascript:void(0)" class="toplink" onclick = "changeText2();">Add New</a>

Now we add following code in script tag现在我们在脚本标签中添加以下代码

<script type="text/javascript" language="javascript">
    function changeText1() {
        document.getElementById("lbl_header").innerText = 'Add New Teacher';
        document.getElementById("btn_Add").value = 'Add';
    }
    </script>

     <script type="text/javascript" language="javascript">
     function changeText2() {
         document.getElementById("lbl_header").innerText = 'Delete Teacher';
         document.getElementById("btn_Add").value = 'Delete';
     }
    </script>

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

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