"?-6ren"> "?-有没有办法在外部 javascript 文件中使用“”? 如果我使用代码 在我的 as(c/p)x 页面内的脚本标记中,它工作正常。在呈现的页面上,ClientID 已解析。但是,如果我放入一个外部-6ren">
gpt4 book ai didi

javascript - 有没有办法在外部 javascript 文件中使用 "<%= someObject.ClientID %>"?

转载 作者:IT王子 更新时间:2023-10-29 03:17:02 25 4
gpt4 key购买 nike

有没有办法在外部 javascript 文件中使用“<%= someObject.ClientID %>”?

如果我使用代码

<%= someObject.ClientID %>

在我的 as(c/p)x 页面内的脚本标记中,它工作正常。在呈现的页面上,ClientID 已解析。但是,如果我放入一个外部 JS 文件并添加:

事实并非如此。有没有办法做到这一点,或者我是否坚持将该代码留在 as(c/p)x 文件中?

附带问题 -- 在你的标记文件中执行 <%=... %> 的行为叫什么?

最佳答案

如果你真的想这样做,你可以执行以下操作

<%@ Page ContentType="text/javascript" Language="C#" AutoEventWireup="false" %>
<%@ OutputCache Duration="86400" Location="Any" VaryByParam="None" %>

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

然后在你的页面中引用它

<script src="Scripts/file.aspx" type="text/javascript"></script>

Note When using mentioned method, the only way to pass target page controls client-ids, is to store client id as string in a public property, and then reference it using new instance of that page

如果唯一让您这样做的是客户端 ID,那么您可以使用以下 ASP.NET 4 功能

<any-tag ID="myCustomId" runat="server" ClientIDMode="Static" />

您还可以将所有客户端 ID 放在 C# 类中,然后使用 JSON 将其序列化并在脚本标记中呈现它,这对于版本 4 之前的 ASP.NET 来说可能是一种明智的方式。

Note using serialization method you have possibility to change any tag ids without worrying about javascript element usages, remember that this is not even possible with ASP.NET 4 ClientIDMode

页面代码文件

public partial class About : System.Web.UI.Page
{
...

protected string GetTagIds()
{
return new JavaScriptSerializer()
.Serialize(new
{
myButton = Button1.ClientID,
myCalendar = Calendar1.ClientID
});
}

...
}

页面-ASPX

<script type="text/javascript">
var IDs = <%= GetTagIds() %>;
</script>

任何地方

<script type="text/javascript">
IDs.myCalendar.doSomthing...
</script>

还有另一种选择,您可以将所有 javascript 文件传递​​给 ASP.NET 处理程序,但我不推荐这样做,因为只有一个 javascript 文件会使 asp.net 处理程序忙碌。

代码块

<%内联代码%>

这是一个内联代码定义,您可以在其中执行代码:

<% Response.Write("Hi"); %>

<% while(i < 0) { %>
<%
Response.Write(i.ToString());
i++;
%>
<%}%>

Note You have to include ';' on end of each statement when using inline code with C# language, you can change inline language using page directive language attribute

<%= 内联表达式 %>

这个相当于调用Response,自己写,看:

<%= "Hi" %> equals to <% Response.Write("Hi"); %>

Note You shouldn't include ';' when using inline expression

<%: 编码内联表达式 %>

这个等于:

Response.Write(HttpUtility.HtmlEncode("<script type="text/javascript">alert('XSS');</script>"))

并且出于安全原因使用 --XSS,任何对此输入的 HTML 都会输出 HTML 编码的文本,可以安全地在页面中显示用户输入的内容。

Note You shouldn't include ';' when using encoded inline expression

<%$ expressionPrefix: expressionField %>

这是一个表达式,您可以使用它来绑定(bind)来自 ConnectionStrings、Resources 和 AppSettings 的值

表达式前缀的可能性是

  • 应用设置
  • 连接字符串
  • 资源

expressionField 是你需要的指定expressionPrefix 的属性,见:

// AppSettings
<asp:Label ... Text="<%$ AppSettings: version %>" />

// ConnectionStrings
<asp:SqlDataSource ... ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" />

// Resources
<asp:Label ... Text="<%$ Resources: Messages, Welcome %>" />

Note You shouldn't include ';' and you can use expressions only on ASP.Net controls attributes

<%# 数据绑定(bind)表达式 %>

您可以在支持数据绑定(bind)的控件内的任何位置使用它,通常由 Eval 和 Bind 方法使用。

<asp:DropDownList SelectedValue='<%# Bind("CategoryID") %>' 
DataSourceID="CategoriesDataSource"
DataTextField="CategoryName"
DataValueField="CategoryID"
runat="Server" />

Eval 和 Bind 哪个?

使用绑定(bind),您可以在 ASP.NET 控件的指定属性上设置双向绑定(bind),请参阅提到的下拉菜单,它使用绑定(bind)意味着如果最终用户选择一个值然后提交页面,下拉菜单将不会丢失其选定的值。

仅将 Eval 用于显示数据。

<asp:FormView ...>
<ItemTemplate>
<a href='Eval("Link")'>Eval("LinkText")</a>
</ItemTemplate>
</asp:FormView>

<%@ 文本模板指令 %>

<%@ Page ...%>
This one is Page Directive

<%@ OutputCache...%>
This one is OutputCache Directive and so on...

关于javascript - 有没有办法在外部 javascript 文件中使用 "<%= someObject.ClientID %>"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6542079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com