2011-10-15 63 views
0

我想在不使用UpdatePanel的情况下将裁剪后的图像加载到asp:image中。但问题是它显示图像,但它需要加载$('。image-cropper')。each(linkUp);函数,它存在于jQuery(window).load()中以开始裁剪过程。但使用RegisterClientScriptBlock方法不会触发该函数。注意:$('。image-cropper')。each(linkUp);只有在用新图像更新图像后才能启动。任何人都可以让我知道错误在哪里?从C#中的按钮单击事件调用jQuery函数

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); 


} 
} 
+0

你试过Page.ClientScript.RegisterStartupScript? MSDN:http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx –

+0

[从C#调用jQuery函数]的可能重复(https://stackoverflow.com/questions/7775491/calling-jquery-function -from-c-sharp)由同一用户 – Nitin

回答

0

你可以尝试这样的...

 <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> 
+0

我需要一种方法来从后面的C#代码中触发jQuery函数。 – Krishh

+1

在StackOverflow上有很多关于此的主题,只是搜索'后面的'jQuery代码' – AFD

1

使用添加属性按​​钮的特性。

e.g

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

我可以使用此属性属性,但我想在使用c#更新面板后触发此函数。如果我这样使用,那么首先客户端脚本触发,然后是后面的c#代码。我使用了__do更新面板,它工作。但我试图找到一种方法,我可以通过自身后面的代码来调用函数 – Krishh