C# 在单击事件ASP.NET时获取ListView绑定数据

C# 在单击事件ASP.NET时获取ListView绑定数据,c#,asp.net,listview,C#,Asp.net,Listview,我试图获取事件中绑定到EditItemTemplate的数据的值。 ListView id是lvProducts,如何在UpdateButton\u Click方法中引用项目的数据 这是编辑项模板: <EditItemTemplate> <tr> <td class="narrow"><asp:Label runat="server" Text='&

我试图获取事件中绑定到EditItemTemplate的数据的值。 ListView id是lvProducts,如何在UpdateButton\u Click方法中引用项目的数据

这是编辑项模板:

<EditItemTemplate>
                         <tr>
                            <td class="narrow"><asp:Label runat="server" Text='<%#Bind("ProductID")%>' ID="idLabel"></asp:Label></td>
                            <td class="wide"><asp:Textbox runat="server" Text='<%#Bind("ProductTitle")%>' ID="nameEdit"></asp:Textbox><span style="color:red">*</span></td>
                            <td class="narrow"><asp:Textbox runat="server" Text='<%#Bind("StockAmount")%>' TextMode="Number" ID="Label4"></asp:Textbox></td>
                            <td class="narrow"><asp:Label runat="server" Text='<%#Bind("AvailableAmount")%>' ID="faxEdit"></asp:Label></td>
                            <td class="narrow"><asp:Checkbox runat="server" Checked='<%#Bind("ProductStatus")%>' Enabled="true" ID="Label6"></asp:Checkbox></td>
                            <td class="wide" style="text-align:center;"> <asp:LinkButton ID="UpdateButton" ToolTip='<%#Eval("ProductID")%>' OnClick="UpdateButton_Click" runat="server" CommandName="Update" Text="Save" />&nbsp;
                                 <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                            </td>
                        </tr>
                    </EditItemTemplate>

我没有测试过,但应该是这样的:

     protected void UpdateButton_Click(object sender, EventArgs e)
{
    if (e.CommandName == "Cancel")
    {
        ListViewDataItem lvd = (ListViewDataItem)((Control)e.CommandSource).NamingContainer;

        //Same two lines for each value
        Label ID = lvd.FindControl("idLabel") as Label;
        string id = id.ToString();
    }

}

将按钮的CommandArgument设置为ProductId(假设这是主键)


谢谢,只需一条评论:我将第二行改为:ListViewDataItem lvd=lvProducts.Items[I]这一行也很好,我也可以这样使用。谢谢
     protected void UpdateButton_Click(object sender, EventArgs e)
{
    if (e.CommandName == "Cancel")
    {
        ListViewDataItem lvd = (ListViewDataItem)((Control)e.CommandSource).NamingContainer;

        //Same two lines for each value
        Label ID = lvd.FindControl("idLabel") as Label;
        string id = id.ToString();
    }

}
<asp:LinkButton ID="UpdateButton" ToolTip='<%#Eval("ProductID")%>' OnClick="UpdateButton_Click" runat="server" CommandArgument='<%#Eval("ProductID")%>' CommandName="Update" Text="Save" />
protected void UpdateButton_Click(object sender, EventArgs e)
{
    LinkButton btn=sender as LinkButton;
    string productid=btn.CommandArgument;
    //now update your data
}