asp.net和jQuery(漂亮的照片)

时间:2010-08-15 11:40:58

标签: jquery asp.net button event-handling

我的aspx页面中有PrettyPhoto的问题。 Update Panel控件中有一个Reapeater控件。 Repeater控件重复表行:每行包含图像,这是一个链接(具有rel = prettyphoto属性)和几个链接按钮(编辑,保存)。 jQuery代码是这样的:

function bindPrettyPhoto()
{
   $("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
};

$(document).ready(function(){
   bindPrettyPhoto();
});

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(bindPrettyPhoto);

当页面加载漂亮的照片工作正常。当我单击一次按钮编辑或保存漂亮的照片工作正常但在此之后单击每个下一步单击更新面板中的任何其他按钮不会导致操作。有任何想法吗?我会很感激任何建议。

此致 马丁

4 个答案:

答案 0 :(得分:1)

您可以采取以下措施:

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

    RegisterScript();
}

private void RegisterScript()
{
    StringBuilder sb2 = new StringBuilder();
    string selector = this.ClientID.ToString();

    if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
    {
        sb2.AppendLine(String.Concat("Sys.Application.add_load(", selector, "_func);"));
        sb2.AppendLine(String.Concat("function ", selector, "_func() {"));
    }
    else
    {
        sb2.AppendLine(" $(document).ready(function() {");
    }

    sb2.AppendLine("$(\"a[rel^='prettyPhoto']\").prettyPhoto({ animation_speed: 'fast' });");
    sb2.AppendLine("}");

    if (!ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
        sb2.Append(");");

    if (ScriptManager.GetCurrent(this.Page).GetRegisteredClientScriptBlocks().FirstOrDefault(e => e.Key == String.Concat("PrettyPhoto", selector)) == null)
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), String.Concat("PrettyPhoto", selector), String.Format("<script type=\"text/javascript\">{0}</script>", sb2.ToString()), false);
}

答案 1 :(得分:1)

使用更新面板时,只有部分页面会回发到服务器。 jquery命令document.ready仅在加载整个页面时触发(或类似的东西)。不幸的是,每次加载页面上的内容时,都需要初始化PrettyPhoto。

如果您使用更新面板并且希望PrettyPhoto正常工作,则需要将PrettyPhoto初始化代码放在.NET AJAX“pageLoad”函数中,如下所示:

function pageLoad() {
   $("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
}

有关document.readypageLoad之间差异的好文章,请查看此链接:

http://encosia.com/document-ready-and-pageload-are-not-the-same/

答案 2 :(得分:0)

尝试将Sys.WebForms...调用放在$(document).ready()函数中。可能是您在页面上加载特定功能之前调用它。

答案 3 :(得分:0)

好吧因为你需要删除这个Jquery插件每次在页面加载时添加的Div标签(在每个帖子上也是如此)。 要做到这一点,请在js文件函数$.fn.prettyPhoto$(document).ready ();中添加以下修复代码 但是你应该确保你的脚本在Jquery插件之前运行

修复代码必须在 $("a[rel^='prettyPhoto']").prettyPhoto() 功能之前在每个页面加载时运行:

//to remove div tag prettyPhoto adds  on each page load 
$('div.pp_pic_holder').remove();
$('div.pp_overlay').remove();
$('div.ppt').remove();
//End  remove div tag prettyPhoto adds  on each page load

因此您可以将功能更改为:

 function bindPrettyPhoto() 
 {
    //to remove div tag prettyPhoto adds  on each page load 
    $('div.pp_pic_holder').remove();
    $('div.pp_overlay').remove();
    $('div.ppt').remove();
    //End  remove div tag prettyPhoto adds  on each page load
    $("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
 }; 

正如我之前所说,您还可以将修复代码添加到js文件函数$.fn.prettyPhoto

因此对于版本2.5.6只需将函数更改为此(通过在函数开头添加固定代码):

 $.prettyPhoto = { version: '2.5.6' }; $.fn.prettyPhoto = function (settings) {

        $('div.pp_pic_holder').remove();
        $('div.pp_overlay').remove();
        $('div.ppt').remove();

.../* the rest of the function*/.....