Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

How to get the values from the Dynamically generated control creted in the wizard control vb.net

I am trying to get the values from the Dynamically generated control in the wizard control. I have tried using the Findcontrol with different parameters but it throws  Object reference not set to an instance of an object error. The strange thing is when i run the code without using the wizard control it works fine, but not with the wizard control.
Below is code for your reference.

Please, could you help me out.
Many Thanks.

aspx page
 
<form id="form1" runat="server">
    
    <div>
    
        <asp:Wizard ID="Wizard1" runat="server">
            <WizardSteps>
                <asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1">
                 Number: <asp:TextBox ID="txbNum" runat="server" />
        <asp:Button ID="Button2" runat="server" Text="Add" />
                   
        
        <asp:Table ID="tblAdd" runat="server" Width="788px" CellPadding="10" CellSpacing="0" Height="32px" >
            <asp:TableHeaderRow ID="TableHeaderRow1" runat="server" BackColor="#507CD1" ForeColor="White">
            <asp:TableHeaderCell ID="TableHeaderCell1" runat="server" HorizontalAlign="Left">Session title</asp:TableHeaderCell>
            <asp:TableHeaderCell ID="TableHeaderCell2" runat="server" HorizontalAlign="Left">Max Places</asp:TableHeaderCell>
          
            </asp:TableHeaderRow>
        </asp:Table>  
         
        <asp:Button ID="btnSubmit" runat="server" Text="Button"/>
        
                </asp:WizardStep>
              
            </WizardSteps>  
           
        </asp:Wizard>
       </div>
 
//code behind
 
 Dim maxRow As Integer = 1
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
 
 
        Dim _num As Integer = Int16.Parse(txbNum.Text)
 
        Dim viewState As Object = Me.ViewState(_num)
        createtext(_num)
        Button2.Enabled = False
        txbNum.Enabled = False
 
    End Sub
 
    Sub createtext(ByVal _num As Integer)
 
        If _num > 0 Then
 
            Dim txbArray As TextBox()() = New TextBox(_num - 1)() {}
            Dim drpArray As DropDownList()() = New DropDownList(_num - 1)() {}
            For i As Integer = 0 To _num - 1
                Dim tblRow As New TableRow()
                txbArray(i) = New TextBox(maxRow - 1) {}
                drpArray(i) = New DropDownList(maxRow - 1) {}
                For j As Integer = 0 To maxRow - 1
 
                    txbArray(i)(j) = New TextBox()
                    txbArray(i)(j).Width = 200
                    txbArray(i)(j).Height = 80
 
                    txbArray(i)(j).ID = "tbx" & i & j
                    txbArray(i)(j).TextMode = TextBoxMode.MultiLine
                    drpArray(i)(j) = New DropDownList
                    drpArray(i)(j).ID = "drp" & i & j
                    For k As Integer = 1 To 150
                        drpArray(i)(j).Items.Add(k)
                    Next
                    Dim tblCell As New TableCell()
 
                    tblCell.Controls.Add(txbArray(i)(j))
                    tblCell.BackColor = Drawing.Color.GhostWhite
                    Dim tblcell2 As New TableCell()
                    tblcell2.Controls.Add(drpArray(i)(j))
                    tblcell2.BackColor = Drawing.Color.GhostWhite
                    tblRow.Controls.Add(tblCell)
                    tblRow.Controls.Add(tblcell2)
 
                Next
                tblAdd.Controls.Add(tblRow)
            Next
        End If
    End Sub
 
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim _num As Integer = Int16.Parse(txbNum.Text)
        Button2.Visible = False
        txbNum.Visible = False
 
        If _num > 0 Then
 
            Response.Write("Submit result:" + "</br><table width=788px cellpadding=20 cellspacing=0>")
            Response.Write("<tr>")
            Response.Write("<td bgcolor=#507CD1> Session Title</td>")
            Response.Write("<td bgcolor=#507CD> Session Order</td>")
            Response.Write("<td bgcolor=#507CD> Max Places</td>")
            Response.Write("</tr>")
 
 
            For i As Integer = 0 To _num - 1
 
                Response.Write("<tr>")
                For j As Integer = 0 To maxRow - 1
                    Dim txbName As String = "tbx" & i & j
                    Dim drporder As String = "drp" & i & j
 
                    'insert into the table all the session detail
 
                    Dim a As TextBox = CType(tblAdd.Controls(0).Controls(0).FindControl(txbName), TextBox)
 
                    Response.Write(a.Text)
                    ' Dim textBoxRef As TextBox = CType(currentControl, TextBox)
 
                    ' Me.Response.Write((txbName & ": " & textBoxRef.Text & "<br>"))
                    Response.Write("<td bgcolor=ghostwhite width=200 height=80>" + Request.Form(txbName) + "</td>")
                    Response.Write("<td bgcolor=ghostwhite width=200 height=80>" & i + 1 & "</td>")
                    Response.Write("<td bgcolor=ghostwhite width=200 height=80>" + Request.Form(drporder) + "</td>")
 
 
                Next
                Response.Write("</tr>")
            Next
            Response.Write("</table>")
        End If
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
    End Sub
 
    Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick
 
    End Sub
    Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
 
        MyBase.OnLoad(e)
 
        Dim _num1 = ViewState("_num")
 
        createtext(_num1)
    End Sub

Open in new window

0
JEH
Asked:
JEH
  • 7
  • 5
  • 2
1 Solution
 
Bob LearnedCommented:
In order for FindControl to work, you need to either use the Unique ID, or call it from the lowest level naming container, such as Panel, <div>, <table>, etc.

Bob
0
 
JEHAuthor Commented:
I did try it for e.g
the table is within the wizard control so
dim a as table=ctype(wizard1.findcontrol("tbladd"),table)
dim b as textbox=ctype(a.findcontrol(txbname),textbox)
but it sill throws an error.

Could you please help me.
Many Thanks,
0
 
ventaurCommented:
If you don't want to bother figuring out the hierarchy, or if you don't know the hierarchy at runtime, use the function below to get your control. Note, it will only find the first control with that ID. Therefore, if you have 2 child controls with the same ID (but in different containers), it will only return the first one.
Public Function FindControlRecursively(startingControl As Control, controlToFindID As String, maxDepth As Integer) As Control
  ' Try to find the control inside the starting control.
  Dim ControlToFind As Control = startingControl.FindControl(controlToFindID)
  If ControlToFind Is Nothing Then
    If maxDepth > 0 Then
      ' Try to find the control inside any of the child controls of the starting control.
      For Each ChildControl As Control In startingControl.Controls
        ControlToFind = FindControlRecursively(ChildControl, controlToFindID, maxDepth - 1)
        If Not (ControlToFind Is Nothing) Then
          Exit For
        End If
      Next
    End If
  End If
  
  Return ControlToFind
End Function

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
JEHAuthor Commented:
Could you tell me how will I acces that value as it doen't throw the error but at the same time the value is blank.
Many Thanks,
0
 
Bob LearnedCommented:
When you view the rendered source in the browser, what is the unique ID that is assigned to the control?

Bob
0
 
ventaurCommented:

Dim txtName As TextBox = TryCast(FindControlRecursively(Wizard1, "txbname", 10), TextBox)

Open in new window

0
 
JEHAuthor Commented:
to Bob- The control name which i can see on the view source is
Wizard1_tbx00
I tried to use wizard 1_tbx and then the array variables
 For j As Integer = 0 To maxRow - 1
                    Dim txbName As String = "Wizard1_tbx" & i & j

but doesn't seem to be working

to Ventatur:
Nop it still doesn't return any value.
Many Thanks
0
 
JEHAuthor Commented:
Hello,
If it is the problem of find control how does it works without the wizard control.
Below is my code behind which works fine
  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
 
 
        Dim _num As Integer = Int16.Parse(txbNum.Text)
        createtext(_num)
        Button2.Enabled = False
        txbNum.Enabled = False
 
    End Sub
 
    Sub createtext(ByVal _num As Integer)
 
        If _num > 0 Then
 
            Dim txbArray As TextBox()() = New TextBox(_num - 1)() {}
            Dim drpArray As DropDownList()() = New DropDownList(_num - 1)() {}
            For i As Integer = 0 To _num - 1
                Dim tblRow As New TableRow()
                txbArray(i) = New TextBox(maxRow - 1) {}
                drpArray(i) = New DropDownList(maxRow - 1) {}
                For j As Integer = 0 To maxRow - 1
 
                    txbArray(i)(j) = New TextBox()
                    txbArray(i)(j).Width = 200
                    txbArray(i)(j).Height = 80
 
                    txbArray(i)(j).ID = "tbx" & i & j
                    txbArray(i)(j).TextMode = TextBoxMode.MultiLine
                    drpArray(i)(j) = New DropDownList
                    drpArray(i)(j).ID = "drp" & i & j
                    For k As Integer = 1 To 150
                        drpArray(i)(j).Items.Add(k)
                    Next
                    Dim tblCell As New TableCell()
 
                    tblCell.Controls.Add(txbArray(i)(j))
                    tblCell.BackColor = Drawing.Color.GhostWhite
                    Dim tblcell2 As New TableCell()
                    tblcell2.Controls.Add(drpArray(i)(j))
                    tblcell2.BackColor = Drawing.Color.GhostWhite
                    tblRow.Controls.Add(tblCell)
                    tblRow.Controls.Add(tblcell2)
 
                Next
                tblAdd.Controls.Add(tblRow)
            Next
        End If
    End Sub
 
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim _num As Integer = Int16.Parse(txbNum.Text)
        Button2.Visible = False
        txbNum.Visible = False
 
        If _num > 0 Then
 
            Response.Write("Submit result:" + "</br><table width=788px cellpadding=20 cellspacing=0>")
            Response.Write("<tr>")
            Response.Write("<td bgcolor=#507CD1> Session Title</td>")
            Response.Write("<td bgcolor=#507CD> Session Order</td>")
            Response.Write("<td bgcolor=#507CD> Max Places</td>")
            Response.Write("</tr>")
 
 
            For i As Integer = 0 To _num - 1
 
                Response.Write("<tr>")
                For j As Integer = 0 To maxRow - 1
                    Dim txbName As String = "tbx" & i & j
                    Dim drporder As String = "drp" & i & j
                    'Dim a As TextBox = CType(txbName, TextBox)
 
                    'insert into the table all the session detail
 
                    'Dim a As TextBox = CType(Wizard1.FindControl(txbName), TextBox)
 
                    'Response.Write(a.Text)
                    ' Dim textBoxRef As TextBox = CType(currentControl, TextBox)
 
                    ' Me.Response.Write((txbName & ": " & textBoxRef.Text & "<br>"))
                    Response.Write("<td bgcolor=ghostwhite width=200 height=80>" & Request.Form(txbName) & "</td>")
                    Response.Write("<td bgcolor=ghostwhite width=200 height=80>" & i + 1 & "</td>")
                    Response.Write("<td bgcolor=ghostwhite width=200 height=80>" + Request.Form(drporder) + "</td>")
 
 
                Next
                Response.Write("</tr>")
            Next
            Response.Write("</table>")
        End If
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
    End Sub
 
End Class

Open in new window

0
 
ventaurCommented:
Because the wizard control will only reference controls in the current step being displayed. I think that may be the issue. In which case, you would need to base everything off of the actual wizard step.
Dim txtName As TextBox = TryCast(FindControlRecursively(WizardStep1, txbname, 10), TextBox)
 
' -- or --
 
' If access the table "tblAdd" is not generating an error.
Dim a As TextBox = TryCast(tblAdd.Rows(i).Cells(j).FindControl(txbName), TextBox)

Open in new window

0
 
JEHAuthor Commented:
Hello
I tried both the solutions but doesn't seem to work.
Could you please help.
Many Thanks
0
 
ventaurCommented:
I'll have to recreated your page to figure it out. Is your original post's code the gist of it?
0
 
JEHAuthor Commented:
Yes, that's the original code in my veru first post.
Many Thanks.
0
 
ventaurCommented:
Okay. There was a bit to update. Firstly, please note that you cannot use Response.Write and expect to be able to get at any of your server controls afterwards. Using Response.Write wipes out everything from the page before writing. However, if that is what you want to do, so be it.

I moved some things from the OnInit method to the Page_Load event handler to get things working. The dynamic controls were not being re-created on post back before the btnSubmit_Click handler was executing. Therefore, your FindControl attempts were failing due to no controls in the table.

See the snippet below for the full code-behind file. No HTML was changed; only the code-behind. I left a single comment in the code to show you where you can access the text box that is being found now by using the wizard control directly.
Dim maxRow As Integer = 1
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If txbNum.Text.Trim() <> String.Empty Then
    Dim _num As Integer = Int16.Parse(txbNum.Text)
    createtext(_num)
  End If
End Sub
 
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  Button2.Enabled = False
  txbNum.Enabled = False
End Sub
 
Sub createtext(ByVal _num As Integer)
  If _num > 0 Then
    Dim txbArray As TextBox()() = New TextBox(_num - 1)() {}
    Dim drpArray As DropDownList()() = New DropDownList(_num - 1)() {}
    For i As Integer = 0 To _num - 1
      Dim tblRow As New TableRow()
      txbArray(i) = New TextBox(maxRow - 1) {}
      drpArray(i) = New DropDownList(maxRow - 1) {}
      For j As Integer = 0 To maxRow - 1
        txbArray(i)(j) = New TextBox()
        txbArray(i)(j).Width = 200
        txbArray(i)(j).Height = 80
 
        txbArray(i)(j).ID = "tbx" & i & j
        txbArray(i)(j).TextMode = TextBoxMode.MultiLine
        drpArray(i)(j) = New DropDownList
        drpArray(i)(j).ID = "drp" & i & j
        For k As Integer = 1 To 150
          drpArray(i)(j).Items.Add(k)
        Next
        Dim tblCell As New TableCell()
 
        tblCell.Controls.Add(txbArray(i)(j))
        tblCell.BackColor = Drawing.Color.GhostWhite
        Dim tblcell2 As New TableCell()
        tblcell2.Controls.Add(drpArray(i)(j))
        tblcell2.BackColor = Drawing.Color.GhostWhite
        tblRow.Controls.Add(tblCell)
        tblRow.Controls.Add(tblcell2)
      Next
      tblAdd.Controls.Add(tblRow)
    Next
  End If
End Sub
 
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
  Dim _num As Integer = Int16.Parse(txbNum.Text)
  Button2.Visible = False
  txbNum.Visible = False
 
  If _num > 0 Then
    For i As Integer = 0 To _num - 1
      For j As Integer = 0 To maxRow - 1
        Dim txbName As String = "tbx" & i & j
        Dim drporder As String = "drp" & i & j
 
        Dim a As TextBox = TryCast(Wizard1.FindControl(txbName), TextBox)
        If Not (a Is Nothing) Then
          ' Do what you need to do with the text box, "a".
        End If
      Next
    Next
  End If
End Sub

Open in new window

0
 
JEHAuthor Commented:
Hello,
The code works fantastic, i have formatted as per my need.
Thanks very much again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now