Thank you for the very detailed answer. I guess I was trying to force
something to happen in the page_load event chasing a challenge I have posted
in the Datagrid forum. Nevertheless, your answer reconfirmed the way I
thought it should be happening. I will take your suggestion on the event
reading. If you think you might be able to help, I am taking action in the
button_click event as shown below, but when I load a column in a datagrid at
run time with no columns at design time (and no autogenerate columns), my
datagrid dissappears on postback.
Everyone was saying to "make sure you bind the datagrid on postback" and
since binding in the button_click was not working, I kept trying to put it
in the load page event. The code below works perfectly if I have even one
column defined at design time, but as soon as I take all columns out at
design time, everything falls apart.
Thanks again for your help. Maybe you can catch something below that we are
missing.
Jim
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindGrid("Company")
End If
End Sub
Private Sub btnFillOne_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFillOne.Click
BindGrid("Company")
End Sub
Private Sub btnFillTwo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFillTwo.Click
BindGrid("Contact")
End Sub
Sub BindGrid(ByVal field_name)
Dim mycn As SqlClient.SqlConnection
Dim daAccounts As SqlClient.SqlDataAdapter
Dim dsAccounts As New DataSet
Dim mysql As String
mycn = New
SqlClient.SqlConnection(System.Configuration.Confi gurationSettings.AppSettin
gs("ConnectString"))
DataGrid1.Columns.Clear()
Dim dcolumn As System.Web.UI.WebControls.ButtonColumn
dcolumn = New System.Web.UI.WebControls.ButtonColumn
dcolumn.ButtonType = ButtonColumnType.LinkButton
dcolumn.DataTextField = field_name
dcolumn.HeaderText = field_name
dcolumn.CommandName = "Select"
dcolumn.Text = "Select"
If field_name = "Company" Then mysql = "Select * from tblCompanys"
If field_name = "Contact" Then mysql = "Select * from tblContacts"
daAccounts = New SqlClient.SqlDataAdapter(mysql, mycn)
daAccounts.Fill(dsAccounts)
DataGrid1.DataSource = dsAccounts
DataGrid1.DataKeyField = "ID"
DataGrid1.Columns.Add(dcolumn)
DataGrid1.DataBind()
mycn.Close()
End Sub
"Kairi Zikpin" <zikkai.nospam.@netscape.net> wrote in message
news:YS***********************@news3.calgary.shaw. ca...
to answer your other question, viewstate is restored during page_init
which happens before page_load.
also, i think the real problem you are having is where to put your code.
do not take action on postback in the page_lod event (in this case).
instead, take action in the bottun's click event since you are
responding to the click event.
i'd encourage you to do some reading on event handlers. a good start
would be the following link (in internet explorer) provided you have the
visual studio.net documentation installed:
ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbconWebFormsPageProcessingStages.htm
Jim Mitchell wrote:
Am I correct to assume that any changes to a control's properties or
changes to a ViewState("Name") are not available until after the Page_Load
fires?
If so, how can you set a flag in a control event so that you can take
action on postback?
I have the following code..... The window.alert always reads 0
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 not ispostback then
textbox1.text = 0
end if
Response.Write("<script language=javascript>window.alert('textbox1 = " &
TextBox1.Text & "')</script>")
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnBindContacts.Click
TextBox1.Text = 1
ViewState("CallType") = 1
End Sub