Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Dynamic Controls.

Status
Not open for further replies.

Meleagant

Programmer
Joined
Aug 31, 2001
Messages
166
Location
US
All,

I am running into some trouble creating dynamic controls on a web page, well not really in CREATING them... but in accessing them after a post back.

In simple terms I dynamically create a list of reports. The user can drag and drop each report (Child Div tag) and re-order the report list or to delete reports.

The page loads fine and I see everything I need, I can drag and drop fine, but when I click the Save button a post back occurs and I was hoping to use DragContainer1.FindControl("div1111") {where 1111 is a report id). But on the postback DragContainer1 does not contain any child controls.

Has anyone ever added controls to a form server side and then was able to access those controls after a postback?

Here's some sample code:
ASPX page
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="SortReportSet.aspx.vb" Inherits="SortReportSet"
    Theme="HWC_Standard" Debug="true" Strict="true" EnableViewState="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
    <?xml version="1.0" ?>
    <title>Bundle Mutiple Files</title>
    <link href="StyleSheets_/HwcStandard.css" rel="stylesheet" type="text/css" />
    <link href="StyleSheets_/Dragging.css" rel="stylesheet" type="text/css" />
    <script src="JavaScript_/DragDrop.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
<!--
    document.onmousemove = mouseMove;
    document.onmousedown = mouseDown;
    document.onmouseup   = mouseUp;

    window.onload = function(){
	    dragHelper = document.createElement('DIV');
	    dragHelper.style.cssText = 'position:absolute;display:none;';
	    CreateDragContainer(document.getElementById('DragContainer1'));
	    document.body.appendChild(dragHelper);
    }
// -->   
    </script>

</head>
<body style="background-color: White; width: 100%;" background="images/bg1.jpg">
    <form id="form1" runat="server" enctype="multipart/form-data" method="POST">
        <div style="background-color: Transparent; height: 20px; width: 728px">
        </div>
        <div style="background-color: White; width: 728px; display: block;
            position: relative; left: 50%; margin-left: -364px; padding-top: 10px;">
            <label class="leftTD Black_12pt_B">
                Sort Reports</label>
        </div>
        <div style="background-color: White; width: 728px; display: block;
            position: relative; left: 50%; margin-left: -364px">
            <div id="divFileList" runat="server">
                <div class="leftTD">
                    <label class="leftTD Blue_9pt_B" style="display: none">
                        Order the files the way you want.</label>
                    [b]<div class="DragContainer" id="DragContainer1" runat="server" enableviewstate="true">
                    </div>
					[/b]
                </div>

.... Blah Blah Blah & Closing Tags

		<igtxt:WebImageButton ID="wibSave" runat="server" AutoSubmit="true" Text="Save" Width="100px">
			<ClientSideEvents Click="wibSaveReports" />
		</igtxt:WebImageButton>

.... Blah Blah Blah & Closing Tags

And the Code behind
Code:
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        CreateDragBoxes()

    End Sub

    Private Sub CreateDragBoxes()
        '* ** For Each Report Defined for that Report Set
        'DragContainer1.Controls.Clear()

        Dim SqlStr As String = ""
        Dim ConnStr As String = xConnStr

        Dim oConn As OracleConnection = New OracleConnection()
        Dim oCmd As New OracleCommand
        Dim rdr As OracleDataReader = Nothing
        Dim ReportSetName As String = ""
        Dim i As Integer = 0


		Dim setId As String = CType(Context.Items("setId"), String)
		oConn.ConnectionString = ConnStr
		oConn.Open()

		oCmd.CommandType = CommandType.Text
		oCmd.Connection = oConn

		'* ** Start Get Report Names
		SqlStr = "Select upper(report_name), report_id " & _
				 "From sched_report_detail srd Inner Join sched_report_set srs on srs.report_set_id = srd.report_set_id " & _
				 "Where srs.userid = " & Session.Item("UserId").ToString.Trim & _
					" and srs.report_set_id = '" & setId.Replace("SetId", "") & "' " & _
				 " Order By upper(report_name)"
		oCmd.CommandText = SqlStr
		rdr = oCmd.ExecuteReader

		Do While rdr.Read

			Dim mainDiv As System.Web.UI.HtmlControls.HtmlGenericControl
			mainDiv = New System.Web.UI.HtmlControls.HtmlGenericControl("div")

			mainDiv.InnerHtml = rdr.Item(0).ToString.Trim
			'mainDiv.ID = String.Format("div{0}", rdr.Item("report_id").ToString.Trim)
			mainDiv.Attributes("class") = "DragBox Bold"
			mainDiv.Attributes("overclass") = "OverDragBox"
			mainDiv.Attributes("dragclass") = "DragDragBox"
			mainDiv.Attributes("outclass") = "OutDragBox"
			mainDiv.Style("width") = "500"
			mainDiv.EnableViewState = True

			Dim childDiv As System.Web.UI.HtmlControls.HtmlGenericControl
			childDiv = New System.Web.UI.HtmlControls.HtmlGenericControl("div")

			Dim leftDiv As System.Web.UI.HtmlControls.HtmlGenericControl
			leftDiv = New System.Web.UI.HtmlControls.HtmlGenericControl("div")

			Dim rightDiv As System.Web.UI.HtmlControls.HtmlGenericControl
			rightDiv = New System.Web.UI.HtmlControls.HtmlGenericControl("div")

			'===================================================================
			'* ** Set Up Report Title 1
			Dim lbl As System.Web.UI.HtmlControls.HtmlGenericControl
			lbl = New System.Web.UI.HtmlControls.HtmlGenericControl("label")
			lbl.InnerHtml = "Title 1: "
			lbl.Attributes("class") = "labelDefault"

			Dim txt As System.Web.UI.HtmlControls.HtmlGenericControl
			txt = New System.Web.UI.HtmlControls.HtmlGenericControl("input")
			txt.Attributes("class") = "txtBox"
			txt.Style("width") = "190px"

			childDiv.Controls.Add(lbl)
			childDiv.Controls.Add(txt)
			childDiv.Attributes("class") = "tableRow"
			childDiv.Style("width") = "230px"
			leftDiv.Controls.Add(childDiv)

			'* ** Add right column for Description title
			childDiv = New System.Web.UI.HtmlControls.HtmlGenericControl("div")
			lbl = New System.Web.UI.HtmlControls.HtmlGenericControl("label")
			lbl.InnerHtml = "Report Description: "
			lbl.Attributes("class") = "labelDefault"
			childDiv.Style("width") = "200px"
			childDiv.Attributes("class") = "tableRow"
			childDiv.Controls.Add(lbl)
			rightDiv.Controls.Add(childDiv)

			leftDiv.Style("width") = "235px"
			rightDiv.Style("width") = "235px"
			leftDiv.Attributes("class") = "leftTD"
			rightDiv.Attributes("class") = "leftTD"


			.... Adding more controls ....

			'===================================================================
			'* ** Add Left and Right Divs to Main Div
			leftDiv.Style("width") = "235px"
			rightDiv.Style("width") = "250px"
			leftDiv.Attributes("class") = "leftTD"
			rightDiv.Attributes("class") = "leftTD"
			mainDiv.Controls.Add(leftDiv)
			mainDiv.Controls.Add(rightDiv)

			hdOrder.Value &= rdr.Item("report_id").ToString.Trim & ";"
			DragContainer1.Controls.Add(mainDiv)

			mainDiv.Dispose()
			leftDiv.Dispose()
			rightDiv.Dispose()
			childDiv.Dispose()

		Loop

		...

    End Sub

    Protected Sub wibSave_Click(ByVal sender As Object, ByVal e As Infragistics.WebUI.WebDataInput.ButtonEventArgs) Handles wibSave.Click

        Dim fileList As New ArrayList
        Dim fileItem As String = ""

        fileList.AddRange(Split(hdOrder.Value, ";", -1, CompareMethod.Text))
        For Each fileItem In fileList

            Dim control As System.Web.UI.Control = DragContainer1.FindControl(String.Format("div{0}", fileItem))
            Dim childcontrol As New System.Web.UI.Control

            'For Each childcontrol In control.Controls

				**** THIS IS Null  *** 

            'Next



        Next


    End Sub

Any help would be greatly appreciated

* Sine scientia ars nihil est
* Respondeat superior
 
There is no amount of cursing that will make me feel better. Those who take a good look will see how my sql is getting its values. One is a session var and the other is from the Context.Items collection since I am Server.Transferring my way into this page. On the post back I lose the Context.Items collection and thus lose my variable thus my sql crashes. The Try block I had around it, "hid" the error from me, even though the auto err email in the Application_Error was sending out emails. And for whatever reason I didn't start outlook this morning.

Arrgghh...Well in any event should be a nice tutorial for whoever wants to add dynamic controls to a form.

* Sine scientia ars nihil est
* Respondeat superior
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top