?
Solved

ASP.NET Dynamic Control Adding - PostBack Issues

Posted on 2014-01-04
8
Medium Priority
?
638 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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 1000 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

765 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