如何调用从C#jQuery函数 [英] How to call Jquery function from C#

查看:465
本文介绍了如何调用从C#jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想淡入和出一个div执行一些code之后。这个jQuery功能使用该按钮的OnClientClick属性时工作正常。但是,不能将其从C#调用。

JS:

 函数确认()
{
 $(保存)淡入(500).fadeOut(500)。
}

C#:

 保护无效btnsave_click(对象发件人,EventArgs的发送)
{
  //一些code
  upd.update();
  ScriptManager.RegisterStartupScript(this.Page,this.GetType(),确认,
   <脚本类型=文/ JavaScript的'>确认();< / SCRIPT>中,真正的);
}

HTML:

 < ASP:的UpdatePanel ID =UPD=服务器的UpdateMode =有条件的>
 <&触发GT;
  < ASP:asyncpostbacktrigger控件ID =btnsaveeventname已=点击/>
 < /触发>
 <&的ContentTemplate GT;
  < D​​IV的风格=的位置是:相对>
    <跨度类=保存>记录保存< / SPAN>
    < ASP:GridView的...... />
  < / DIV>
  < ASP:按钮的ID =btnsave=​​服务器文本=拯救的onclick =btnsave_click/>
 < /&的ContentTemplate GT;
< / ASP:的UpdatePanel>

CSS:

  .saved
{
  位置:绝对的;
  背景:绿色;
  保证金:0汽车;
  宽度:100像素;
  高度:30PX;
  visibility:hidden的;
}


解决方案

C#

 保护无效btn_click(对象发件人,EventArgs的发送)
{
    ScriptManager.RegisterStartupScript(this.Page,this.GetType(),脚本,确认();,真正的);
}

JS:

 <脚本>
   功能确认()
   {
      $(保存)淡入(500).fadeOut(500)。
   }
< / SCRIPT>


编辑提高code

C#

 保护无效btn_click(对象发件人,EventArgs的发送)
{
    ScriptManager.RegisterStartupScript(this.Page,this.GetType(),脚本,确认();,真正的);
}

JS:

 <脚本>
    $(文件)。就绪(函数(){       功能确认(){
           $(保存)淡入(500).fadeOut(500)。
       };
    });
< / SCRIPT>

I would like to fadein and out a div after performing some code. This JQuery function is working fine when using the button's onclientclick property. But, Unable to call it from c#.

JS:

function Confirm()
{
 $(".saved").fadeIn(500).fadeOut(500);
}

C#:

protected void btnsave_click(object sender, Eventargs e)
{
  //some code
  upd.update();
  ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Confirm", 
   "<script type='text/javascript'>Confirm();</script>", true);
}

HTML:

<asp:updatepanel id="upd" runat="server" updatemode="conditional">
 <triggers>
  <asp:asyncpostbacktrigger controlid="btnsave" eventname="click"/>
 </triggers>
 <contenttemplate>
  <div style="position:relative">
    <span class="saved">Record Saved</span>
    <asp:gridview....../>
  </div>
  <asp:button id="btnsave" runat="server" text="save" onclick="btnsave_click"/>
 </contenttemplate>
</asp:updatepanel>

CSS:

.saved
{
  position:absolute;
  background:green;
  margin:0 auto;
  width:100px;
  height:30px;
  visibility:hidden;
}

解决方案

C#

protected void btn_click(object sender, Eventargs e)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "Confirm();", true);
}

JS:

<script>
   function Confirm()
   {
      $(".saved").fadeIn(500).fadeOut(500);
   }
</script>


Edit Improve Code

C#

protected void btn_click(object sender, Eventargs e)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "Confirm();", true);
}

JS:

<script>
    $(document).ready(function () {

       function Confirm() {
           $(".saved").fadeIn(500).fadeOut(500);
       };
    });
</script>

这篇关于如何调用从C#jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆