Solved

ASP.NET Dynamic Control Adding - PostBack Issues

Posted on 2014-01-04
8
629 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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