Repeater DataBinding on PostBack and base.DataBind()

O

Oleg

Hi,

Can anyone please clarify why Repeater does not maintain its viewstate when
the base.DataBind() call is made on Page_Load.
Example that illustrates this problem is here:

1. protected void Page_Load(object sender, EventArgs e) {
2.
3. base.DataBind();
4.
5. if (!IsPostBack) {
6. string[] source = new string[] { "1", "2", "3" };
7. ddlTest.DataSource = source;
8. ddlTest.DataBind();
9.
10. rptTest.DataSource = source;
11. rptTest.DataBind();
12.
13. }
14.
15. }


When the page goes through the postabck DropDownList maintain its state, but
repeater is not.
Removing base.DataBind(); on line 3 solves the problem.

Why is this difference between the DropDownList and Repeater?

Does this problem means that Repeater needs to be DataBound on both PostBack
and !Postback ?

The only reason I have base.DataBind() on the page is to void this error
"The Controls collection cannot be modified because the control contains
code blocks (i.e. <% ... %>)"
This is because I have following validation code on aspx page

1. var ddl = document.forms[0].<%#ddlTest.ClientID%>;

Thanks in advance,
Oleg
 
B

bruce barker

the repeater needs to be data bound on postback. as postback data is
applied before page load, the databind should be done in oninit. to do
the databind itself on postback, the repeater would need to store the
datasource in viewstate (a hidden field on the form) via serialization.
this is too heavy a load.

the dropdown list usually is smaller, and storing a copy of the list in
a hidden field is not too expensive, but you are a better of disabling
viewstate and loading it in oninit to make the page size smaller.

-- bruce (sqlwork.com)
 

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,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top