By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.