FindControl 未在向导控件中找到动态添加的用户控件

发布于 2024-08-04 01:55:16 字数 2175 浏览 3 评论 0原文

我有一个向导控件,我在其中添加一个包含一个简单表格的用户控件 一些输入字段基于用户输入他们有多少个孩子。例如:你有多少个孩子,所以我根据该循环添加用户控件 ascx 这进入了我的向导的第 5 步,它也在母版页中。

然后,我使用 findcontrol 尝试获取这些输入框,以便我可以将数据保存到我的数据库中,findcontrol 始终为空,即使用户控件可见并在回发后在页面加载时重新创建。

非常感谢任何帮助。 找到控制按钮: Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 处理 Button1.Click

    Dim numbchildren As Integer = CInt(Howmanychildren.Text)

    For i As Integer = 1 To numbchildren - 1

        Dim textbox As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Minor_1_Child_Name"), TextBox)
        'Dim textbox2 As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Howmanychildren"), TextBox)


        If textbox IsNot Nothing Then

            Response.Write("Found TextBox1 <br>")
            Dim val As String = textbox.Text
            Response.Write(val & "<br>")

        Else
            Response.Write("not found" & "<br>")
        End If

        ' Insert into DB
        'SaveValueToDatabase(val)
    Next

End Sub

用户控件在下拉列表中添加的功能:

Protected Sub Doyouhavechildren_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) 处理 Doyouhavechildren.SelectedIndexChanged

    Dim numbchildren As Integer = CInt(Howmanychildren.Text)
    Dim cnt As Integer = 1


    'Panel1.Controls.Clear()

    Select Case Doyouhavechildren.SelectedIndex
        Case 0



            ViewState.Add("Doyouhavechildren", numbchildren)


            Do While cnt <= numbchildren

                Dim uc As Web.UI.UserControl = DirectCast(Page.LoadControl("MinorChild.ascx"), Web.UI.UserControl)
                uc.ID = "Minor_" + cnt.ToString()
                Wizard1.ActiveStep.Controls.Add(uc)



                cnt = cnt + 1 
            Loop
            Exit Select
        Case 1

            Exit Select
    End Select

End Sub

用户控件:

<%@ 控制语言="VB" AutoEventWireup="false" CodeFile="MinorChild.ascx.vb" Inherits="MinorChild" %>

姓名

年龄

SS#

出生日期

查找控件在静态的 howmanychildren 字段中工作

I have a wizard control in wich I am adding a user control containing a simple table with
some input fields based on users entry of how many children they have. ex: how many kids do you have so I add the user control ascx based on that loop
that goes into step 5 of my wizard wich is also in a masterpage.

I then use findcontrol to atttempt the get to those input boxes so i can save the data into my db, findcontrol allway comes up null, even though the user control in visable and recreated on page load after post back.

any help greatly appreciated.
find control button:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim numbchildren As Integer = CInt(Howmanychildren.Text)

    For i As Integer = 1 To numbchildren - 1

        Dim textbox As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Minor_1_Child_Name"), TextBox)
        'Dim textbox2 As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Howmanychildren"), TextBox)


        If textbox IsNot Nothing Then

            Response.Write("Found TextBox1 <br>")
            Dim val As String = textbox.Text
            Response.Write(val & "<br>")

        Else
            Response.Write("not found" & "<br>")
        End If

        ' Insert into DB
        'SaveValueToDatabase(val)
    Next

End Sub

user control added function on dropdown :

Protected Sub Doyouhavechildren_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Doyouhavechildren.SelectedIndexChanged

    Dim numbchildren As Integer = CInt(Howmanychildren.Text)
    Dim cnt As Integer = 1


    'Panel1.Controls.Clear()

    Select Case Doyouhavechildren.SelectedIndex
        Case 0



            ViewState.Add("Doyouhavechildren", numbchildren)


            Do While cnt <= numbchildren

                Dim uc As Web.UI.UserControl = DirectCast(Page.LoadControl("MinorChild.ascx"), Web.UI.UserControl)
                uc.ID = "Minor_" + cnt.ToString()
                Wizard1.ActiveStep.Controls.Add(uc)



                cnt = cnt + 1 
            Loop
            Exit Select
        Case 1

            Exit Select
    End Select

End Sub

user control:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="MinorChild.ascx.vb" Inherits="MinorChild" %>

Name

Age

SS#

DOB

the find control works in the howmanychildren field that is static

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

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

发布评论

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

评论(1

绾颜 2024-08-11 01:55:16

我自己想出来了
基本上,你必须引用容器,这就是其他地方的每个人所说的,但我一直忽略答案
正确的代码是

Dim textbox As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Minor_1").FindControl("Child_Name"), TextBox)

您必须首先引用用户控件名称,然后在其中进行搜索,即使客户端源代码具有欺骗性。

I figured it out myself
basicly, you have to referance the container, thats what everyone everywhere else where saying but I kept ignoring the answer
the correct code is

Dim textbox As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Minor_1").FindControl("Child_Name"), TextBox)

you have to referance the user control name first then search within it , even though the client source is disceptive.

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