簡體   English   中英

如何使用linkbutton獲取在轉發器中選擇的值?

[英]How to get the value of selected in repeater using linkbutton?

我的轉發器中有一個鏈接按鈕.. lnkEdit和lnkDelete ..我的問題是如何分配所選值並刪除它?

這是我的代碼:

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>

在鏈接按鈕中使用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;
        }
}

按鈕上的CommandArgument是可行的選項嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM