Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    48

    [asp.NET + VB] Puntamento ai controlli dinamici runat=server.

    Salve a tutti, ho la seguente sub che si occupa di crearmi dinamicamente una tabella con delle textbox runat=server:

    codice:
     
        Private Sub CreateDynamicTable() 
     
            Dim tblRows As Integer = 64 
            Dim tblCols As Integer = 8 
            Dim intRQS As Integer = 1 
     
            Dim startRow As Integer = ((64 * intRQS) - 64) 
            Dim tbl As Table = New Table() 
            tbl.ID = "tblMain" 
            tbl.Attributes.Add("runat", "server") 
            PlaceHolder1.Controls.Add(tbl) 
     
            For i As Integer = startRow To (startRow + tblRows - 1) 
     
                Dim tr As TableRow = New TableRow() 
     
                For j As Integer = 0 To tblCols - 1 
     
                    Dim tc As TableCell = New TableCell() 
                    Dim txtBox As TextBox = New TextBox() 
     
                    If (j Mod 2) = 0 Then 
                        txtBox.Width = "80" 
                        txtBox.Text = "10.14.1." & (i + (64 * (j / 2))) 
                        txtBox.ID = "IP_" & (i + (64 * (j / 2))) 
                        txtBox.BackColor = Drawing.Color.AntiqueWhite 
                    Else 
                        txtBox.Width = "150" 
                        txtBox.Text = "Desc. no. " & (i + (64 * (j / 2))) 
                        txtBox.ID = "DS_" & (i + (64 * ((j - 1) / 2))) 
                        txtBox.BackColor = Drawing.Color.Beige 
     
                    End If 
     
                    txtBox.Attributes.Add("runat", "server") 
                    txtBox.AutoPostBack = False 
                    txtBox.ReadOnly = True 
                    tc.Controls.Add(txtBox) 
                    tr.Cells.Add(tc) 
     
                Next j 
     
                tbl.Rows.Add(tr) 
     
            Next i 
     
            tbl.BorderStyle = BorderStyle.Solid 
            tbl.BorderColor = Drawing.Color.Black 
            tbl.BorderWidth = "1" 
     
        End Sub
    Ovunque io lanci la sub, sia in init, che in preload o dove altro, se poi tento tramite un button ad esempio (o in qualsiasi altro modo) di riferirmi ad una textbox così creata (es: "IP_3.text") mi dice che il controllo non esiste. Sapete dirmi dove sbaglio?

    FYI, il placeholder1 sta in un form creato a mano (formQ) che é runat=server.

    Per farvi capire un po' meglio l'output che da il codice vi mostro uno spezzone della tabella (tutta era troppo grande).

    http://img228.imageshack.us/img228/6780/tblip.jpg

    Vi ringrazio per un aiuto.
    Luca

  2. #2
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    codice:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="a.aspx.vb" Inherits="prove_a" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    
        <script type="text/javascript" src="../js/jquery/jquery-1.3.2.min.js"></script>
    
        <script language="javascript" type="text/javascript">
    // <!CDATA[
    
    
    // ]]>
    </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" />
            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        </div>
        </form>
    </body>
    </html>
    codice:
    Option Strict On
    Imports l = libreria.ModuloWeb
    
    Partial Class prove_a
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            CreateDynamicTable()
        End Sub
    
        Private Sub CreateDynamicTable()
    
            Dim tblRows As Integer = 64
            Dim tblCols As Integer = 8
            Dim intRQS As Integer = 1
    
            Dim startRow As Integer = ((64 * intRQS) - 64)
            Dim tbl As Table = New Table()
            tbl.ID = "tblMain"
            tbl.Attributes.Add("runat", "server")
            PlaceHolder1.Controls.Add(tbl)
    
            For i As Integer = startRow To (startRow + tblRows - 1)
    
                Dim tr As TableRow = New TableRow()
    
                For j As Integer = 0 To tblCols - 1
    
                    Dim tc As TableCell = New TableCell()
                    Dim txtBox As TextBox = New TextBox()
    
                    If (j Mod 2) = 0 Then
                        txtBox.Width = Unit.Pixel(80)
                        txtBox.Text = "10.14.1." & (i + (64 * (j / 2)))
                        txtBox.ID = "IP_" & (i + (64 * (j / 2)))
                        txtBox.BackColor = Drawing.Color.AntiqueWhite
                    Else
                        txtBox.Width = Unit.Pixel(150)
                        txtBox.Text = "Desc. no. " & (i + (64 * (j / 2)))
                        txtBox.ID = "DS_" & (i + (64 * ((j - 1) / 2)))
                        txtBox.BackColor = Drawing.Color.Beige
    
                    End If
    
                    txtBox.Attributes.Add("runat", "server")
                    txtBox.AutoPostBack = False
                    txtBox.ReadOnly = True
                    tc.Controls.Add(txtBox)
                    tr.Cells.Add(tc)
    
                Next j
    
                tbl.Rows.Add(tr)
    
            Next i
    
            tbl.BorderStyle = BorderStyle.Solid
            tbl.BorderColor = Drawing.Color.Black
            tbl.BorderWidth = Unit.Pixel(1)
    
        End Sub
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim j As TextBox = TryCast(Me.PlaceHolder1.FindControl("ip_43"), TextBox)
            If j IsNot Nothing Then
                PrintLn(j.Text)
            End If
    
        End Sub
    End Class
    Pietro

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2002
    Messaggi
    48
    Pietro grazie davvero tantissimo!!! Ho sempre problemi del menga e quasi nessuno mi sa mai aiutare, sei una manna dal cielo!!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.