在asp.net codebehind JavaScript的执行 [英] execute javascript from codebehind in asp.net
问题描述
我要火从code这个脚本只有在数据库中指定特定的日期后面
我需要显示的图像作为一个的fancybox根据CMS系统指定的日期。
我会写我的逻辑,以显示中的Default.aspx影像与否,然后以某种方式从default.cs到MarterPage的Javascript $(窗口).load(函数传递这块code的()
code座
<脚本>
$(窗口).load(函数(){ 我想这code开火母版页的特定位置。我不知道这里假设。
///这里??? });
< / SCRIPT>
我想下面的脚本传递一个值,以便它JS执行 /// HERE ???
code的一部分
Page.ClientScript.RegisterStartupScript(this.GetType()的MyScript,$('a.fancybox-留言板)的fancybox({宽度:600,高度:440 ,closeClick:真的,hideOnOverlayClick:真中,href:http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg'})触发('点击');,真正的);
我有点这个失去了...
我想简单做在Default.aspc文件,并显示图像我的逻辑检查,因为只有在Default.aspx页面看中框。但我的'$(窗口).load(函数(){}); code块是母版文件,如果我再写'$(窗口).load(函数(){}) ;
在Default.aspx文件,然后看中框无法正确显示。
我怎样才能做到这一点没有任何问题。
更新:
我设法把它关闭。这是根据发布的伊琳娜Bogomaz
解决方案。
到目前为止,这是我的工作完全可以逻辑添加到code-落后以后。
$(窗口).load(函数(){ 如果(window.showMessage){
//警报(imgPath);
//逻辑创建的fancybox
$(a.fancybox-留言板)。的fancybox({
宽度:600,
高度:440,
closeClick:真实,
hideOnOverlayClick:真实,
HREF:imgPath
})触发('点击');
} });
code背后 保护覆盖无效在preRender(EventArgs的发送)
{
字符串imgMB ='http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg';
字符串sScript =VAR showMessage = TRUE; VAR imgPath =+ imgMB;
Page.ClientScript.RegisterStartupScript(this.GetType(),的MyScript,sScript,真);
}
您可以通过以下方式实现这一点:
母版页:
<脚本类型=文/ JavaScript的>
$(窗口).load(函数(){
如果(window.isFancybox){
//逻辑创建的fancybox
}
});
< / SCRIPT>
Default.aspx.cs:
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
VAR fanceBoxDate =新日期时间(2013,11,20); //从CMS系统获取日期
如果(DateTime.Today == fanceBoxDate)
{
ScriptManager.RegisterClientScriptBlock(此,的GetType(),的fancybox,变种isFancybox =真,真);
}
}
I need to fire this script from code behind only on certain dates specified in the database
I need to show an image as a fancybox based on the date specified in CMS system.
I will write my logic to show image or not in the Default.aspx and then somehow pass this piece of code from default.cs to MarterPage Javascript $(window).load(function ()
Code Block
<Script>
$(window).load(function () {
I want this code to fire at a particular location of master page. I am not sure suppose here.
///HERE???
});
</Script>
I want to pass below script as a value so that it js execute ///HERE???
part of code
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "$('a.fancybox-messageboard').fancybox({ width: 600, height: 440,closeClick: true, hideOnOverlayClick: true, href: 'http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg' }).trigger('click');", true);
I am bit lost with this ...
I simple want to do my logic check in Default.aspc file and show image as a fancy box only in default.aspx page. But my ' $(window).load(function () { });code block is in MasterPage file if i write another ' $(window).load(function () { });
in default.aspx file then fancy box is not showing properly.
How can i achieve this without any issue
UPDATE:
I managed to pull it off.. this is based on the solution posted by Irina Bogomaz
.
So far this is working I can add full logic to code-behind later on.
$(window).load(function () {
if (window.showMessage) {
// alert(imgPath);
//logic to create fancybox
$("a.fancybox-messageboard").fancybox({
width: 600,
height: 440,
closeClick: true,
hideOnOverlayClick: true,
href: imgPath
}).trigger('click');
}
});
CODE BEHIND
protected override void OnPreRender(EventArgs e)
{
string imgMB = "'http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg'";
string sScript = "var showMessage = true; var imgPath=" + imgMB;
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", sScript, true);
}
You can achieve this in the following way:
Master page:
<script type="text/javascript">
$(window).load(function () {
if (window.isFancybox) {
//logic to create fancybox
}
});
</script>
Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
var fanceBoxDate = new DateTime(2013, 11, 20); //get date from CMS system
if (DateTime.Today == fanceBoxDate)
{
ScriptManager.RegisterClientScriptBlock(this, GetType(), "fancyBox", "var isFancybox = true", true);
}
}
这篇关于在asp.net codebehind JavaScript的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!