Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2007-11-19
14
Medium Priority
?
255 Views
Last Modified: 2010-04-23
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
Comment
Question by:JEH
  • 7
  • 5
  • 2
14 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20312293
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
 

Author Comment

by:JEH
ID: 20312508
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
 
LVL 6

Expert Comment

by:ventaur
ID: 20312546
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:JEH
ID: 20312635
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20312672
When you view the rendered source in the browser, what is the unique ID that is assigned to the control?

Bob
0
 
LVL 6

Expert Comment

by:ventaur
ID: 20312733

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

Open in new window

0
 

Author Comment

by:JEH
ID: 20312901
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
 

Author Comment

by:JEH
ID: 20313064
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
 
LVL 6

Expert Comment

by:ventaur
ID: 20313118
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
 

Author Comment

by:JEH
ID: 20318299
Hello
I tried both the solutions but doesn't seem to work.
Could you please help.
Many Thanks
0
 
LVL 6

Expert Comment

by:ventaur
ID: 20319708
I'll have to recreated your page to figure it out. Is your original post's code the gist of it?
0
 

Author Comment

by:JEH
ID: 20319741
Yes, that's the original code in my veru first post.
Many Thanks.
0
 
LVL 6

Accepted Solution

by:
ventaur earned 1000 total points
ID: 20320525
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
 

Author Comment

by:JEH
ID: 20326290
Hello,
The code works fantastic, i have formatted as per my need.
Thanks very much again.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question