Solved

ASP.NET Dynamic Control Adding - PostBack Issues

Posted on 2014-01-04
8
627 Views
Last Modified: 2014-01-12
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
Comment
Question by:BoltonWanderer
  • 4
  • 4
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39757366
Are you re-adding the controls on post-back, before the ViewState is processed?
0
 

Author Comment

by:BoltonWanderer
ID: 39757384
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39757431
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:BoltonWanderer
ID: 39757454
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
 
LVL 96

Expert Comment

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

Author Comment

by:BoltonWanderer
ID: 39757941
Yes it was working before I did the move to PreInit. Thanks
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39757969
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
 

Author Closing Comment

by:BoltonWanderer
ID: 39775231
Removed the feature of Add Row and just kept it as one big box. Sadly not an actual solution if others need this.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change to file doesn't show up 16 52
asp.net mvc 2 22
Can Angular be used with classic asp.net? 3 13
Time comparison asp 1 12
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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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