Results 1 to 3 of 3

Thread: Thought this would be simple

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Coming to a keyboard near you.
    Posts
    79

    Unhappy Thought this would be simple

    Thought I would work on setting certain UI elements dynamically. Sounded like an easy task until now. Any ideas how to resolve this error?

    The ASPX page:

    Code:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="DynamicallyBuildTables.aspx.vb" Inherits="BookChp5a.DynamicallyBuildTables"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    	<HEAD>
    		<title>DynamicallyBuildTables</title>
    		<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    		<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    		<meta name="vs_defaultClientScript" content="JavaScript">
    		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    	</HEAD>
    	<body>
    		<form id="Form1" method="post" runat="server">
    			<P>
    				Rows:
    				<asp:TextBox id="txtRows" runat="server" />&nbsp; Cols:
    				<asp:TextBox id="txtCols" runat="server" /></P>
    			<P>
    				<asp:CheckBox id="chkBorder" runat="server" Text="Put border around Cells"></asp:CheckBox></P>
    			<P>
    				<asp:DropDownList id="ddl1" runat="server"></asp:DropDownList>
    			</P>
    			<P>
    				<asp:Button id="cmdCreate" runat="server" Text="Create"></asp:Button><BR>
    			</P>
    			<P>
    				<asp:Table id="Tbl1" runat="server">
    					<asp:TableRow ID="row" Runat="server">
    						<asp:TableCell id="cell" Runat="server" Text="A Test RowSpan">
    							<!-- Instead of using the Text property, you could add other
    							     ASP.NET control tags here. --></asp:TableCell>
    					</asp:TableRow>
    				</asp:Table>
    			</P>
    		</form>
    	</body>
    </HTML>
    The code behind file:

    VB Code:
    1. Public Class DynamicallyBuildTables
    2.     Inherits System.Web.UI.Page
    3.     ' Inherits System.Web.UI.WebControls
    4. #Region " Web Form Designer Generated Code "
    5.  
    6.     'This call is required by the Web Form Designer.
    7.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    8.  
    9.     End Sub
    10.     Protected WithEvents txtRows As System.Web.UI.WebControls.TextBox
    11.     Protected WithEvents txtCols As System.Web.UI.WebControls.TextBox
    12.     Protected WithEvents chkBorder As System.Web.UI.WebControls.CheckBox
    13.     Protected WithEvents cmdCreate As System.Web.UI.WebControls.Button
    14.     Protected WithEvents Tbl1 As System.Web.UI.WebControls.Table
    15.     Protected WithEvents ddl1 As System.Web.UI.WebControls.DropDownList
    16.  
    17.     'NOTE: The following placeholder declaration is required by the Web Form Designer.
    18.     'Do not delete or move it.
    19.     Private designerPlaceholderDeclaration As System.Object
    20.  
    21.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    22.         'CODEGEN: This method call is required by the Web Form Designer
    23.         'Do not modify it using the code editor.
    24.         InitializeComponent()
    25.     End Sub
    26.  
    27. #End Region
    28.  
    29.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    30.         'Put user code to initialize the page here
    31.         If IsPostBack = False Then
    32.             Tbl1.Visible = False
    33.             ddl1.Items.Add("Dashed")
    34.             ddl1.Items.Add("Dotted")
    35.             ddl1.Items.Add("Doubled")
    36.             ddl1.Items.Add("Groove")
    37.             ddl1.Items.Add("Inset")
    38.             ddl1.Items.Add("None")
    39.             ddl1.Items.Add("NotSet")
    40.             ddl1.Items.Add("Outset")
    41.             ddl1.Items.Add("Ridge")
    42.             ddl1.Items.Add("Solid")
    43.         Else
    44.             Tbl1.Visible = True
    45.         End If
    46.         '        Tbl1.BorderStyle = BorderStyle.Inset
    47.         Tbl1.BorderWidth = Unit.Pixel(1)
    48.     End Sub
    49.  
    50.     Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click
    51.         'Remove all current rows and cells.
    52.         'This would not be necessary if you set
    53.         'EnableViewState = False.
    54.         Tbl1.Controls.Clear()
    55.         Dim strRows As String = txtRows.Text
    56.         Dim strCols As String = txtCols.Text
    57.         Dim strBorder As String = "BorderStyle." & ddl1.SelectedItem.Text
    58.         Dim i, j As Integer
    59.         For i = 1 To Val(strRows)
    60.             'Create a new tablerow object.
    61.             Dim rowNew As New TableRow
    62.             'put the new tablerow into the table object.
    63.             Tbl1.Controls.Add(rowNew)
    64.             For j = 1 To Val(strCols)
    65.                 'Create a new TableCell object.
    66.                 Dim cellNew As New TableCell
    67.                 '***************************** standard text
    68.                 'cellNew.Text = "Example Cell (" & i.ToString() & ","
    69.                 'cellNew.Text &= j.ToString() & ")<br />"
    70.                 '*****************************
    71.                 '***************************** Added as objects
    72.                 Dim lblNew As New Label
    73.                 Dim imgNew As New System.Web.UI.WebControls.Image
    74.                 imgNew.ImageUrl = "/images/smile.gif"
    75.                 lblNew.Text = "hello"
    76.                 cellNew.Controls.Add(lblNew)
    77.                 cellNew.Controls.Add(imgNew)
    78.                 '*****************************
    79.  
    80.                 If chkBorder.Checked Then
    81.                     ' Not working accurately, but dows show how to
    82.                     ' dynamically set border style.
    83.                     txtCols.Text = strBorder
    84.                     cellNew.BorderStyle = strBorder 'Bombs right here
    85.                     cellNew.BorderWidth = Unit.Pixel(4)
    86.                 End If
    87.                 'Put the TableCell in the TableRow.
    88.                 rowNew.Controls.Add(cellNew)
    89.             Next
    90.         Next
    91.     End Sub
    92. End Class

    Also, Is there a way to populate the ddl1.items.add("") list within the code and not hard coding?
    Tenebrosity
    "But the plans were on display ..."
    "On display? I eventually had to go down to the cellar to find them, With a torch."
    "That's the display department. The lights had probably gone out."
    "So had the stairs."
    "But look, you found the notice didn't you?"
    "Yes," said Arthur, "yes I did. It was on display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying Beware of the Leopard."

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Thought this would be simple

    Quote Originally Posted by Tenebrosity

    Also, Is there a way to populate the ddl1.items.add("") list within the code and not hard coding?

    I'm not sure what you're trying to do.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Coming to a keyboard near you.
    Posts
    79

    Re: Thought this would be simple

    Quote Originally Posted by mendhak
    I'm not sure what you're trying to do.
    Would help if I explained it more. Sorry about that.

    I am trying to dynamicaly assign the BorderStyle from the value in a string variable. The following line blows up:

    cellNew.BorderStyle = strBorder 'Bombs right here

    In addition to that, when you manually assign "BorderStyle." intellisense pops up with the listed values:

    Dashed
    Dotted
    Doubled
    Groove
    Inset
    None
    NotSet
    Outset
    Ridge
    Solid

    I want to capture those values and place them into a DropDownList with out having to hard code. Instead of doing it like this:

    ddl1.Items.Add("Dashed")

    Can I code it to auto populate those values?

    Any ideas?
    Tenebrosity
    "But the plans were on display ..."
    "On display? I eventually had to go down to the cellar to find them, With a torch."
    "That's the display department. The lights had probably gone out."
    "So had the stairs."
    "But look, you found the notice didn't you?"
    "Yes," said Arthur, "yes I did. It was on display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying Beware of the Leopard."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width