English 中文(简体)
使用ajax从代码背后调用javascript
原标题:Calling javascript from code-behind using ajax

因此,我有了创建一个asp.net用户控件的想法,以处理所有形式的模态弹出窗口(无论是错误处理、窗体、对话框等)。

我之前已经创建了这个,将用户控件放在顶部母版页中,将其暴露给所有内容页,因此我可以执行以下操作:

Master.Popup.ShowException(Exception);

用户控件本身将具有必要的标记,使其看起来像模态对话框,并且在show方法中只需执行.Visible=true即可。

这一切都很好,但我已经开始考虑用jquery实现一些样式。我想让用户控件显示一些jquery动画。但我不知道如何实现这一点,因为我不知道该如何从codeehind而不是popup.visible=true调用jquery函数。

有人能给我一个可能的解决方案吗?

最佳回答

在您的Master.Popup.ShowException(….)中使用以下内容:

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "animateJS", "animateMyBox( popupid );", true);

其中animateMyBox(…)是你想做的任何动画。最后弹出。display=“block”;等

此外,查找是否可以传递一个函数以在设置动画后执行的动画代码中回调。比如:

animateMyBox( popupid , function() { document.getElementById( popupid ).display= block ; } )

另一种可能性是使用一个动画,该动画最终会在框中显示,就像设置从0到100%的不透明度动画一样。

我知道这很模糊,但在得到更好的答案之前,您必须共享更多的代码。

问题回答

您可以创建一个呈现javascript的控件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace WebApplication1
{
    [DefaultProperty("EffectType"), ToolboxData("<{0}:jQueryAnimation runat=server></{0}:jQueryAnimation>")]
    public class jQueryAnimation : System.Web.UI.WebControls.WebControl
    {
        private const string SCRIPT_KEY = "jQueryAnimation";

        [Bindable(true), Category("Appearance"), DefaultValue("Bounce")]
        public string EffectType { get; set; }
        public string ControlId { get; set; }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            RegisterAnimationScript();
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
        }

        private void RegisterAnimationScript()
        {
            if (!Page.ClientScript.IsClientScriptBlockRegistered(SCRIPT_KEY))
            {

                StringBuilder script = new StringBuilder();
                script.Append("<script type= text/javascript >");
                script.Append(Environment.NewLine);
                script.Append("$(document).ready(function () {");
                script.Append(Environment.NewLine);
                script.Append("var options = {};");
                script.Append(Environment.NewLine);
                script.Append("$( #");
                script.Append(this.ControlId);
                script.Append(" ).effect( ");
                script.Append(this.EffectType);
                script.Append(" , options, 1500, ");
                script.Append(this.ControlId);
                script.Append("_callback);");
                script.Append(Environment.NewLine);
                script.Append("function ");
                script.Append(this.ControlId);
                script.Append("_callback() {");
                script.Append(Environment.NewLine);
                script.Append("setTimeout(function() {");
                script.Append("$( #");
                script.Append(this.ControlId);
                script.Append(" ).removeAttr( style ).hide().fadeIn();");
                script.Append("}, 1000 );};");
                script.Append(Environment.NewLine);
                script.Append("});");
                script.Append(Environment.NewLine);
                script.Append("</script>");

                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(),SCRIPT_KEY,script.ToString());
            }
        }
    }
}

I have defined a property EffectType which is the effect name you want to execute; ControlId is the id of the HTML element you want to animate.
RegisterAnimationScript outputs the javascript to do the animation. In your ASPX you have to register the control

<%@ Register assembly="WebApplication1" namespace="WebApplication1" tagprefix="cc1" %>

然后放下你的控件(它应该出现在你的工具箱中)

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="test" style="width:100px;height:100px;background-color:Red">&nbsp;</div>
        <cc1:jQueryAnimation ID="jQueryAnimation1" runat="server" EffectType="Shake" ControlId="test"></cc1:jQueryAnimation>
    </div>
    </form>
</body>
</html>

Here, I ve bound my serer-control jQueryAnimation to the div test (ControlId="test") and I ve defined the effect I want to apply (EffectType="Shake").
It s not very easy to manage javascript in C#, though ;-)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签