简体   繁体   中英

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

I had a link button in my repeater.. lnkEdit and lnkDelete.. My questions is how I assign the selected value and delete it?

here's my code:

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>

Use the commandArgument in you link buttons. example:

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

Is CommandArgument on the button a viable option?

If not, you can try e.Item.DataItem to get a copy back of the data bound item you clicked on and then should be able to read the ID from that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM