JQuery 对 ASP.NET AJAX 更新面板中动态创建的控件的影响
你好,StakOverflowians! 想知道是否有人可以帮忙解决这个问题。
我在 AJAX 更新面板中有一个动态创建的标签。单击该标签后,我尝试(暂时)使用 JQuery 显示警报。 问题是,动态生成标记时不会显示警报,但是,如果我在 .aspx 中静态声明标记,则它可以工作。 这是代码:
protected void Button_Click(object sender, EventArgs e){
HtmlAnchor htmlA = new HtmlAnchor();
htmlA.ID = "hidden_link";
PlaceHolder1.Controls.Add(htmlA);
string javaScriptFunction =
"jQuery(document).ready(function() {" +
"$(function () {" +
"$(\"a[id$='hidden_link']\").click(function () {" +
"alert('Alert: Hello from jQuery!');" +
"});" +
"});" +
"}";
ScriptManager.RegisterStartupScript(this,
this.GetType(),
"myScript",
javaScriptFunction,
true);
}
在 .aspx 上,我有:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
<script language="javascript" type="text/javascript">
function pageLoad() {
jQuery(document).ready(function() {
$("a[id$='hidden_link']").click(function() {
alert("Alert: Hello from jQuery!");
});
});
}
</script>
我应该做什么才能使警报适用于 PlaceHolder1 内动态创建的控件? 任何帮助将不胜感激。
谢谢, 阿里
Hello StakOverflowians!
Wondering whether someone could help with this.
I have a dynamically created tag inside an AJAX update panel. Upon clicking the tag, I am trying to (for the time being) display an alert using JQuery.
The problem is that the alert doesn't get displayed when the tag is generated dynamically, however, if I declare the tag statically in .aspx, then it works.
Here is the code:
protected void Button_Click(object sender, EventArgs e){
HtmlAnchor htmlA = new HtmlAnchor();
htmlA.ID = "hidden_link";
PlaceHolder1.Controls.Add(htmlA);
string javaScriptFunction =
"jQuery(document).ready(function() {" +
"$(function () {" +
"$(\"a[id$='hidden_link']\").click(function () {" +
"alert('Alert: Hello from jQuery!');" +
"});" +
"});" +
"}";
ScriptManager.RegisterStartupScript(this,
this.GetType(),
"myScript",
javaScriptFunction,
true);
}
and on .aspx, I have:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
<script language="javascript" type="text/javascript">
function pageLoad() {
jQuery(document).ready(function() {
$("a[id$='hidden_link']").click(function() {
alert("Alert: Hello from jQuery!");
});
});
}
</script>
What should I do to get the alert working for dynamically created controls inside the PlaceHolder1?
Any help will be most appreciated.
Thanks,
Ali
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的按钮单击在异步回发中运行,则您实际上并没有重新加载页面。因此,
$(document).ready()
方法不会被调用。要解决您的问题,只需删除呼叫即可。
或者,您可能会发现 jQuery 的 live() 函数更适合将处理程序附加到动态创建的元素
If you're button click is running in an async post back, you're not actually reloading the page. So, the
$(document).ready()
method won't get called.To solve your problem, just remove the call.
Or alternatively, you may find jQuery's live() function more suitable for attaching handlers to elements that are created dynamically