【问题标题】:jquery dialog and asp.net dynamic linkbuttonjquery 对话框和 asp.net 动态链接按钮
【发布时间】:2011-12-06 14:49:46
【问题描述】:

我有一个 gridview,它有一行包含动态添加的 LinkBut​​tons。 单击这些链接按钮时,我需要显示一个确认对话框。 我尝试按照这篇文章中的建议工作: JQuery DIalog and ASP.NET Repeater 但它不起作用,postBackReference 不包含正确的 ID(它忽略占位符) 这是我的代码:

GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
   //some code here

   LinkButton lb = new LinkButton();
   lb.Text = "something";
   lb.ID = "someId";
   string postBackReference = ClientScript.GetPostBackEventReference(lb, string.Empty);
   lb.OnClientClick = "javascript: showConf(function(){"+ postBackReference  +"});return false;";

   TableCell cell = new TableCell();
   cell.Controls.Add(lb);
   e.Row.Cells.Add(cell);
}

有人有想法吗?

【问题讨论】:

  • 找到了!应该改用 RowDataBound 事件(用于使用 asp.net __doPostback 注册我的 JS 函数)
  • 我明白你的意思。无论您是在 OnRowDataBound 还是 OnRowCreated 上执行此操作,我都会使用您的 own id 向 showConfirmation 函数添加一个额外的参数,这样您就不必解析服务器端id 以了解您正在操作的记录。我在答案的 cmets 部分添加了一些细节。

标签: c# javascript jquery asp.net


【解决方案1】:

所以这是一个对我有用的解决方案: 基本上我根据这篇文章中的解决方案工作: JQuery DIalog and ASP.NET Repeater 唯一的区别是我必须使用 RowCreated 事件来添加我的动态 LinkBut​​tons 和 RowDataBound 事件来注册我的客户端函数(否则原始 __doPostBack 不会正确获取 ID 参数(好像它忽略了它在占位符))。 所以我后面的代码现在看起来像这样:

GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
   //some code here

   LinkButton lb = new LinkButton();
   lb.Text = "something";
   lb.ID = "someId";

   TableCell cell = new TableCell();
   cell.Controls.Add(lb);
   e.Row.Cells.Add(cell);
}

和:

GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
   //some code here

   LinkButton lb = e.Row.FindControl("someId") as LinkButton;
   string postBackReference = ClientScript.GetPostBackEventReference(lb, string.Empty);
   lb.OnClientClick = "javascript: showConf(function(){"+ postBackReference  +"});return false;";

}

客户端功能 - showConf 和标记保持原样。

【讨论】:

  • 欢迎来到 Stack Overflow!只要您有能力,请务必将您的答案标记为已接受,以便其他人知道有解决方案。
【解决方案2】:

我不确切知道 JQuery 在您的场景中的作用。我想您想显示某种精美的确认框,如果您提供详细信息,我将提供更具体的答案,但是现在,这就是使用纯 javascript 完成的方式:

GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{

   LinkButton lb = new LinkButton();
   lb.Text = "something";
   lb.ID = "someId";
   lb.OnClientClick = "javascript: return confirm('Are you sure that you want to do this and that?'); ";

   TableCell cell = new TableCell();
   cell.Controls.Add(lb);
   e.Row.Cells.Add(cell);
}

更新 - 为 JQuery UI 方法尝试类似的方法

  1. 在您的标记中有一个 id="dialog-confirm" 的 div,如下所示:

     <div id="dialog-confirm" title="" ></div>
    
  2. 定义一个名为 showConfirmation 的 JavaScript 函数:

     function showConfirmation(confirmationMessage)
     {
          $("#dialog-confirm").dialog("destroy");
          $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            title: confirmationMessage,
            modal: true,
            buttons: {
                "Yes": function() {
                    $( this ).dialog( "close" );
                    __doPostBack(linkItemID, '');//will cause postback
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            }
        });
     return false; //it will never postback
     }
    
  3. 现在您的代码应该如下所示:

     GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
     {
       LinkButton lb = new LinkButton();
        lb.Text = "something";
        lb.ID = "someId";
        lb.OnClientClick = "return showConfirmation('Are you sure you want to do this and that?','"+lb.ID+"'); ";
        TableCell cell = new TableCell();
        cell.Controls.Add(lb);
        e.Row.Cells.Add(cell);
     }
    

注意:上面的代码尚未经过测试,但应该非常接近您的需要。

【讨论】:

  • 对不起,忘了说,我要使用jquery-ui对话框
  • 只需将返回确认替换为显示 jquery ui 对话框的函数即可。
  • 感谢 lcarus 和 thiagoleite,但我认为它不会起作用,因为它不会阻止 ASP.Net 生成的原始 __doPostBack 触发。因此,在用户能够选择按钮之前,会发生回发
  • @A.B.Cade:是的,它会停止它,因为当用户单击对话框上的“取消”按钮时,javascript 函数会返回 false。请注意 OnClientClick 如何具有 return showConfirmation... 这与普通香草 confirm 的工作方式完全相同。如果您在任何情况下都不想回发,只需在Yes 按钮上将return true 更改为return false,或者完全删除两个返回并让函数到达最后一行,它总是返回false。
  • 再次感谢 lcarus,这是我的第一个想法,但它从未对我有用。显然,jquery 对话框不会暂停该进程。你的代码对你有用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-06
相关资源
最近更新 更多