【问题标题】:Calling jQuery function from Button Click event in C#从 C# 中的按钮单击事件调用 jQuery 函数
【发布时间】:2011-10-15 17:49:59
【问题描述】:

我正在尝试在不使用更新面板的情况下将裁剪后的图像加载到 asp:image 中。但问题是它显示图像但它需要加载 $('.image-cropper').each(linkUp); jQuery(window).load() 中存在的函数来启动裁剪过程。但是使用 RegisterClientScriptBlock 方法它不会触发该函数。注意:$('.image-cropper').each(linkUp);应仅在使用新图像更新图像后启动。谁能告诉我错误在哪里?

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ImageResizer;
using System.Text;

public partial class Advanced : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {
        ScriptManager1.RegisterAsyncPostBackControl(Button1); 
        if(!IsPostBack)
            Image1.ImageUrl = "fountain-small.jpg?width=300";
        if (Page.IsPostBack && !string.IsNullOrEmpty(img1.Value)) 
        ImageBuilder.Current.Build("~/"+ImageResizer.Util.PathUtils.RemoveQueryString(img1.Value), "~/cropped.jpg", new ResizeSettings(img1.Value));
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Image1.ImageUrl = "cropped.jpg?width=300";

        UpdatePanel1.Update();
        string key = "executeCropScript";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), key, "<script      
type=\"text/javascript\">jQuery(window).load();</script>", false);


 }
}

【问题讨论】:

标签: c# jquery asp.net c#-4.0 c#-3.0


【解决方案1】:

使用按钮的add属性属性。

例如

Button1.Attributes.Add("onclick", "return:javascript functionname()");

【讨论】:

  • 我可以使用这个属性属性,但是我想在使用 c# 更新面板后触发这个函数。如果我这样使用,那么首先触发客户端脚本,然后触发 .我已经使用了 __do 更新面板并且它有效。但我试图找到一种方法,可以通过自身背后的代码调用函数
【解决方案2】:

你可以这样试试……

     <html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>  
  <script>
  $(document).ready(function(){    
    $("button:first").click(function () {
      update($("span:first"));
    });
    $("button:last").click(function () {
      $("button:first").trigger('click');

      update($("span:last"));
    });

    function update(j) {
      var n = parseInt(j.text(), 10);
      j.text(n + 1);
    }
  });
  </script>
  <style>
  button { margin:10px; }
  div { color:blue; font-weight:bold; }
  span { color:red; }
  </style>
</head>
<body>
  <button>Button #1</button>
  <button>Button #2</button>
  <div><span>0</span> button #1 clicks.</div>
  <div><span>0</span> button #2 clicks.</div>
</body>
</html>

【讨论】:

  • StackOverflow 上有很多这方面的主题,只要搜索'jQuery Code behind'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多