Datagrid in Custom Server control Does not show

T

ton

Hi,
I've created a custom server control dbList which contains a datagrid

In my CreateChildControls sub I add the control
Controls.Add(Grid)

In my Fill sub I have this code (Fill can be executed from a button on the
page like dbList.Fill)

sqlConn.ConnectionString = "workstation id=VRS7;packet size=4096;user
id=Sa;data source=VRSSERVER;persist security info=False;initial
catalog=Sales"

sqlAdap = New SqlClient.SqlDataAdapter("select top 50 * from tuning",
sqlConn.ConnectionString) '' ConnectionString

sqlAdap.Fill(DS, "Tuning")

grid.DataSource = DS

grid.Visible = True

grid.BorderStyle = BorderStyle.Solid

grid.DataBind()

CreateChildControls()

After pressing the buttons nothing happens.

I use:

Imports System

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.HtmlControls

Imports System.ComponentModel

Imports System.Web.SessionState

Imports System.Data

Imports System.Data.SqlClient

and my declaration is:

Dim grid As New DataGrid

Dim sqlConn As New SqlClient.SqlConnection

Dim sqlAdap As SqlClient.SqlDataAdapter

Dim DS As New DataSet

When I add a grid to my page and I use the same code as in de fill command,
the datagrid will be visible.



What is wrong ??

Ton
 
A

Alessandro Zifiglio

What you are doing wrong is that your grid is being filled only when the
button is clicked. Add all code that adds your datagrid and binds data to it
somewhere else where it will always be run even after page postback. This is
a requirement for dynamically added controls. To work around your problem
that is still continue to add your datgrid on button click, use viewstate to
set a flag and check this flag in your createchildcontrols method, forcing
the code that added your control to the controls collection to fire even
after a postback.

To better understand what mean, follow up on this post, the last post in
particular.

http://groups.google.com/groups?hl=...Lb.1434%24nC1.287%40news.edisontel.com&rnum=1

The scenario there is a little different however you can see how viewstate
is being applied. Use that logic. Instead of page_load, use the
createchildcontrols method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top