2016-07-27 78 views
1

我希望有人能帮我弄明白这一点。我已经看到四周还没有找到一个能帮助我的例子。我有一种感觉,我在这个超级接近...ASP.NET GridView:确认按钮单击回发与jQueryUI对话框

我有一个asp:gridview在每行的末尾有一个linkbutton。在运行onrowcommand之前,我需要做一个确认对话框。这是我必须做的代码...我错过了什么?

function confirmProcessing(event) { 
    event.preventDefault(); 

    $("#dialog") 
     .dialog({ 
      title: "Confirm Transaction", 
      buttons: { 
       "Process": function() {           
        $("#dialog").dialog('close'); 

       }, 
       Cancel: function() { 
        $("#dialog").dialog('close'); 
        return false; 
       } 
      }, 
      close: function() {        
       return false; 
      }, 
      modal: true, 
      appendTo: "form" 

     }) 
     .parent() 
     .css('z-index', '1005'); 

} 

ASPX代码

<div class="row"> 
    <asp:GridView runat="server" ID="ccList" CssClass="table table-striped table-responsive" ShowHeader="True" AutoGenerateColumns="False" OnRowDataBound="ccList_OnRowDataBound" OnRowCommand="ccList_OnRowCommand"> 
     <Columns> 
      <asp:BoundField DataField="Street" ItemStyle-CssClass="Street" HeaderText="Street"/> 
      <asp:BoundField DataField="ZipCode" ItemStyle-CssClass="ZipCode" HeaderText="Zip"/> 
      <asp:BoundField DataField="MB4P" ItemStyle-CssClass="MB4P" HeaderText="MB4P"/> 
      <asp:BoundField DataField="MB4S" ItemStyle-CssClass="MB4S" HeaderText="MB4S"/> 
      <asp:BoundField DataField="BAL" ItemStyle-CssClass="BAL" HeaderText="BAL"/> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <asp:LinkButton runat="server" ID="btnProcess" OnClientClick="confirmProcessing(event);" Text="Process Payment" CommandName="Process" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>  
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 
</div> 

<div id="dialog" style="display:none">   
    <b>Are you sure you want to process this record?</b> 
</div> 

OnRowCommand代码

protected void ccList_OnRowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Process") 
    { 
     //Do the processing stuff here after confirm dialog. 
    }   
} 

如果我删除的preventDefault它触发通过向其预期OnRowCommand。我只是不知道如何让模态对话框中的Process按钮发出命令......我在这里丢失了什么?

在此先感谢。

+0

aspx代码没有显示出来。我试图编辑它时没有得到编辑器工具栏,所以如果我改变了任何东西,为什么它不能通过格式验证,所以我不知所措...... –

回答

1

LinkBut​​ton的的OnClientClick属性可以在GridView的RowDataBound事件处理程序被设置,与该按钮作为参数的UniqueIDconfirmProcessing

protected void ccList_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     LinkButton btnProcess = e.Row.FindControl("btnProcess") as LinkButton; 
     btnProcess.OnClientClick = string.Format("return confirmProcessing('{0}');", btnProcess.UniqueID); 
     ... 
    } 
} 

JavaScript的事件处理程序然后可以修改为如下:

function confirmProcessing(btnUniqueID) { 
    $("#dialog").dialog({ 
     title: "Confirm Transaction", 
     buttons: { 
      "Process": function() { 
       $("#dialog").dialog('close'); 
       __doPostBack(btnUniqueID, ''); 
      }, 
      Cancel: function() { 
       $("#dialog").dialog('close'); 
       return false; 
      } 
     }, 
     close: function() { 
      return false; 
     }, 
     modal: true, 
     appendTo: "form" 

    }).parent().css('z-index', '1005'); 

    return false; 
} 

当点击LinkBut​​ton的,显示对话框,函数返回false取消初始回发。如果通过单击Process关闭对话框,则会使用LinkBut​​ton的UniqueID调用__doPostBack以导致回发。

注意:我认为UniqueID可以借助name属性在客户端代码中检索。它适用于Button控件,但不适用于LinkBut​​ton。这就是为什么我建议在代码隐藏中设置OnClientClick属性。

+0

我没有意识到我无法获得UniqueID LinkBut​​ton。这就是为什么编辑给我一个错误,说它不能识别我的btnProcess.UniqueId引用。我无法弄清楚为什么,它真的把我抛弃了。我现在会测试它,看看它是否工作。 PS:感谢您修复上述格式中的代码。 –

+0

这样做。真棒。非常感谢。 –

相关问题