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

我有一个gridview,其中有一行包含动态添加的LinkBut​​tons。 单击这些LinkBut​​tons时,我需要显示一个确认对话框。 我尝试按照这篇文章的建议工作: JQuery DIalog和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); } 

有没有人有想法?

所以这里有一个对我有用的解决方案:基本上我根据这篇文章中的解决方案工作: JQuery DIalog和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;"; } 

客户端function – showConf和标记保持不变。

我不确切知道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,如下所示:

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

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