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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic DataGrid Problem

Status
Not open for further replies.

RISTMO

Programmer
Nov 16, 2001
1,259
US
Hey,

I'm having trouble making this work ... I create the DataGrid and try to connect it with the query in the BindData() function, but apparently the function's trying to run before the datagrid gets created?

Any ideas?

Thanks,
Rick

Code:
<% @Import Namespace="System" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script runat="server">
Sub Page_Init(sender as Object, e as EventArgs)
	tTitle.Text="Hey you!"
	tHeader.Text="Welcome!"
	tContent.Controls.Add(new Label())
	Dim c
	Dim d=1
	For Each c in tContent.Controls
		If d = tContent.Controls.Count Then
			c.Text = "This is a site to test out ASP.NET and see what cool stuff it can do!"
		End If
		d=d+1
	Next
	tContent.Controls.Add(new DataGrid())
	d=1
	For Each c in tContent.Controls
		If d = tContent.Controls.Count Then
			c.ID = "myDG"
		End If
		d=d+1
	Next
End Sub
Sub Page_Load(sender as Object, e as EventArgs)
'    BindData()
End Sub

Sub BindData()
	Dim myConnection as New SqlConnection("server=SERVER;uid=UID;pwd=PWD;database=DB")
	Const strSQL as String = "SELECT id, Name FROM names ORDER BY id"
	Dim myCommand as New SqlCommand(strSQL, myConnection)
	myConnection.Open()
	myDG.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
	myDG.DataBind()
	myConnection.Close()
End Sub
</script>
<html>
<head><title><asp:Literal id="tTitle" Text="The Website Name Goes Here" runat="server" /></title></head>
<body>
<table style="width:770px;border:1px solid black;">
	<tr>
		<td style="width:150px;background-color:#EEEEEE;"><asp:Literal id="tMenu" Text="<a href='text.aspx'>test...</a>" runat="server" /></td>
		<td valign="top">
		<h1><asp:Literal id="tHeader" Text="Welcome to the Website!" runat="server" /></h1>
		<asp:PlaceHolder id="tContent" runat="server" />
		</td>
	</tr>
</table>
</body>
</html>

RISTMO Designs: Rockwall Web Design
Arab Church: Arabic Christian Resources
Genuine Autos: Kaufman Chevrolet & Pontiac Dealer
Rick Morgan's Official Website
 
What is the error you are getting? Maybe the ID of the datagrid is not being set based of your IF statement, since you have already added a control, the name will not be set and the bind will not find that ID. I am not sure why you are doing it that way. I do it by first creating a new control, assigning it any properties including a name, then adding the control to the page, or panel etc.
 
I see no need for the loop. Try my example. Create the control, assign the properties, then add the control.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top