使用以编程方式创建的标签 [英] Use a label that was created programatically

查看:115
本文介绍了使用以编程方式创建的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

******** ASP.NET 4.0,VB.NET 2010 ************
大家好,

我有一个页面以编程方式创建标签.每个标签都会根据getcustomer方法初始化为一个值-请参见下面的代码.
这是我的问题,如何引用创建的标签来使用它?

例如,假设我要在msgbox中显示其值.
示例:

******** ASP.NET 4.0, VB.NET 2010 ************
Hi all,

I have a page that creates labels programmatically. Each label initializes to a value based on the getcustomer method - see my code below.
Here is my problem, how can I reference the created label to use it?

For example, lets say I want to show it''s value in a msgbox.
Example:

msgbox(label1.text) 'Im get an error when I use label1


 Dim i As Integer
 Dim numlabels As Integer

 numlabels = 6
 For i = 1 To numlabels
     Dim myLabel As Label = New Label()
     myLabel.Text = "Value " & getcustomer()
     myLabel.ID = "Label" & i
     Panel1.Controls.Add(myLabel)

     Dim spacer As LiteralControl = New LiteralControl("<br />")
    Panel1.Controls.Add(spacer)
 Next

推荐答案

^ ]

因为您是动态添加控件的,所以您不能直接访问它.下面的示例显示如何通过给定的ID访问此类控件. (直接从上面的链接中获取)

FindControl function[^]

because you added the control dynamically you can''t directly access it. The example below shows how to access such a control by the ID that you have given it. (this was taken directly from the link above)

' This occurs for Repeater1 and originates from LinkButton onClick.
  Protected Sub OnMyCommand1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
    Dim b As LinkButton = sender
    If Not (b Is Nothing) Then
      Dim c As Label = CType(b.Parent.FindControl("foo"), Label)
      If Not (c Is Nothing) Then
        c.Text = "text changed in handler"
        c.ForeColor = System.Drawing.Color.Green
      End If
    End If

  End Sub


感谢您的回答. ..它很棒..我有一个后续问题..

我正在使用
创建一个在线考试页面 动态创建的30个单选按钮(在运行期间).
这是我的问题,如何获取每个单选按钮的click事件并将其标记在我的方法中,该方法将检查是否需要跳过或转义下一个问题.

样本:
如果即时通讯在问题10中并且如果我回答=是",它将把我重定向至问题15,否则将转到下一个问题,否则什么也没发生
thanks for your answer...its brilliant.. I have a follow up question..

I am creating an online exam page with
30 radiobuttons that created dynamically(during run time).
Here is my problem, how will I get the click event of each radiobutton and tag it in my method that will check if the next question is needed to be jump or escaped.

sample :
If im in question 10 and if I answer = "Yes" it will redirect me to Question 15 else it go to the next question or nothing happen


这篇关于使用以编程方式创建的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆