Thursday, March 22, 2012

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:

<%@dotnet.itags.org. 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" /> 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:

Public Class DynamicallyBuildTables
Inherits System.Web.UI.Page
' Inherits System.Web.UI.WebControls
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents txtRows As System.Web.UI.WebControls.TextBox
Protected WithEvents txtCols As System.Web.UI.WebControls.TextBox
Protected WithEvents chkBorder As System.Web.UI.WebControls.CheckBox
Protected WithEvents cmdCreate As System.Web.UI.WebControls.Button
Protected WithEvents Tbl1 As System.Web.UI.WebControls.Table
Protected WithEvents ddl1 As System.Web.UI.WebControls.DropDownList

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If IsPostBack = False Then
Tbl1.Visible = False
ddl1.Items.Add("Dashed")
ddl1.Items.Add("Dotted")
ddl1.Items.Add("Doubled")
ddl1.Items.Add("Groove")
ddl1.Items.Add("Inset")
ddl1.Items.Add("None")
ddl1.Items.Add("NotSet")
ddl1.Items.Add("Outset")
ddl1.Items.Add("Ridge")
ddl1.Items.Add("Solid")
Else
Tbl1.Visible = True
End If
' Tbl1.BorderStyle = BorderStyle.Inset
Tbl1.BorderWidth = Unit.Pixel(1)
End Sub

Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click
'Remove all current rows and cells.
'This would not be necessary if you set
'EnableViewState = False.
Tbl1.Controls.Clear()
Dim strRows As String = txtRows.Text
Dim strCols As String = txtCols.Text
Dim strBorder As String = "BorderStyle." & ddl1.SelectedItem.Text
Dim i, j As Integer
For i = 1 To Val(strRows)
'Create a new tablerow object.
Dim rowNew As New TableRow
'put the new tablerow into the table object.
Tbl1.Controls.Add(rowNew)
For j = 1 To Val(strCols)
'Create a new TableCell object.
Dim cellNew As New TableCell
'***************************** standard text
'cellNew.Text = "Example Cell (" & i.ToString() & ","
'cellNew.Text &= j.ToString() & ")<br />"
'*****************************
'***************************** Added as objects
Dim lblNew As New Label
Dim imgNew As New System.Web.UI.WebControls.Image
imgNew.ImageUrl = "/images/smile.gif"
lblNew.Text = "hello"
cellNew.Controls.Add(lblNew)
cellNew.Controls.Add(imgNew)
'*****************************

If chkBorder.Checked Then
' Not working accurately, but dows show how to
' dynamically set border style.
txtCols.Text = strBorder
cellNew.BorderStyle = strBorder 'Bombs right here
cellNew.BorderWidth = Unit.Pixel(4)
End If
'Put the TableCell in the TableRow.
rowNew.Controls.Add(cellNew)
Next
Next
End Sub
End Class


Also, Is there a way to populate the ddl1.items.add("") list within the code and not hard coding?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.
I'm not sure what you're trying to do.

Would help if I explained it more. Sorry about that. :blush:

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?

0 comments:

Post a Comment