Jquery对话框和asp.net动态链接按钮

本文关键字:动态 链接 按钮 net asp 对话框 Jquery | 更新日期: 2023-09-27 18:16:51

我有一个gridview,其中有一行包含动态添加的LinkButtons。当这些链接按钮被点击时,我需要显示一个确认对话框。我试着按照这篇文章的建议工作:JQuery DIalog和ASP。网络中继器但是它不起作用,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);
}

谁有什么主意?

Jquery对话框和asp.net动态链接按钮

所以这是一个解决方案,为我工作:基本上,我是根据这篇文章中的解决方案工作的:JQuery DIalog和ASP。网络中继器唯一的区别是,我必须使用RowCreated事件来添加我的动态LinkButtons和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和markup保持原样

我不知道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);
}

UPDATE -尝试使用JQuery UI方法

  1. 在你的标记中添加一个div, id="dialog-confirm"

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

注意:上面的代码还没有经过测试,但是应该非常非常接近你需要的。