Changing an asp:label text inside a repeater
I have this label inside a repeater
<asp:Label id="lblsub" runat=server text="sdds" />
I am trying to change the text of this label,this is the code behind
For Each It开发者_开发百科em As RepeaterItem In dg.Items
Dim l As New label
l = CType(Item.FindControl("lblsub"), System.Web.UI.WebControls.Label)
l.text="test"
Next
unfortunately this code doesn't work for me ,the text value doesn't change ,so I will be very happy to some help here
thanks
See my answer in this question. Believe it will solve your issue.
Getting the databound value from repeater control in asp.net
EDIT
First of all, make sure you always use quotes in your markup:
<asp:Label ID="lblsub" runat="server" Text="sdds" />
Try something like this in your code behind. Forgive me if my VB is a little rusty:
For Each Item As RepeaterItem in dg.Items
Dim l As Label = CType(Item.FindControl("lblsub"), Label)
l.Text = "test"
Next
There were just a couple little problems with syntax. I don't know if they are causing your issue, but best get the easy stuff out of the way first.
When is that bit of code being ran?
Make sure you are not rebinding the repeater afterwards by doing the following in your Page_Load
:
Sub Page_load
If not Page.IsPostBack Then
BindRepeater()
End If
End Sub
If you need to do this bit of code every time the repeater is bound, then put your code into the DataBound event:
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
I haven't got any programming software with me so unsure of the syntax
C# code sample (it should be inside method or page_load)
foreach (RepeaterItem rt in MyRepeater.Items)
{
Label l = ((Label)rt.FindControl("lblPrice"));
l.Text = "200";
}
精彩评论