从 ASP.Net 和 C# 后面的代码调用 JQuery 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/555016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call JQuery function from code behind (aside) ASP.Net and C#
提问by Ixtlan
I need to disable JQuery tabs programmatically. The tabs are inside an update panel (Ajax) and the update panel is in an ASP.NET page. Code:
我需要以编程方式禁用 JQuery 选项卡。选项卡位于更新面板 (Ajax) 内,更新面板位于 ASP.NET 页面中。代码:
<link type="text/css" rel="stylesheet" href="http://ui.jquery.com/testing/themes/base/ui.all.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://ui.jquery.com/testing/ui/ui.core.js"></script>
<script type="text/javascript" src="http://ui.jquery.com/testing/ui/ui.tabs.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#example").tabs();
});
function hidetabs(){
$(document).ready(function(){
$("#example").tabs();
$('#example').data('disabled.tabs', [1, 2]);});
}
</script>
<%@ Page Language="C#" MasterPageFile="~/any.Master" AutoEventWireup="true" Codebehind="anycode.aspx.cs"
Inherits="anycode" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel_Deal_Import" runat="server">
<ContentTemplate>
<div id="example">
<ul>
<li><a href="#fragment-1"><span>One</span></a></li>
<li><a href="#fragment-2"><span>Two</span></a></li>
<li><a href="#fragment-3"><span>Three</span></a></li>
</ul>
<div id="fragment-1">
<p>
First tab is active by default:</p>
<pre><code>$('#example').tabs();</code></pre>
<asp:Button ID="btn_Save" OnClick="btn_Save_Click" runat="server" Text="Save" Visible="False" CommandName="Save">
</div>
<div id="fragment-2">
Lorem ipsum dolor sit amet, consectetuer
ut laoreet dolore magna aliquam erat volutpat.
</div>
<div id="fragment-3">
Lorem ipsum dolor sit amet, consectetuer
aliquam erat volutpat.
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Code behind:
protected void btn_Save_Click(object sender, EventArgs e)
{
//here I need to diable the panels.
}
The function btn_Save_Click doesn't post the page so it doesn't call the Javascript/jquery hidetabs function. Thanks for any help!!!
函数 btn_Save_Click 不会发布页面,因此它不会调用 Javascript/jquery hidetabs 函数。谢谢你的帮助!!!
回答by hamed aj
I've use following way and for me work 100% properly:
我使用以下方式,对我来说 100% 正常工作:
the first i create a function and write my jquery function in to the function in the my page:
第一个我创建一个函数并将我的 jquery 函数写入我页面中的函数:
<script>
function myFunction(params) {
$.my_jquery(params...)
......
}
</script>
then i used this code in event handler of my control(for example click a button) who my control is inside a update panel:
然后我在我的控件的事件处理程序中使用了此代码(例如单击按钮),我的控件位于更新面板内:
protected void myButton(object sender, EventArgs e)
{
.....
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>myFunction(params...);</script>", false);
}
successful
成功的
回答by Chris Ballance
Sounds like you need a button that has a client-sideaction instead of one that posts back.
听起来您需要一个具有客户端操作而不是回发操作的按钮。
<asp:Button ID="Clickable" runat="server" text="Click me" OnClientClick="JavascriptCall();" />
Additionally, while jQuery is generally backward compatible, it's not a good idea to reference jQuery's latest .js file directly. Instead I would download a version of it that you want, and place it statically on your site for direct reference of a known version. It is not good to add a dependency on the state or availability of a resource on an external site when not necessary.
此外,虽然 jQuery 通常向后兼容,但直接引用 jQuery 的最新 .js 文件并不是一个好主意。相反,我会下载您想要的版本,并将其静态放置在您的站点上,以便直接参考已知版本。在没有必要的情况下,在外部站点上添加对资源状态或可用性的依赖是不好的。
回答by Ixtlan
Thanks Chris,
谢谢克里斯,
Actually yes, part of the solution is to call directly the javascript function, but instead of using a client-side control I called the Javascript once my UpdatePanel request is over as is explained in the following blog:
实际上是的,解决方案的一部分是直接调用 javascript 函数,但不是使用客户端控件,而是在我的 UpdatePanel 请求结束后调用 Javascript,如以下博客中所述:
http://blog.jeromeparadis.com/archive/2007/03/01/1501.aspx
http://blog.jeromeparadis.com/archive/2007/03/01/1501.aspx
Now my code looks like:
现在我的代码看起来像:
<link type="text/css" rel="stylesheet" href="css/ui.all.css" />
<script type="text/javascript" src="jquery-latest.js"></script>
<script type="text/javascript" src="ui.core.js"></script>
<script type="text/javascript" src="ui.tabs.js"></script>
<script type="text/javascript">
//enable tabs if a deal is selcted or saved.
function EndRequestHandler(sender, args) {
var rec_id = document.getElementById('<%=hidden_value.UniqueID %>').value;
if (rec_id=="")
hidetabs();
else
showtabs();
}
hidetabs();
$(document).ready(function(){
$("#rec_entry").tabs();
});
function hidetabs(){
$(document).ready(function(){
$("#rec_entry").tabs();
$('#rec_entry').data('disabled.tabs', [1, 2, 3, 4, 5]);});
}
function showtabs(){
$(document).ready(function(){
$("#rec_entry").tabs();
$('#rec_entry').data('disabled.tabs', []);});
}
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
</script>
...html code add the tabs...
Code behind:
后面的代码:
protected void btn_Save_Click(object sender, EventArgs e) {
protected void btn_Save_Click(object sender, EventArgs e) {
.... code to save the new record ........
UpdatePanel_mypanel.Update();
}
after the panel is updated the EndRequestHandler evalues a flag (in this case a hiddenfield) and calls the Javascript function that enable or disable the tabs.
面板更新后,EndRequestHandler 评估一个标志(在本例中为隐藏字段)并调用启用或禁用选项卡的 Javascript 函数。
The endrequesthandler is controlled thanks to this sentence included in my javascript:
由于我的 javascript 中包含这句话,endrequesthandler 得到控制:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
I followed your advice of including the javascript files into my project. Thanks again!
我按照您的建议将 javascript 文件包含到我的项目中。再次感谢!
Ixtlan
伊斯特兰