473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Why/Why not isPostBack is used when creating controls at run-time ?

I have a Panel1 and button1 on my webform. At runtime, I create 2 textboxes.
I do it at the Page_Load event. I put the code within the " If Not
isPostBack"

For the button click event, I will do a post-to-database coding and then
show the same page again. I put EnableViewState = True for the textboxes.

In the Page_Load if I dont use the IsPostBack checking, the code works fine.
The button shows that the code has been fired and the page is shown after
the network round trip with the values in the textboxes as it is.
If I put the IsPostBack checking, after the button click , the page shows
up with a empty Panel1 and a button.

What I want to know is, Shouldnt I be using the isPostBack checking ?
because otherwise the page will be recreating the textboxes everytime when
the button is clicked ?

What are the issues about the IsPostBack checking when controls are creating
at runtime. Please give ideas.
Here is the code.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

'Put user code to initialize the page here

Dim t1 As New TextBox()

Dim t2 As New TextBox()

t1.Text = "sagar"

t1.ID = "t1"

t1.EnableViewState = True

'AddHandler t1.TextChanged, AddressOf DoThis

Panel1.Controls.Add(t1)

t2.Text = "Tres"

t2.ID = "t2"

t2.EnableViewState = True

Panel1.Controls.Add(t2)

End If

End Sub

Sub DoThis(ByVal sender As Object, ByVal e As EventArgs)

Response.Write("feeling great")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim c As Control

Dim t As TextBox

For Each c In Panel1.Controls

If c.GetType.ToString = "System.Web.UI.WebControls.TextBox" Then

Response.Write("-------")

Response.Write("ClientID = " & c.ClientID)

Response.Write("ID = " & c.ID)

Response.Write("Unique ID = " & c.UniqueID)

Response.Write("GetType = " & c.GetType.ToString)

t = CType(c, TextBox)

Response.Write("text = " & t.Text)

Response.Write("<p>")

End If

Next

End Sub

Thanks,
Anand Sagar.
Nov 18 '05 #1
2 2873
I think for the page to work, IsPostBack check should not be done.

run time binds the view state and fires the Post events once controls tree
structure is built. So, if your runtime controls are not created again,
postback events will not fire for them.

i dont see any issues recreating the runtime controls. for that matter, all
controls are recreated for every post back is int?

Av.
"Anand Sagar" <ba*****@vermar.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl...
I have a Panel1 and button1 on my webform. At runtime, I create 2
textboxes.
I do it at the Page_Load event. I put the code within the " If Not
isPostBack"

For the button click event, I will do a post-to-database coding and then
show the same page again. I put EnableViewState = True for the textboxes.

In the Page_Load if I dont use the IsPostBack checking, the code works
fine.
The button shows that the code has been fired and the page is shown after
the network round trip with the values in the textboxes as it is.
If I put the IsPostBack checking, after the button click , the page shows
up with a empty Panel1 and a button.

What I want to know is, Shouldnt I be using the isPostBack checking ?
because otherwise the page will be recreating the textboxes everytime when
the button is clicked ?

What are the issues about the IsPostBack checking when controls are
creating
at runtime. Please give ideas.
Here is the code.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

'Put user code to initialize the page here

Dim t1 As New TextBox()

Dim t2 As New TextBox()

t1.Text = "sagar"

t1.ID = "t1"

t1.EnableViewState = True

'AddHandler t1.TextChanged, AddressOf DoThis

Panel1.Controls.Add(t1)

t2.Text = "Tres"

t2.ID = "t2"

t2.EnableViewState = True

Panel1.Controls.Add(t2)

End If

End Sub

Sub DoThis(ByVal sender As Object, ByVal e As EventArgs)

Response.Write("feeling great")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim c As Control

Dim t As TextBox

For Each c In Panel1.Controls

If c.GetType.ToString = "System.Web.UI.WebControls.TextBox" Then

Response.Write("-------")

Response.Write("ClientID = " & c.ClientID)

Response.Write("ID = " & c.ID)

Response.Write("Unique ID = " & c.UniqueID)

Response.Write("GetType = " & c.GetType.ToString)

t = CType(c, TextBox)

Response.Write("text = " & t.Text)

Response.Write("<p>")

End If

Next

End Sub

Thanks,
Anand Sagar.

Nov 18 '05 #2
Sam
You need to recreate *all* the controls *every time in the same way*
during PageLoad (or even better in Init). Everything is recreated
from scratch on the server for every postback (and every request for
that matter). In fact, if its not ViewState could get corrupted.

People generally only use if (Postback==false) for database *loading*
so that the database isn't hit unnecessarily on postbacks.

BTW, I dont' think you need to enable Viewstate on the textboxes to
get all this to work (unless you are looking for TextChanged events).
The form data will fill in the textboxes for you.

"Anand Sagar" <ba*****@vermar.com> wrote in message news:<uL**************@TK2MSFTNGP12.phx.gbl>...
I have a Panel1 and button1 on my webform. At runtime, I create 2 textboxes.
I do it at the Page_Load event. I put the code within the " If Not
isPostBack"

For the button click event, I will do a post-to-database coding and then
show the same page again. I put EnableViewState = True for the textboxes.

In the Page_Load if I dont use the IsPostBack checking, the code works fine.
The button shows that the code has been fired and the page is shown after
the network round trip with the values in the textboxes as it is.
If I put the IsPostBack checking, after the button click , the page shows
up with a empty Panel1 and a button.

What I want to know is, Shouldnt I be using the isPostBack checking ?
because otherwise the page will be recreating the textboxes everytime when
the button is clicked ?

What are the issues about the IsPostBack checking when controls are creating
at runtime. Please give ideas.
Here is the code.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

'Put user code to initialize the page here

Dim t1 As New TextBox()

Dim t2 As New TextBox()

t1.Text = "sagar"

t1.ID = "t1"

t1.EnableViewState = True

'AddHandler t1.TextChanged, AddressOf DoThis

Panel1.Controls.Add(t1)

t2.Text = "Tres"

t2.ID = "t2"

t2.EnableViewState = True

Panel1.Controls.Add(t2)

End If

End Sub

Sub DoThis(ByVal sender As Object, ByVal e As EventArgs)

Response.Write("feeling great")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim c As Control

Dim t As TextBox

For Each c In Panel1.Controls

If c.GetType.ToString = "System.Web.UI.WebControls.TextBox" Then

Response.Write("-------")

Response.Write("ClientID = " & c.ClientID)

Response.Write("ID = " & c.ID)

Response.Write("Unique ID = " & c.UniqueID)

Response.Write("GetType = " & c.GetType.ToString)

t = CType(c, TextBox)

Response.Write("text = " & t.Text)

Response.Write("<p>")

End If

Next

End Sub

Thanks,
Anand Sagar.

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matthew | last post by:
After selecting an option from the DropDownList and posting back, the SelectedIndex for the DropDownList (ddlShippingMethods) remains at -1. This form inherits from a base class. There is no code...
2
by: Earl Teigrob | last post by:
I have run into a situation where I need to run the !IsPostBack code under one circumstance, even if it is a postback. Something that may complicate matters more is that this is a double postback...
3
by: Alan Pretre | last post by:
I have a a WebForms.Table control that I am populating in Page_Load(). One of the columns has cells with a button in each row, which I wire up to its Click event handler. Everything is fine if I...
14
by: Mike | last post by:
I have a routine that i only want to execute if a variable is set to Y. The routine is getting some information on the screen then redirecting to a new page. How can i passed a variable to the...
4
by: sebastien | last post by:
Hi, In page_load : If Not Page.IsPostBack Then Dim myUC As UserControl = LoadControl("ficheProspects.ascx") myUC.ID = "FicheProspects1" End If In another Sub i need to find my control :
5
by: CBKowitz | last post by:
I have a page that calls a public sub that is in the code behind page. This sub will, depending on the number of records, write out a number of statements. One of the statements that is repeated...
3
by: serge calderara | last post by:
Dear all, Is there any particular rules on the type of code we usually place inside the Page load event and checking the IsPostback status ? As a beginner I could imagine to put quite many...
0
by: TB | last post by:
Hi All For reasons unknown to me, the Page.IsPostBack is always false when I click a link in a hyperlink column of a datagrid which is created programmatically. My code in a page called...
0
by: DC | last post by:
Hi, I am dynamically adding a usercontrol that uses "this.IsPostBack" in Page_Load to decide whether it must populate some of it's inner controls or not. Since I am adding the usercontrol to the...
5
by: BM | last post by:
I have a question that seems like it should have a simple answer, but I can't seem to find it by searching... Anyway, I'm trying to capture the IsPostBack event when I select an item within an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.