如何在外部循环中引用控件声明 [英] How to refer control declare outside loop

查看:86
本文介绍了如何在外部循环中引用控件声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
很高兴得到您的建议.我告诉你,我尝试了以下代码

Hello friends,
Glad to back with get suggestion from u. Let me tell you, I tried out following code

Private Sub createDCList(ByVal objDT As DataTable)
        Dim row As DataRow
        Dim intIndex As Integer = 0
        Dim pnlCheckList As New Panel
        pnlCheckList.Direction = ContentDirection.LeftToRight   ''POSITIONING OF CONTROLS IN PANEL
        pnlCheckList = Page.FindControl("pnlJCList")
        For Each row In objDT.Rows
            Dim lblHeading As New Label
            lblHeading.Text = row.Item("CheckListName").ToString() & ":"
            lblHeading.ID = "lblHeading_" & intIndex
            pnlCheckList.Controls.Add(lblHeading)          ''Line Error
            intIndex = intIndex + 1
        Next
    End Sub



我在线路上遇到错误错误说:
对象引用nto设置为对象的实例

据我对FOR LOOP的了解,pnlCheckList不可用
知道如何引用它吗?

在此先感谢



I was getting error on line Error saying:
Object reference nto set to an instance of an object

As per my knowledge inside FOR LOOP, pnlCheckList is not available
Any idea how to refer it?

Thanks in advance

推荐答案

如果pnlJCList存在于Page中,则可以直接使用该对象.为什么要首先使用FindControl.

这应该可以工作:

If pnlJCList exists in the Page you use that object directly. Why are using FindControl in the first place.

This should work:

Private Sub createDCList(ByVal objDT As DataTable)
        Dim row As DataRow
        Dim intIndex As Integer = 0
        For Each row In objDT.Rows
            Dim lblHeading As New Label
            lblHeading.Text = row.Item("CheckListName").ToString() & ":"
            lblHeading.ID = "lblHeading_" & intIndex
            pnlJCList.Controls.Add(lblHeading)          'Line Error
            intIndex = intIndex + 1
        Next
End Sub



如果有帮助,请将其标记为答案



Mark it as answer if it is helpful


如果您查看 ^ ] Page.FindControl的文档,那么您会发现:
If you look at the MSDN[^] documentation for Page.FindControl, then you will find:
Return Value
Type: System.Web.UI.Control
The specified control, or null if the specified control does not exist.


检查您的姓名!


Check your names!


答案很简单,如果在运行代码时面板不存在,则无法访问它.您将不得不将运行此代码的那一刻转移到确实存在面板 的那一刻.
The answer is simple, if the panel doen''t exist at the time you run the code, you cannot access it. You''re going to have to move the moment of running this code to a cetrain moment where the panel does exist.


这篇关于如何在外部循环中引用控件声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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