find control in listview

点点圈 提交于 2019-12-12 02:25:15

问题


By pressing button in GridView i need to find control in Listview.

<ItemTemplate>
<td>

    <asp:Label ID="lblMarketLVBalanceHeader" runat="server" Text="Balance: "></asp:Label>
</td>


<td>
    <asp:Label ID="lblMarketLVBalanceValue" runat="server" Text='<%# Bind("Money", "{0:####}$") %>'></asp:Label>

</td>

</ItemTemplate>

Code Behind:

protected void GVMarketItems_RowCommand(object sender, GridViewCommandEventArgs e)
    {
if (e.CommandName.Equals("Buy"))
        {  GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
         /*  string itemID = row.Cells[0].Text;
           string itemName = row.Cells[1].Text;
           string itemCostStr = row.Cells[3].Text; */
 string curBalanceStr = ((Label)LVMarketBalanceShow.FindControl("lblMarketLVBalanceValue")).Text;
}

Code seems find, but i have "Object reference not set to an instance of an object" when i'm trying to Find control.

string curBalanceStr = ((Label)LVMarketBalanceShow.FindControl("lblMarketLVBalanceValue")).Text;

Did the same to DetailsView and it was Ok.. What's wrong with ListView?

UPD: Trying to get first fow of listview

ListViewDataItem curBalanceLV = LVMarketBalanceShow.Items[0];
       string curBalanceStr =((Label)curBalanceLV.FindControl("lblMarketLVBalanceValue")).Text;

But getting an error "Index was out of range."


回答1:


I think you want to find the control within the specific row.

string curBalanceStr = ((Label)row.FindControl("lblMarketLVBalanceValue")).Text



回答2:


Did the same to DetailsView and it was Ok.. What's wrong with ListView?

A DetailsView is used to tackle with one row at a time so you can directly call FindControl() on the detailsview but gridview and listview are meant to display multiple records and your markup you define inside <ItemTemplate /> is just a template for each row. You'll be able to find the controls that you define in the template inside of each row.



来源:https://stackoverflow.com/questions/7341838/find-control-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!