2011-08-16 41 views
2

我有一箇中繼器更改一個asp:中繼

<asp:Label id="lblsub" runat=server text="sdds" /> 

我試圖改變這個標籤的文本內這個標籤內標籤的文字,這是背後

For Each Item As RepeaterItem In dg.Items 
    Dim l As New label 
     l = CType(Item.FindControl("lblsub"), System.Web.UI.WebControls.Label) 
     l.text="test" 
    Next 

不幸的是,代碼此代碼不適用於我,文本值不會改變,所以我會很樂意在這裏提供一些幫助

謝謝

回答

1

看到我在這個問題的答案。相信它會解決你的問題。

Getting the databound value from repeater control in asp.net

編輯

首先,確保你總是在你的標記使用引號:

<asp:Label ID="lblsub" runat="server" Text="sdds" /> 

嘗試這樣的事情在後面的代碼。原諒我,如果我的VB有點生鏽:

For Each Item As RepeaterItem in dg.Items 
    Dim l As Label = CType(Item.FindControl("lblsub"), Label) 
    l.Text = "test" 
Next 

只有一些小問題的語法。我不知道他們是否會引起你的問題,但最好先把容易的東西先弄清楚。

+0

不知道我的朋友在你的答案是什麼,我沒有看到如何更改標籤 – baaroz

+0

我得到空你的代碼太文:System.NullReferenceException:對象未將引用設置爲對象的實例。 – baaroz

+0

你在什麼時候嘗試改變文本?你可以將代碼移動到ItemDataBound事件嗎?這種方法絕對有效,所以一定還有其他的事情要做。 –

2

這段代碼是在什麼時候運行的?

確保你沒有做你Page_Load以下之後重新綁定中繼器:

Sub Page_load 
    If not Page.IsPostBack Then 
     BindRepeater() 
    End If 
End Sub 

如果你需要每個中繼勢必時間做到這一點位的代碼,然後把你的代碼進入DataBound事件:

Protected Sub dg_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles dg.ItemDataBound 
    Select Case e.item.itemtype 
     Case ListItemType.Item, ListItemType.AlternatingItem 
    Dim l As Label = e.Item.FindControl("lblsub") 
    l.Text = "foo" 
    End Select 
End Sub 

我沒有任何編程軟件對我是如此不確定的語法

+0

我試過你的代碼,但我得到null:System.NullReferenceException:對象引用未設置爲對象的實例。 – baaroz

+0

我已經對代碼進行了更改,以避免包含頁眉/頁腳,我認爲這是造成空引用異常的原因 – Curt

0

C#代碼示例(它應該是我n面法的Page_Load)

foreach (RepeaterItem rt in MyRepeater.Items) 
      { 
       Label l = ((Label)rt.FindControl("lblPrice")); 
       l.Text = "200"; 
      }