如何使用linkbutton获取在转发器中选择的值?

时间:2011-04-25 06:47:49

标签: c# asp.net repeater

我的转发器中有一个链接按钮.. 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>

2 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:1)

按钮上的CommandArgument是否可行?

如果没有,您可以尝试e.Item.DataItem获取您单击的数据绑定项的副本,然后应该能够从中读取ID吗?