Dynamic Table Question

  • Thread starter Thread starter Stan Sainte-Rose
  • Start date Start date
S

Stan Sainte-Rose

Hi,

I use this bit of code to generate dynamic tables in the page load section
....
Dim ntable as New Table
For i = 1993 To 2008
ntable = New Table
ntable.ID = "Q" + i.ToString
....
....
Next

How to use the properties of each table ?
I would like to set visible and not visible some of them using their ID.

Stan
 
Stan, I'd add the tables to a hashtable so that you can access them back
that way:

Dim tables as HashTable(15)
for i = 1993 to 2008
dim table as new Table()
...
Controls.Add(table)
tables.Add(i, table)
next

you can then access the table by the year:

dim table as Table = ctype(tables(1999), Table)
table.visible = false

Karl
 
Maybe I have to reconsider my code.

Here s what I want to do (I works fine with ASP and I m trying to do it with
asp.net)

I need to load 16 tables, and hide 15 of them.
I have a another table where I display the years between 1993 to 2008
When I choose one year, I would like to hide all the table and just display
the table regarding the year.
Within asp, I m using Span and a javascript for hiding/showing the right
table.
Any help ?

Of course all the tables are dynamic and their content come from a database.
I don't have problems with the ado.net part..

Stan
 
Oopps..forgot the new
Try:
Dim tables as new Hashtable(15)

or, if you don't have System.Collections imported, try:
Dim tables As New System.Collections.Hashtable(15)

What ryan says about using FindControl will also work, but you should get
better performance this way, and you'll need to do less error checking.

Karl
 
Hi again

Stupid question...
Your procedure works fine..

But I have a problem..
When I look at the html source from the browser, the table is out the <Form>
tag.
How can I add the table between <Form> and </Form> knowing that my table has
input textbox and a submit button

Or, is there an elegant method to do it ?

Stan
 
When you did Controls.Add(table), you were accessing the Page's Control
property and adding the table to it Instead, you want to add the tables to
the Controls() property of the form, or a placeholder inside the form.

Not sure if you are using vs.net or not, but you should be able to to
Form1.Controls.Add(table) where Form1 is the ID of the form.

Karl
 
Thanks Karl,

Yes I m using VS.Net
In fact, I ve used a Panel control and I ve added the Table like this
Panel1.Controls.Add(tableau)
So, I will try yours..

However, I still have a little problem.
here's my code.
I would like to use the Page.Ispostback to avoid to reload the database and
I would like to update it when I click on the button.
If you have time to look at the code and tell me where I can add the
Ispostback command.
When the button is pressed and the database is update I would like to change
the button text to "Updated".
I think I will be able to change it to Update when one textbox will be
modified.
As I said, I m from ASP ... so.... :-)

Here's the code..


Imports System.Configuration
Imports System.Data.SqlClient
Public Class A_QUESTIONS
Inherits System.Web.UI.Page
Dim Ds As New DataSet("MonDS")
Dim cn As New
SqlConnection(ConfigurationSettings.AppSettings.Item("dbData"))

Protected WithEvents Table As System.Web.UI.WebControls.Table
Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
Protected WithEvents Bouton As System.Web.UI.WebControls.Button
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Dim Table1 As Table
Dim Langue As String
Dim tables As New Hashtable(15)

#Region " Code généré par le Concepteur Web Form "

'Cet appel est requis par le Concepteur Web Form.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'REMARQUE : la déclaration d'espace réservé suivante est requise par le
Concepteur Web Form.
'Ne pas supprimer ou déplacer.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN : cet appel de méthode est requis par le Concepteur Web
Form
'Ne le modifiez pas en utilisant l'éditeur de code.


InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Langue = "FR"
Dim Pays, Chapitre, Schapitre As Integer
'Pays = Request.QueryString("PAYS")
Pays = 1
Chapitre = Request.QueryString("IDC")
If Request.QueryString("IDS") = "" Then
Schapitre = 0
Else
Schapitre = Request.QueryString("IDS")
End If
Ds.Tables.Add("QUESTIONS")
Dim daQUESTIONS As New SqlDataAdapter
daQUESTIONS.SelectCommand = SQL_LISTE_QUESTIONS(Langue, Pays,
Chapitre, Schapitre)
daQUESTIONS.Fill(Ds.Tables("QUESTIONS"))

Dim paragraphe As String = ""
Dim tableau As New Table
Dim Ligne As New TableRow
Dim cell1 As New TableCell
Dim cell2 As New TableCell
Dim cell3 As New TableCell
Dim row As DataRow
Dim i As Integer
Dim CPTAN As Integer = 3


For i = 1993 To 2008
CPTAN += 1
tableau = New Table
tableau.ID = "Q" + i.ToString
tableau.Visible = False
For Each row In Ds.Tables("QUESTIONS").Rows
If paragraphe <> row("paragraphe") Then
Ligne = New TableRow
cell1 = New TableCell
cell1.ColumnSpan = 3
cell1.Text = row("Paragraphe")
paragraphe = row("paragraphe")
Ligne.Cells.Add(cell1)
tableau.Rows.Add(Ligne)
End If
Ligne = New TableRow
cell1 = New TableCell
cell2 = New TableCell
cell3 = New TableCell

If row("Question") = "[TEXT]" Then
cell1.Text = "<INPUT TYPE=TEXT NAME=T_" &
row("idquestion") & "_" & i & " VALUE=""" & row(16 + CPTAN) & """>"
Else
cell1.Text = row("Question")
End If
cell2.Text = "<INPUT TYPE=TEXT NAME=" & row("idquestion") &
"_" & i & " VALUE=""" & row(16 + CPTAN) & """ onBlur=""TypeChamps(this,'" &
Trim(row("TypeTest")) & "');"">"
cell3.Text = "<INPUT TYPE=TEXT NAME=S" & row("idquestion") &
"_" & i & " VALUE=""" & row(16 + CPTAN) & """>"
cell1.Width = Unit.Point(254)
cell2.Width = Unit.Point(100)
cell3.Width = Unit.Point(110)

Ligne.Cells.Add(cell1)
Ligne.Cells.Add(cell2)
Ligne.Cells.Add(cell3)
tableau.Rows.Add(Ligne)
Next
Ligne = New TableRow
cell1 = New TableCell
cell1.ColumnSpan = 3
Dim button1 As New Button
button1.CausesValidation = False
button1.Text = "Mise à Jour"

button1.ID = "B" + i.ToString
cell1.Controls.Add(button1)
Ligne.Cells.Add(cell1)
tableau.Rows.Add(Ligne)
tableau.Visible = False
Panel1.Controls.Add(tableau)
tables.Add(i, tableau)
Next
Table1 = CType(tables(1999), Table)
Table1.Visible = True


End Sub
Private Function SQL_LISTE_QUESTIONS(ByVal Langue As String, ByVal Pays
As Integer, ByVal Chapitre As Integer, ByVal Schapitre As Integer) As
SqlCommand
Dim cmd As New SqlCommand("A_LISTE_QUESTIONS", cn)
cmd.CommandType = CommandType.StoredProcedure
Dim pc As SqlParameterCollection = cmd.Parameters
pc.Add("@langue", SqlDbType.VarChar, 2)
pc.Add("@Pays", SqlDbType.Int, 4)
pc.Add("@Chapitre", SqlDbType.Int, 4)
pc.Add("@Schapitre", SqlDbType.Int, 4)
pc("@langue").Value = Langue
pc("@pays").Value = Pays
pc("@Chapitre").Value = Chapitre
pc("@Schapitre").Value = Schapitre
Return cmd
End Function

Private Sub Bouton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Bouton.Click
sender.text = "OK"
End Sub
 
Back
Top