2012-02-16 31 views
5

Tôi muốn nhắc người dùng xác nhận khi anh ta cố gắng xóa bản ghi trong chế độ xem chi tiết? Tôi có lệnh được gửi trong đó showDeletebutton được đặt thành true.cách thêm lời nhắc xác nhận xóa cho trường lệnh trong chế độ xem chi tiết?

Tôi đã tìm cách thực hiện xác nhận cho chế độ xem lưới, nhưng làm cách nào tôi có thể sửa đổi để khớp với chế độ xem chi tiết?

Code:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    // loop all data rows 
    foreach (DataControlFieldCell cell in e.Row.Cells) 
    { 
     // check all cells in one row 
     foreach (Control control in cell.Controls) 
     { 
      // Must use LinkButton here instead of ImageButton 
      // if you are having Links (not images) as the command button. 
      ImageButton button = control as ImageButton; 
      if (button != null && button.CommandName == "Delete") 
       // Add delete confirmation 
       button.OnClientClick = "if (!confirm('Are you sure " + 
         "you want to delete this record?')) return;"; 
     } 
    } 
} 
} 

Bất kỳ ai?

Trả lời

3

Tôi tìm thấy câu trả lời cho câu hỏi của tôi .

câu trả lời của tôi:

protected void DViewComputer_DataBound1(object sender, EventArgs e) 
{ 
    int noRow = DViewComputer.Rows.Count - 1;//get the no of record 

    if (noRow >0) 
    { 
     Button button = (Button)(DViewComputer.Rows[noRow].Cells[0].Controls[2]); 

     // Add delete confirmation 
     ((System.Web.UI.WebControls.Button)(button)).OnClientClick = "if (!confirm('Are you sure " + 
           "you want to delete this record?')) return;"; 

    } 
} 

Anyways cảm ơn cho kẻ giúp đỡ của bạn.

+0

Chỉ cần thêm rằng có một giải pháp jQuery có thể thu hút một số người trong câu trả lời cho [câu hỏi này] (http://stackoverflow.com/questions/8105556/how-do-i-explicitly-execute-default- action-from-jquery-event/8135983 # 8135983) – Joe

+0

Không hoạt động. Giải pháp này sẽ yêu cầu người dùng xác nhận họ muốn xóa bản ghi này khi họ nhấp vào hủy trong Chỉnh sửa hoặc Chế độ chèn. – RMuesi

+0

... xem giải pháp của tôi bên dưới để khắc phục vấn đề này. – RMuesi

8
 <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
     ..... 
      <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" /> 
      <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
       SortExpression="Quantity" /> 
      <asp:TemplateField ShowHeader="False"> 
       <ItemTemplate> 
        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
         CommandName="New" Text="New"></asp:LinkButton> 

        <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" 
         CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record');"></asp:LinkButton> 

       </ItemTemplate> 
      </asp:TemplateField> 
     </Fields> 
    </asp:DetailsView 

Điều này có thể được thực hiện dễ dàng trên mã đánh dấu. Tôi chỉ cần thêm đoạn code js đến tài sản OnClientClick của nút delete:

OnClientClick="return confirm('Are you sure you want to delete this record');" 

Hoặc nếu bạn muốn làm điều này trong các mã sau:

protected void DetailsView1_DataBound(object sender, EventArgs e) 
    { 
    LinkButton bttn = (LinkButton)DetailsView1.FindControl("lnkDelete"); 
    bttn.OnClientClick = "return confirm('Are you sure you want to delete this record!');"; 
    } 
+0

TemplateField bắt đầu ở đâu? –

+0

Tất nhiên, tôi bỏ qua các phần của mã trên mục đích, tuy nhiên, việc mở TemplateField cuối cùng và ItemTemplate có thể được đặt ngay trước nút liên kết đầu tiên. Xem bản cập nhật – Mubarek

1
foreach (Control control in cell.Controls) 
    { 
     // Must use LinkButton here instead of ImageButton 
     // if you are having Links (not images) as the command button. 
     ImageButton button = control as ImageButton; 
     if (button != null && button.CommandName == "Delete") 
      // Add delete confirmation 
      button.Attributes.Add("onclick","your javascript here"); 
    } 
0

này sửa chữa giải pháp của OP. Mã được dịch từ mã được tìm thấy tại đây: http://forums.aspfree.com/net-development-11/confirm-button-when-deleting-detailsview-120113-2.html

protected void dvEvent_DataBound(object sender, EventArgs e) 
{ 

    int commandRowIndex = dvEvent.Rows.Count - 1; 
    if (commandRowIndex > 0) 
    { 
     DetailsViewRow commandRow = dvEvent.Rows[commandRowIndex]; 
     DataControlFieldCell cell = (DataControlFieldCell)commandRow.Controls[0]; 

     foreach (Control ctrl in cell.Controls) 
     { 
      if (ctrl is ImageButton) 
      { 
       ImageButton ibt = (ImageButton)ctrl; 
       if (ibt.CommandName == "Delete") 
       { 
        ibt.ToolTip = "Click here to Delete"; 
        ibt.CommandName = "Delete"; 
        ibt.Attributes["onClick"] = "if (!confirm('Are you sure " + 
           "you want to delete this record?')) return;"; 
       } 
      } 
     } 
    } 
} 
Các vấn đề liên quan