更改转发器内的 asp:label 文本

发布于 2024-11-29 21:16:25 字数 394 浏览 2 评论 0原文

我在中继器中有这个标签

<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 

,不幸的是,此代码对我不起作用,文本值不会改变,所以我将非常乐意提供帮助在这里

谢谢

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 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 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

王权女流氓 2024-12-06 21:16:25

请参阅我在这个问题中的回答。相信它会解决您的问题。

从转发器控件中获取数据绑定值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

语法上只有一些小问题。我不知道它们是否导致了您的问题,但最好先解决简单的问题。

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.

薄荷→糖丶微凉 2024-12-06 21:16:25

那段代码什么时候运行?

通过在 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

:我没有带任何编程软件,所以不确定语法

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

荒路情人 2024-12-06 21:16:25

C# 代码示例(应该在方法或 page_load 内部)

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

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";
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文