locked
I can't read textbox in a dynamically created table RRS feed

  • Question

  • User-311780272 posted

    1. I need a form NOT a gridview or anything that requires toggling between editview and update. Old school looking form with button.

    2. I am writing FROM the database to the dynamic table and it populates the form just fine.

    3. type a little change in the textbox; and it crashes unable to read the Table ID.

    I have no idea how to find the Table and textbox id since this method doesn't seem to want to work.

    Any help out there?

    Thanks in advance.

    (code bellow)



    'VB.NET CODE
    '----------------------------------------------------------------------------
    Public Sub PopulateWaterRecords(ByVal dDateTo As Date) 'Populates the water record forms for each year 'Dim UserName As String = Membership.GetUser().UserName Dim query, dDateFrom Dim rows As Integer = 0 'Should this be 1/1/1980? to catch all dates to present? dDateFrom = #1/1/2009# query = "SELECT * FROM tblWaterRecords WHERE USER_NAME='" & UserName & "' AND (RecordDate BETWEEN #" & dDateFrom & "# AND #" & dDateTo & "#) ORDER BY RecordDate ASC;" Dim sConnString As OleDbConnection = New OleDbConnection(ConfigurationManager.AppSettings("ConnectHere")) Dim myCmd As OleDbCommand = New OleDbCommand(query, sConnString) sConnString.Open() 'open database Dim myReader As OleDbDataReader = myCmd.ExecuteReader() Dim ArraySize As Integer = 0 ArraySize = ArraySize + myReader.FieldCount 'create Array with upperlimits at number of datarecords Dim myWaterRecords(ArraySize, 7) As String While myReader.Read And rows <= ArraySize If userName.ToUpper.ToString = myReader("USER_NAME").ToUpper.ToString Then If myReader("RecordDate").ToString.Length > 0 Then 'AT LEAST SOME Data is present myWaterRecords(rows, 0) = Format(myReader("RecordDate"), "MMM/d/yyyy").ToString myWaterRecords(rows, 1) = myReader("MunicipalUse").ToString myWaterRecords(rows, 2) = myReader("GroundwaterUse").ToString myWaterRecords(rows, 3) = myReader("SurfacewaterUse").ToString myWaterRecords(rows, 4) = myReader("ReclaimedwaterUse").ToString myWaterRecords(rows, 5) = myReader("TotalwaterUse").ToString myWaterRecords(rows, 6) = myReader("AdditionalNotes").ToString 'add a row for each date rows = rows + 1 End If End If End While 'Columns is a set value of 7 defined in tblWaterRecords(RecordDate, MunicipalUse, GroundwaterUse, SurfacewaterUse, ReclaimedwaterUse, TotalWaterUse, AdditionalNotes CreateDynamicTable(rows, 7, myWaterRecords) End Sub Private Sub CreateDynamicTable(ByVal Rows As Integer, ByVal Columns As Integer, ByVal myWaterRecords As Array) PlaceHolder1.Controls.Clear() ' Fetch the number of Rows and Columns for the table ' using the properties Dim tblRows As Integer = Rows Dim tblCols As Integer = Columns 'create array with table headers Dim myHeaders As New ArrayList myHeaders.Add("Record Date") myHeaders.Add("Municipal") myHeaders.Add("Ground") myHeaders.Add("Surface") myHeaders.Add("Reclaimed") myHeaders.Add("Total") myHeaders.Add("Additional Notes") ' Create a Table and set its properties Dim tbl As Table = New Table() ' Add the table to the placeholder control tbl.ID = CStr("tbl" & Now().Year.ToString) PlaceHolder1.Controls.Add(tbl) ' Now iterate through the table and add your controls Dim thr As TableHeaderRow = New TableHeaderRow() For i As Integer = 0 To tblRows - 1 Dim tr As TableRow = New TableRow() For j As Integer = 0 To tblCols - 1 If i = 0 Then 'first ROW so create table headers Dim th As TableHeaderCell = New TableHeaderCell() Dim Header As Label = New Label() Header.Text = myHeaders(j) Header.CssClass = "Header" Header.Width = 125 Header.Height = 35 th.Controls.Add(Header) thr.Cells.Add(th) If j = (tblCols - 1) Then tbl.Rows.Add(thr) End If End If Dim tc As TableCell = New TableCell() Dim txtBox As TextBox = New TextBox() txtBox.Text = myWaterRecords(i, j) '"RowNo:" & i & " " & "ColumnNo:" & " " & j If j < (tblCols - 1) Then 'standard text boxes txtBox.MaxLength = 20 txtBox.Width = 75 Else 'multiline text box txtBox.MaxLength = 400 txtBox.Width = 150 txtBox.TextMode = TextBoxMode.MultiLine End If txtBox.ID = CStr("TextBox_" + i.ToString + j.ToString) ' Add the control to the TableCell tc.Controls.Add(txtBox) tc.Style.Value = "valign:top;" ' Add the TableCell to the TableRow tr.Cells.Add(tc) Next j ' Add the TableRow to the Table tbl.Rows.Add(tr) Next i ' This parameter helps determine in the LoadViewState event, ' whether to recreate the dynamic controls or not ViewState("dynamictable") = True End Sub Public Sub SaveData() 'write Annual Water Records to tblWaterRecords in database Dim cn As OleDbConnection = New OleDbConnection(ConfigurationManager.AppSettings("ConnectHere")) cn.Open() Dim dbCommand As New OleDbCommand dbCommand.Connection = cn Dim userName As String = Page.User.Identity.Name dbCommand.CommandText = "UPDATE tblWaterRecords (MunicipalUse,GroundwaterUse,SurfacewaterUse, ReclaimedwaterUse,TotalWaterUse, AdditionalNotes) " & _ "VALUES(@MunicipalUse, @GroundwaterUse,@SurfacewaterUse, @ReclaimedwaterUse, @TotalWaterUse, @AdditionalNotes) " & _ "WHERE USER_NAME = '" & userName & "' & Year(RecordDate)= " & Now().Year 'read data from dynamic table Dim rows As Integer = 12 ' months in year Dim columns As Integer = 7 'Dim RecordDate As Date = Date.Now ' MunicipalUse + GroundwaterUse + SurfacewaterUse + ReclaimedwaterUse For i As Integer = 0 To rows - 1 ' Dim RecordDate As Date Dim MunicipalUse As Integer = 0 Dim GroundwaterUse As Integer = 0 Dim SurfacewaterUse As Integer = 0 Dim ReclaimedwaterUse As Integer = 0 Dim TotalWaterUse As Integer = 0 Dim AdditionalNotes As String = "" Dim mpContentPlaceHolder As ContentPlaceHolder = CType(Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder) Dim HolderName As PlaceHolder = CType(mpContentPlaceHolder.FindControl("PlaceHolder1"), PlaceHolder) '***********PROBLEM IS HERE: Dim tableName As Table = CType(mpContentPlaceHolder.FindControl("tbl2009"), Table) 'Table returns a value of nothing. making txtMunicipaUse Null and it crashes! Dim txtMunicipalUse As TextBox = CType(mpContentPlaceHolder.FindControl("TextBox_01"), TextBox) MunicipalUse = txtMunicipalUse.Text Dim txtGroundwaterUse As TextBox = CType(PlaceHolder1.FindControl("TextBox_" & i.ToString & 2), TextBox) GroundwaterUse = txtGroundwaterUse.Text Dim txtSurfacewaterUse As TextBox = CType(PlaceHolder1.FindControl("TextBox_" & i.ToString & 3), TextBox) SurfacewaterUse = txtSurfacewaterUse.Text Dim txtReclaimedwaterUse As TextBox = CType(PlaceHolder1.FindControl("TextBox_" & i.ToString & 4), TextBox) ReclaimedwaterUse = txtReclaimedwaterUse.Text 'need to add this on the fly, since numbers could have changed TotalWaterUse = MunicipalUse + GroundwaterUse + SurfacewaterUse + ReclaimedwaterUse Dim txtAdditionalNotes As TextBox = CType(PlaceHolder1.FindControl("TextBox_" & i.ToString & 6), TextBox) AdditionalNotes = txtAdditionalNotes.Text ''write to the database 'dbCommand.Parameters.AddWithValue("@USER_NAME", userName) 'dbCommand.Parameters.AddWithValue("@RecordDate", RecordDate) dbCommand.Parameters.AddWithValue("@MunicipalUse", MunicipalUse) dbCommand.Parameters.AddWithValue("@GroundwaterUse", GroundwaterUse) dbCommand.Parameters.AddWithValue("@ReclaimedwaterUse", ReclaimedwaterUse) dbCommand.Parameters.AddWithValue("@TotalwaterUse", TotalWaterUse) dbCommand.Parameters.AddWithValue("@AdditionalNotes", AdditionalNotes) Next Try dbCommand.ExecuteNonQuery() Catch myerror As OleDbException Dim strErr As String = "There was an error updating the database: " & myerror.Message End Try dbCommand.Dispose() cn.Close() End Sub

    'FRONT END CODE
    
    '--------------------------------------------------------------------------------------------------------------------------------
    <%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Scorecard.Master" CodeBehind="AnnualWaterRecords.aspx.vb" Inherits="TERC.AnnualWaterRecords" Title="WATER RECORDS" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <asp:Label ID="lblUserName" runat="server" Text="Label" Visible="False"></asp:Label><br /> <asp:Label ID="lblThisYear" runat="server" Visible="true" /> <h1 style="display: inline;" align="left"> SECTION III. Water Use Records</h1> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> <br /> <asp:Button ID="Button1" runat="server" Text="Button" /> </asp:Content>


    Monday, November 2, 2009 2:37 PM

Answers

  • User-1759277745 posted

    I feel your frustration! I have had instances where I have created dynamic controls and values were missing and/or never set. It sucks. However, from what you posted I would look into a few things. 

    • When and where do you call PopulateWaterRecords()? 
      • In the Page_Load or Page_Init?
      • Are you checking for IsPostBack?
    • When and where do you call SaveData()?
      • In the Page_Load or Page_Init?
      • Are you checking for IsPostBack?

    One of the most common issues with ASP.NET dynamic controls is that whenever you are building and setting the IDs of the control dynamically, you must remember that you need to build that same ID on the PostBack. The controls need to built on the initial load and postback in order for your capture code(SaveData()) to work. Check out my blog post on this > http://joshua-clark.blogspot.com/2009/09/dyanmic-controls-in-aspnet-from-field.html

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 2, 2009 4:24 PM
  • User-952121411 posted

    Hello-

    The issue you are experiencing is due to the fact that your dynamically created textbox controls do not exist on PostBack, and therefore you are not able to access thier value as you have experienced.  I wrote up a description of this problem recnetly on this same forum, so please read for the solution on how to solve your problem (yes, there is a method to make your code work properly if you read thorugh the links).

    http://forums.asp.net/t/1484352.aspx#3473815

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 3, 2009 10:38 AM

All replies

  • User-1759277745 posted

    I feel your frustration! I have had instances where I have created dynamic controls and values were missing and/or never set. It sucks. However, from what you posted I would look into a few things. 

    • When and where do you call PopulateWaterRecords()? 
      • In the Page_Load or Page_Init?
      • Are you checking for IsPostBack?
    • When and where do you call SaveData()?
      • In the Page_Load or Page_Init?
      • Are you checking for IsPostBack?

    One of the most common issues with ASP.NET dynamic controls is that whenever you are building and setting the IDs of the control dynamically, you must remember that you need to build that same ID on the PostBack. The controls need to built on the initial load and postback in order for your capture code(SaveData()) to work. Check out my blog post on this > http://joshua-clark.blogspot.com/2009/09/dyanmic-controls-in-aspnet-from-field.html

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 2, 2009 4:24 PM
  • User-311780272 posted

    Thanks, good reading on your blog.


    This table is being created at the pageload level via the PopulateWaterRecords call.


    Q: so when I hit the 'SAVE' button (which triggers the SaveData() function) I need what?, a postback call to re-create via dynamicTable the table AND THEN reference the IDs? Seems like that might  wipe out textbox data entered by users?

    Monday, November 2, 2009 5:10 PM
  • User-952121411 posted

    Hello-

    The issue you are experiencing is due to the fact that your dynamically created textbox controls do not exist on PostBack, and therefore you are not able to access thier value as you have experienced.  I wrote up a description of this problem recnetly on this same forum, so please read for the solution on how to solve your problem (yes, there is a method to make your code work properly if you read thorugh the links).

    http://forums.asp.net/t/1484352.aspx#3473815

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 3, 2009 10:38 AM