2013-04-14 21 views
0

我需要設置一個標籤文本是listview和listview在登錄模板中。 我無法設置標籤的值。這是下面的代碼。設置登錄模板中的listview控件中的標籤文本

ListView ListView1 = (ListView)LoginView.FindControl("ListView1"); 

for (int i = 0; i < ListView1.Controls.Count; i++) 
{ 
    Label someLabel = (Label)ListView1.Controls[i].FindControl("nItemsId"); 
    if (someLabel != null) 
     someLabel.Text = dt.Rows.Count.ToString(); 
} 

回答

0

所以我認爲你需要使用ListView.ItemCreated事件來實現這一

protected void LV_ItemCreated(object sender, ListViewItemEventArgs e) 
{ 
    // Retrieve the current item. 
    ListViewItem item = e.Item; 

    // Verify if the item is a data item. 
    if (item.ItemType == ListViewItemType.DataItem) 
    { 
    Label someLabel = (Label)ListView1.Controls[i].FindControl("nItemsId"); 
    if (someLabel != null) 
     someLabel.Text = dt.Rows.Count.ToString(); 
    } 
} 

要使用它,改變您的標記聲明的事件處理程序是這樣的。

<asp:ListView OnItemCreated="LV_ItemCreated" /> 
+0

我在頁面加載時需要它,該怎麼做? –

+0

當你將在Page_Load上綁定你的列表視圖。此事件將自動觸發,以在listview中創建項目。 – Sachin

+0

是的,但我有另一種方法保護無效On_Select_Item(對象發件人,ListViewCommandEventArgs e){}。我需要調用你從這個方法建議的上述方法..我可以如何發送ListViewItemEventArgs參數? –

相關問題