如何在.CS中调用JS文件中的某个函数 【跪求】急!!!

zhangxun829 2012-05-11 10:11:11
我做了个ajax弹出层效果 控制层打开隐藏的函数都在wbox.js 文件中. 原本在前台 ImageButton1的属性设置为 class="wBox_close"就可以关闭弹出层。 现在我需要在CS文件中调用这个属性的方法 。具体代码如下


(function ($) {
//class为.wBox_close为关闭
$.fn.wBox = function (options) {
var defaults = {
wBoxURL: "wbox/",
opacity: 0.5, //背景透明度
callBack: null,
noTitle: false,
show: false,
timeout: 0,
target: null,
requestType: "iframe", //iframe,ajax,img
title: "wBox Title",
drag: true,
iframeWH: {//iframe 设置高宽
width: 800,
height: 400
},
html: ''//wBox内容
}, _this = this;
this.YQ = $.extend(defaults, options);
var wBoxHtml = '<div id="wBox"><div class="wBox_popup"><table><tbody><tr><td class="wBox_tl"/><td class="wBox_b"/><td class="wBox_tr"/></tr><tr><td class="wBox_b"><div style="width:10px;"> </div></td><td><div class="wBox_body">' + (_this.YQ.noTitle ? '' : '<table class="wBox_title"><tr><td class="wBox_dragTitle"><div class="wBox_itemTitle">' + _this.YQ.title + '</div></td><td width="20px" title="关闭"><div class="wBox_close"></div></td></tr></table> ') +
'<div class="wBox_content" id="wBoxContent"></div></div></td><td class="wBox_b"><div style="width:10px;"> </div></td></tr><tr><td class="wBox_bl"/><td class="wBox_b"/><td class="wBox_br"/></tr></tbody></table></div></div>', B = null, C = null, $win = $(window), $t = $(this); //B背景,C内容jquery div
this.showBox = function (what) {
$("#wBox_overlay").remove();
$("#wBox").remove();

B = $("<div id='wBox_overlay' class='wBox_hide'></div>").hide().addClass('wBox_overlayBG').css('opacity', _this.YQ.opacity).dblclick(function () {
_this.close();
}).appendTo('body').fadeIn(300);
C = $(wBoxHtml).appendTo('body');
handleClick(what);
}
/*
* 处理点击
* @param {string} what
*/
function handleClick(what) {
var con = C.find("#wBoxContent");
if (_this.YQ.requestType && $.inArray(_this.YQ.requestType, ['iframe', 'ajax', 'img']) != -1) {
con.html("<div class='wBox_load'><div id='wBox_loading'><img src='" + _this.YQ.wBoxURL + "loading.gif' /></div></div>");
if (_this.YQ.requestType === "img") {
var img = $("<img />");
img.attr("src", _this.YQ.target);
img.load(function () {
img.appendTo(con.empty());
setPosition();
});
}
else
if (_this.YQ.requestType === "ajax") {
$.get(_this.YQ.target, function (data) {
con.html(data);
C.find('.wBox_close').click(_this.close);
setPosition();
})

}
else {
_this.YQ.target = what;

ifr = $("<iframe name='wBoxIframe' style='width:" + _this.YQ.iframeWH.width + "px;height:" + _this.YQ.iframeWH.height + "px;' scrolling='auto' frameborder='0' src='" + _this.YQ.target + "'></iframe>");
ifr.appendTo(con.empty());
ifr.load(function () {
try {
$it = $(this).contents();
$it.find('.wBox_close').click(_this.close);
fH = $it.height(); //iframe height
fW = $it.width();
w = $win;
newW = Math.min(w.width() - 40, fW);
newH = w.height() - 25 - (_this.YQ.noTitle ? 0 : 30);
newH = Math.min(newH, fH);
if (!newH)
return;
var lt = calPosition(newW);
C.css({
left: lt[0],
top: lt[1]
});

$(this).css({
height: newH,
width: newW
});
}
catch (e) {
}
});
}

}
else
if (_this.YQ.target) {
$(_this.YQ.target).clone(true).show().appendTo(con.empty());

}
else
if (_this.YQ.html) {
con.html(_this.YQ.html);
}
else {
$t.clone(true).show().appendTo(con.empty());
}
afterHandleClick();
}
/*
* 处理点击之后的处理
*/
function afterHandleClick() {
setPosition();
C.show().find('.wBox_close').click(_this.close).hover(function () {
$(this).addClass("on");
}, function () {
$(this).removeClass("on");
});
$(document).unbind('keydown.wBox').bind('keydown.wBox', function (e) {
if (e.keyCode === 27)
_this.close();
return true
});
typeof _this.YQ.callBack === 'function' ? _this.YQ.callBack() : null;
!_this.YQ.noTitle && _this.YQ.drag ? drag() : null;
if (_this.YQ.timeout) {
setTimeout(_this.close, _this.YQ.timeout);
}

}
/*
* 设置wBox的位置
*/
function setPosition() {
if (!C) {
return false;
}

var width = C.width(), lt = calPosition(width);
C.css({
left: lt[0],
top: lt[1]
});
var $h = $("body").height(), $wh = $win.height(), $hh = $("html").height();
$h = Math.max($h, $wh);
B.height($h).width($win.width())
}
/*
* 计算wBox的位置
* @param {number} w 宽度
*/
function calPosition(w) {
l = ($win.width() - w) / 2;
t = $win.scrollTop() + $win.height() / 9;
return [l, t];
}
/*
* 拖拽函数drag
*/
function drag() {
var dx, dy, moveout;
var T = C.find('.wBox_dragTitle').css('cursor', 'move');
T.bind("selectstart", function () {
return false;
});

T.mousedown(function (e) {
dx = e.clientX - parseInt(C.css("left"));
dy = e.clientY - parseInt(C.css("top"));
C.mousemove(move).mouseout(out).css('opacity', 0.8);
T.mouseup(up);
});
/*
* 移动改变生活
* @param {Object} e 事件
*/
function move(e) {
moveout = false;
if (e.clientX - dx < 0) {
l = 0;
}
else
if (e.clientX - dx > $win.width() - C.width()) {
l = $win.width() - C.width();
}
else {
l = e.clientX - dx
}
C.css({
left: l,
top: e.clientY - dy
});

}
/*
* 你已经out啦!
* @param {Object} e 事件
*/
function out(e) {
moveout = true;
setTimeout(function () {
moveout && up(e);
}, 10);
}
/*
* 放弃
* @param {Object} e事件
*/
function up(e) {
C.unbind("mousemove", move).unbind("mouseout", out).css('opacity', 1);
T.unbind("mouseup", up);
}
}

/*
* 关闭弹出框就是移除还原
*/
this.close = function close() {
if (C) {
B.remove();
C.stop().fadeOut(300, function () {
C.remove();
})
}
}
/*
* 触发click事件
*/
$win.resize(function () {
setPosition();
});
_this.YQ.show ? _this.showBox() : $t.click(function () {
var what = $(this).attr("href");
_this.showBox(what);
return false;
});
return this;
};
})(jQuery);
这是WBOX.JS
  
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
strsql = "insert into ABC(单位名称)values(" + t + ");
bool z = Das.ExecSql(strsql);
if (z)
{
这里如何调用WBOX.JS 的关闭弹出层的函数
}

else
{
Response.Write("<script>alert('单位信息新增失败!');</script>");
}
}
...全文
813 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxun829 2012-05-12
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 的回复:]
Response.Write 的内容会出现在 生成的HTML的前面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xht……
[/Quote]
嗯我知道会出现自最前面,每次执行完都会查看下源码的,现在已经不知道怎么办了,太纠结了
这问题已经纠结了一天半了。。。。
bonnibell 2012-05-12
  • 打赏
  • 举报
回复
Response.Write 的内容会出现在 生成的HTML的前面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
zhangxun829 2012-05-12
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 的回复:]
引用 11 楼 的回复:

我换了个思路 直接隐藏一个按钮class=“wbox” HTML code
<head id="Head2" runat="server">

<script type="text/javascript">
function Click(o)
{
event.returnValue = false;
documen……


Page.Client……
[/Quote]

我想直接把关闭弹出层的函数输出到客户端执行但是老是报错 Response.Write("<script>$(function(){$("#wBox").remove();})</script>"); 预处理器指令必须作为一行的第一个非空白字符出现
zhangxun829 2012-05-12
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 的回复:]
引用 11 楼 的回复:

我换了个思路 直接隐藏一个按钮class=“wbox” HTML code
<head id="Head2" runat="server">

<script type="text/javascript">
function Click(o)
{
event.returnValue = false;
documen……


Page.Client……
[/Quote]运行结果溢出
<script type="text/javascript">
onload = function click() {
click();
}
</script>
bonnibell 2012-05-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

我换了个思路 直接隐藏一个按钮class=“wbox” HTML code
<head id="Head2" runat="server">

<script type="text/javascript">
function Click(o)
{
event.returnValue = false;
documen……
[/Quote]

Page.ClientScript.RegisterStartupScript 的内容改成
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script type=\"text/javascript\">");
sb.AppendLine("onload = function() {");
sb.AppendLine(" Click();");
sb.AppendLine("}");
sb.AppendLine("</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "Click", sb.ToString());

试试
zhangxun829 2012-05-12
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]
你干嘛非的在css中调用js啊,你用的jquery,直接$("id").css在这里更改css不就完了
[/Quote]不懂,求教育
zhangxun829 2012-05-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
ifr.load(
。。。。
$it.find('.wBox_close').click(_this.close);
。。。

注册了 class是 wBox_close的 Dom对象的 click处理函数_this.close

估计是 ifr 还没有 完成这个注册过程,Page.ClientScript.RegisterStartupScript里生成的js就已经执行了
……
[/Quote]嗯 后来我测试了下 确实是执行了 ImageButton.click() 但是页面不关闭 必须要手动点击ImageButton才能关闭。应该是你说的没有完成注册 Page.ClientScript.RegisterStartupScript就已经执行JS了,弱弱的问一下延时怎么加?
zhangxun829 2012-05-12
  • 打赏
  • 举报
回复
自己钻了死胡同 调用jQuery函数不成功,就没想到 重写关闭函数 。。
zhangxun829 2012-05-12
  • 打赏
  • 举报
回复
感谢 bonnibell 提供的思路。虽然最后还是没有能够将jQuery函数调用成功,但是我要得效果还是实现了。我重新写了关闭iframe弹出层


function click1()
{
parent.document.getElementById("XXX").style.display = "none";

}



Page.ClientScript.RegisterStartupScript(this.GetType(), "click1", "<script>   document.getElementById('click1').click(); </script>");
bonnibell 2012-05-11
  • 打赏
  • 举报
回复
asp:ImageButton 其他的内容不变,我这只是一个演示
bonnibell 2012-05-11
  • 打赏
  • 举报
回复
应该是页面回传了,没有来得及执行客户端的方法
如果你给了ImageButton 一个服务器短的单击事件,那就在ImageButton的前面隐藏一个链接,
并且ImageButton的class去掉


<a id="tempclick" class="wBox_close" href="#"></a>
<asp:ImageButton/>


注册脚本改成

Page.ClientScript.RegisterStartupScript(this.GetType(), "tempclick", "<script> document.getElementById('tempclick').click(); </script>");


我没有试啊,感觉是这个问题。
zhangxun829 2012-05-11
  • 打赏
  • 举报
回复
我是菜鸟 求高手解答 希望能告诉我错的原因
zhangxun829 2012-05-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
http://www.cnblogs.com/zyx_blog/articles/1993473.html
[/Quote]
直接调用ASPX页面中的JS函数 用Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script></script>"); 能实现 ,但是我要引用 WBOX.JS文件中的函数 这样写没有反应。不知道哪里错了求解答
hwbox 2012-05-11
  • 打赏
  • 举报
回复
又看了下问题,我完全驴唇马嘴了
zhangxun829 2012-05-11
  • 打赏
  • 举报
回复


bool z = Das.ExecSql(strsql);
if (z)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "wBox_close", "<script>function (close)</script>");
}
我试过了 没有反应
netstudy0105 2012-05-11
  • 打赏
  • 举报
回复
你干嘛非的在css中调用js啊,你用的jquery,直接$("id").css在这里更改css不就完了
bonnibell 2012-05-11
  • 打赏
  • 举报
回复
ifr.load(
。。。。
$it.find('.wBox_close').click(_this.close);
。。。

注册了 class是 wBox_close的 Dom对象的 click处理函数_this.close

估计是 ifr 还没有 完成这个注册过程,Page.ClientScript.RegisterStartupScript里生成的js就已经执行了

这种情况最搓的方案是 加个延迟时间在执行RegisterStartupScript里生成的js





hwbox 2012-05-11
  • 打赏
  • 举报
回复
我的cms项目中的js引擎类

/// <summary>
/// JSEngine 在后台运行js的解释器返回值
/// </summary>
public class JSEngine
{
//初始化加载程序的字符串;
public static readonly String _jsString = @"class theEval{ public function Eval(str:String):String {return eval(str)}}";
public static readonly String _jsClassName = @"theEval";
public static readonly String _jsMethodName = @"Eval";
//定义对象
public static object _evalObject = new object();
public static Type _evalType = new object().GetType();


//构造函数
static JSEngine()
{
CreatJSEngine(ref _evalObject, ref _evalType, _jsString, _jsClassName);
}

/// <summary>
/// 初始化引擎的方法
/// </summary>
/// </summary>
/// <param name="evalObject">指定一个 已存在的变量 保存 使用evalType的类型生成一个实例</param>
/// <param name="evalType">指定一个 已存在的变量 保存 生成的程序集中有指定名称的对象type</param>
/// <param name="jsString">指定用来生成程序集的代码文本</param>
/// <param name="jsClassName">指定用来进行调用的程序集中的类别名称</param>
private static void CreatJSEngine(ref object evalObject, ref Type evalType, string jsString, string jsClassName)
{
CodeDomProvider _provider = new JScriptCodeProvider();
CompilerParameters _parameters = new CompilerParameters();
_parameters.GenerateInMemory = true;
CompilerResults _result = _provider.CompileAssemblyFromSource(_parameters, jsString);
Assembly _assembly = _result.CompiledAssembly;
evalType = _assembly.GetType(jsClassName);
evalObject = Activator.CreateInstance(evalType);
}

/// <summary>
/// StringEval
/// </summary>
/// <param name="inp_JSString">传入一个表达式</param>
/// <returns>返回一个表达式的值</returns>
public static object StringEval(string inp_JSString)
{
return _evalType.InvokeMember(_jsMethodName, BindingFlags.InvokeMethod, null, _evalObject, new object[] { inp_JSString });
}

/// <summary>
/// StringEval
/// </summary>
/// <param name="inp_JSFunctionString">指定用来生成程序集的代码文本</param>
/// <param name="inp_JSFunctionClassName">指定用来进行调用的程序集中的类别名称</param>
/// <param name="inp_JSFunctionClassFuncName">指定用来进行调用的程序集中的类别中的函数的名称</param>
/// <param name="inp_ParameterObject">要传递到程序集中的变量对象数组,无参时使用 new object[0]</param>
/// <returns>返回函数调用的值</returns>
public static object StringEval(string inp_JSFunctionString, string inp_JSFunctionClassName, string inp_JSFunctionClassFuncName, object[] inp_ParameterObject)
{
object ClosureevalObject = null;
Type ClosureevalType = null;
CreatJSEngine(ref ClosureevalObject, ref ClosureevalType, inp_JSFunctionString, inp_JSFunctionClassName);
return ClosureevalType.InvokeMember(inp_JSFunctionClassFuncName, BindingFlags.InvokeMethod, null, ClosureevalObject, inp_ParameterObject);
}

/// <summary>
/// StringEval
/// </summary>
/// <param name="inp_JSString">传入一个表达式</param>
/// <param name="inp_theType">指定的要返回的值类型</param>
/// <returns>返回的值</returns>
public static object StringEval(string inp_JSString, Type inp_theType)
{
return Microsoft.JScript.Convert.Coerce(_evalType.InvokeMember(_jsMethodName, BindingFlags.InvokeMethod, null, _evalObject, new object[] { inp_JSString }), inp_theType);
}
}
加载更多回复(4)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧