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

A

Anand Sagar

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.
 
A

avnrao

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.
 
S

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top