Repeater DataBinding on PostBack and base.DataBind()

Discussion in 'ASP .Net' started by Oleg, Sep 25, 2009.

  1. Oleg

    Oleg Guest

    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
     
    Oleg, Sep 25, 2009
    #1
    1. Advertising

  2. Oleg

    bruce barker Guest

    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)




    Oleg wrote:
    > 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
     
    bruce barker, Sep 25, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    530
    Karl Seguin [MVP]
    Oct 11, 2006
  2. jobs
    Replies:
    0
    Views:
    1,767
  3. Michael
    Replies:
    0
    Views:
    245
    Michael
    Dec 29, 2003
  4. Earl Teigrob
    Replies:
    1
    Views:
    444
    Shravan
    Mar 1, 2004
  5. Joel Finkel

    ItemCommand handler and DataBind on Postback

    Joel Finkel, Aug 16, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    188
Loading...

Share This Page