2011-04-25 61 views
2

我在我的轉發器中有一個鏈接按鈕.. lnkEdit和lnkDelete ..我的問題是我如何分配選定的值並刪除它?如何使用linkbutton獲取中繼器中選定的值?

這裏是我的代碼:

protected void rptrInsurance_ItemCommand(object source, RepeaterCommandEventArgs e) 
{ 

    try 
    { 

     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
     { 
      switch (e.CommandName) 
      { 
       case "Delete": 
        { 
         HCSInsurance oInsuranceDelete = new HCSInsurance(); 
         Insurance oInsurance = new Insurance(); 
         // oInsurance.InsuranceCode.ID = "2"; 
         oInsuranceDelete.DeleteInsurance(oInsurance); 
        } 
        break; 
       case "Edit": 
        { 

        } 
        break; 
       default: 
        { 

        } 
        break; 
      } 
     } 

    } 

    catch (Exception ex) 
    { 

    } 

} 

asp.net

<asp:LinkButton ID="lnkEdit" runat="server" onclick="lnkEdit_Click" CommandName="Edit">Edit</asp:LinkButton>&nbsp;<asp:LinkButton 
ID="lnkDelete" runat="server" onclick="lnkDelete_Click" CommandName="Delete" OnClientClick="if (!confirm('Are you sure do you want to delelte it?')) return false;">Delete</asp:LinkButton> 
      </td> 

回答

7

在你的鏈接按鈕中使用commandArgument。例如:

<asp:Repeater ID="rptrInsurance" runat="server" 
    OnItemCommand="rptrInsurance_ItemCommand"> 
    <ItemTemplate> 
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit" CommandArgument='<%# Eval("ID") %>'>Edit</asp:LinkButton>&nbsp; 
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>' OnClientClick="if (!confirm('Are you sure do you want to delelte it?')) return false;">Delete</asp:LinkButton> 
    </ItemTemplate> 
</asp:Repeater> 


protected void rptrInsurance_ItemCommand(object source, RepeaterCommandEventArgs e) 
{ 
    switch (e.CommandName) 
     { 
      case "Delete": 
       { 
        HCSInsurance oInsuranceDelete = new HCSInsurance(); 
        Insurance oInsurance = new Insurance(); 
        oInsurance.InsuranceCode.ID = e.CommandArgument; 
        oInsuranceDelete.DeleteInsurance(oInsurance); 
       } 
       break; 
      case "Edit": 
       { 

       } 
       break; 
      default: 
       { 

       } 
       break; 
     } 
} 
+0

我更新了我的代碼以上..請檢查它 – 2011-04-25 07:07:58

+0

感謝您的代碼。非常有幫助。 – 2017-04-22 11:34:50

1

CommandArgument按鈕一個可行的選擇?

如果不是,您可以嘗試e.Item.DataItem以獲取您單擊的數據綁定項的副本,然後應該能夠從中讀取該ID?

相關問題