简体   繁体   English

如何在行中检查listview的datakeyvalue

[英]How to get datakeyvalue of listview in row checked

I want when I click button checkbox i get data key value in row have checkbox is checked. 我想当我点击按钮复选框我得到行中的数据键值有复选框被选中。 Anyone have idea 任何人都有想法

<asp:ListView ID="rlv_CouponCode" runat="server" DataKeyNames="ID" DataSourceID="sds_CouponCode" ItemPlaceholderID="itemPlace">
<ItemTemplate>
    <tr>
        <td>
            <asp:CheckBox ID="Checkbox1" runat="server" />
        </td>
        <td>
            <asp:TextBox Enabled="false" ID="Checkbox" runat="server"
                Text='<%#Eval("Code") %>' />
        </td>
        <td>
            <asp:TextBox Enabled="false" ID="NameLabel" runat="server"
                Text='<%#Eval("Discount") %>' />
        </td>
    </tr>
</ItemTemplate>

Here my code behind for Button1: 这里我的代码背后是Button1:

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (ListViewItem item in rlv_CouponCode.Items)
    {
        CheckBox cb = (CheckBox)item.FindControl("Checkbox1");
        if (cb.Checked)
        {
           //
        }
    }
}

Give your checkbox an ID that is the Id of the records it is displaying. 为您的复选框提供一个ID,该ID是其显示的记录的ID。 This way each checkbox will have a unique ID that you can use to process the data. 这样,每个复选框都有一个唯一的ID,您可以使用它来处理数据。 For example if your data has CouponID then: 例如,如果您的数据有CouponID那么:

<asp:CheckBox ID='<%#Eval("CouponID") %>' runat="server" />

On the server side get it using that ID and see if it has been checked. 在服务器端使用该ID获取它,看看它是否已被检查。

You have have set DataKeyNames , so you can access those. 您已设置DataKeyNames ,因此您可以访问这些。

for (int i = 0; i < rlv_CouponCode.Items.Count; i++)
{
    CheckBox cb = (CheckBox)rlv_CouponCode.Items[i].FindControl("Checkbox1");

    if (cb.Checked)
    {
        int id = Convert.ToInt32(rlv_CouponCode.DataKeys[i].Values[0]);    
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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