• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 669
  • Last Modified:

ASP.NET Dynamic Control Adding - PostBack Issues

Hello,
I have an issue with an ASP.Net VB page I am trying to build.
The interface is for a Job Sheet so it has one hard coded box for each of the 3 main categories (Tasks, Materials Used, Outstanding Tasks).
OnloadViewNext to each of the first boxes it has a Add Text Box field. The code for this is in VB.net and is below.
  Protected Sub btnDescription(ByVal sender As Object, ByVal e As EventArgs)

        AddRows_tblDescriptionRowCounter(1)
        ViewState("tblDescriptionRowCounter") = (tblDescriptionRowCounter + 1)

    End Sub

    Public Sub AddRows_tblDescriptionRowCounter(ByVal rowstoadd As Integer)
        Dim i As Integer = 0
        Do While (i < rowstoadd)
            Dim row As TableRow = New TableRow
            Dim cell1 As TableCell = New TableCell
            Dim cell2 As TableCell = New TableCell
            Dim intWidth As New Unit(100, UnitType.Percentage)
            Dim intFontSize As FontSize = FontSize.XXLarge

            Dim txtTextBox As New TextBox()
            txtTextBox.BorderStyle = BorderStyle.Solid
            txtTextBox.Width = intWidth
            txtTextBox.TextMode = TextBoxMode.MultiLine
            txtTextBox.Font.Size = txtDescriptionOfJobOrTask_1.Font.Size
            txtTextBox.Height = txtDescriptionOfJobOrTask_1.Height
            cell1.Controls.Add(txtTextBox)
            row.Cells.Add(cell1)
            row.Cells.Add(cell2)
            tblDescriptionOfJobs.Rows.Add(row)
            i = (i + 1)
        Loop
    End Sub

Open in new window

I have attempted to put in a manual ID (and make it unique based on a number) for each TextBox but it always says “ID already on page”.
The issue I am getting is when someone enters data in this order (For example).
Enter Data in Description of Task
Press Add Row (ON Description)
Enter Data in Materials Used
Press Add Row (On Materials)
Enter Data in new box on the Description Tasks
Enter Data in new box on the Materials.
Press Add Row (on either Materials or Tasks).
It then puts the data in the wrong text boxes.
WrongDataIt should be like this
CorrectData
However when the user does enter data for that section and presses Add Row. Then once a blank box has appeared underneath, the user moves onto the next section and presses Add Row. All the data is being returned in the correct boxes.

I believe it has something to do about the ViewState controls and ID’s of the controls during a postback (not the best web developer but doing some research appears to point to this).
Any idea on how I can correct this error please?
Thanks,
Alex
0
BoltonWanderer
Asked:
BoltonWanderer
  • 4
  • 4
1 Solution
 
Bob LearnedCommented:
Are you re-adding the controls on post-back, before the ViewState is processed?
0
 
BoltonWandererAuthor Commented:
Hi,

The code I am using when the page is loaded is


 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If IsPostBack = False Then
         
            For x = 0 To 24 - 1
                ddlStartTime_Hour.Items.Add(PadWithStartZeros(2, x))
                ddlEndTime_Hour.Items.Add(PadWithStartZeros(2, x))
            Next

            For x = 0 To 60 - 1 Step 5
                ddlStartTime_Minute.Items.Add(PadWithStartZeros(2, x))
                ddlEndTime_Minute.Items.Add(PadWithStartZeros(2, x))
            Next

            'Load Next Job Sheet Ref
            txtCompanyID.Text = 0
            Try

                GetNextJobSheetRef()

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            bdpDate.SelectedDate = Now
            bdpClientDate.SelectedDate = Now
            bdpEmployeeDate.SelectedDate = Now
            ddlStartTime_Minute.Text = ddlStartTime_Minute.Items(0).ToString
            ddlEndTime_Minute.Text = ddlEndTime_Minute.Items(0).ToString

            ddlStartTime_Hour.Text = ddlStartTime_Hour.Items(Now.Hour).ToString
            ddlEndTime_Hour.Text = ddlEndTime_Hour.Items(Now.Hour).ToString
            LoadAccountRefDetails()
            If ddlAccountRef.Text = "*Other" Then
                txtAccountName.Visible = True
                txtAccountName.Enabled = True
            Else
                txtAccountName.Visible = False
            End If
            GetAllJobSheets()
            
        Else
        
            If ddlAccountRef.Text = "*Other" Then
                txtAccountName.Visible = True
                txtAccountName.Enabled = True
            Else
                txtAccountName.Visible = False
            End If
            If (Not (ViewState("tblDescriptionRowCounter")) Is Nothing) Then 
                tblDescriptionRowCounter = Convert.ToInt32(ViewState("tblDescriptionRowCounter"))
              
                AddRows_tblDescriptionRowCounter(tblDescriptionRowCounter)
            End If
            If (Not (ViewState("tblMaterialsRowCounter")) Is Nothing) Then
                tblMaterialsRowCounter = Convert.ToInt32(ViewState("tblMaterialsRowCounter"))
               
                AddRows_tblMaterialsRowCounter(tblMaterialsRowCounter)
            End If
            If (Not (ViewState("tblOutstandingRowCounter")) Is Nothing) Then
                tblOutstandingRowCounter = Convert.ToInt32(ViewState("tblOutstandingRowCounter"))
                AddRows_tblOutstandingRowCounter(tblOutstandingRowCounter)
            End If
          
        End If
      
    End Sub

Open in new window


Not sure if I am re adding the controls before the viewstate is processed? Am I doing it correctly? I did post a question a good time ago about this and was advised to do it this way Old Question. I believe I followed it correctly?
Thanks,
0
 
Bob LearnedCommented:
Let's review the page life cycle:

ASP.NET Page Life Cycle Overview
http://msdn.microsoft.com/en-us/library/ms178472.aspx

You can see in this image that LoadViewState comes after Page_Init, and before Page_Load.

ASP.NET Page Life Cycle
0
Get expert help—faster!

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

 
BoltonWandererAuthor Commented:
Thanks for the diagram. I have adjusted my code so that the Dymaic controls being loaded is happening in the PreInit method.  However where would I load the actual data as it appears that it only ever allows me to have one table with two textboxes and can't add any more.

 Private Sub JobSheet_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
        If IsPostBack = True Then
            If (Not (ViewState("tblDescriptionRowCounter")) Is Nothing) Then
                tblDescriptionRowCounter = Convert.ToInt32(ViewState("tblDescriptionRowCounter"))
                AddRows_tblDescriptionRowCounter(tblDescriptionRowCounter)
            End If
            If (Not (ViewState("tblMaterialsRowCounter")) Is Nothing) Then
                tblMaterialsRowCounter = Convert.ToInt32(ViewState("tblMaterialsRowCounter"))
                AddRows_tblMaterialsRowCounter(tblMaterialsRowCounter)
            End If
            If (Not (ViewState("tblOutstandingRowCounter")) Is Nothing) Then
                tblOutstandingRowCounter = Convert.ToInt32(ViewState("tblOutstandingRowCounter"))
                AddRows_tblOutstandingRowCounter(tblOutstandingRowCounter)
            End If
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SqlConnection.ClearAllPools()

        'http://msdn.microsoft.com/en-us/library/xhy3h9f9.aspx
        If IsPostBack = False Then
            For x = 0 To 24 - 1
                ddlStartTime_Hour.Items.Add(PadWithStartZeros(2, x))
                ddlEndTime_Hour.Items.Add(PadWithStartZeros(2, x))
            Next
            For x = 0 To 60 - 1 Step 5
                ddlStartTime_Minute.Items.Add(PadWithStartZeros(2, x))
                ddlEndTime_Minute.Items.Add(PadWithStartZeros(2, x))
            Next

            'Load Next Job Sheet Ref
            txtCompanyID.Text = 0
            Try
                GetNextJobSheetRef()
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            bdpDate.SelectedDate = Now
            bdpClientDate.SelectedDate = Now
            bdpEmployeeDate.SelectedDate = Now
            ddlStartTime_Minute.Text = ddlStartTime_Minute.Items(0).ToString
            ddlEndTime_Minute.Text = ddlEndTime_Minute.Items(0).ToString
            ddlStartTime_Hour.Text = ddlStartTime_Hour.Items(Now.Hour).ToString
            ddlEndTime_Hour.Text = ddlEndTime_Hour.Items(Now.Hour).ToString
            LoadAccountRefDetails()
            If ddlAccountRef.Text = "*Other" Then
                txtAccountName.Visible = True
                txtAccountName.Enabled = True
            Else
                txtAccountName.Visible = False
            End If
            GetAllJobSheets()
        Else
            If ddlAccountRef.Text = "*Other" Then
                txtAccountName.Visible = True
                txtAccountName.Enabled = True
            Else
                txtAccountName.Visible = False
            End If
           
        End If

    End Sub

Open in new window


ab
Thanks,
0
 
Bob LearnedCommented:
However where would I load the actual data as it appears that it only ever allows me to have one table with two text boxes and can't add any more.

Did this happen when you moved to PreInit?  Was this working correctly earlier?
0
 
BoltonWandererAuthor Commented:
Yes it was working before I did the move to PreInit. Thanks
0
 
Bob LearnedCommented:
If you are grabbing rows to add from ViewState, it can't be processed until after LoadViewState.  You would need to figure out a different approach.
0
 
BoltonWandererAuthor Commented:
Removed the feature of Add Row and just kept it as one big box. Sadly not an actual solution if others need this.
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.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now