UserControl with databound repeater drops values on postback

Discussion in 'ASP .Net Building Controls' started by paul.hester@gmail.com, Sep 1, 2006.

  1. Guest

    Hi all,

    I have a user control that contains a repeater that generates a list of
    check boxes. The checkboxes render fine, but they don't maintain their
    checked state on postback.

    I've tried using Page_Load, Page_Init, OnInit, enabling and disabling
    viewstate without success. Any help would be appreciated.

    Thanks,

    Paul

    ASCX:

    <asp:Repeater ID="countryList" runat="server">
    <HeaderTemplate>
    <table cellpadding="2" cellspacing="0">
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td><asp:CheckBox ID="countryID" Value='<%# Eval("CountryID") %>'
    runat="server" /></td>
    <td><asp:Label AssociatedControlID="countryID" runat="server"><%#
    Eval("Name") %></asp:Label></td>
    <td><img src="<%# string.Format("/images/flags/{0}.gif",
    Eval("CountryID")) %>" height="13" width="24" /></td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>

    ASCX.CS:

    protected void Page_Init(object sender, EventArgs e)
    {
    // get countries
    countryList.DataSource = Country.GetCountries();
    DataBind();
    }
    , Sep 1, 2006
    #1
    1. Advertising

  2. Teemu Keiski Guest

    Hi,

    you must not run DataBind on every postback in the control since it clears
    the selections. And second, you need to do binding in Page_Load inside
    if(!IsPostBack check), essentially:

    protected void Page_Load(object sender, EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    // get countries
    countryList.DataSource = Country.GetCountries();
    countryList.DataBind();
    }
    }

    If you need to reload the data, then do DataBind as needed but not on every
    request.
    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    <> wrote in message
    news:...
    > Hi all,
    >
    > I have a user control that contains a repeater that generates a list of
    > check boxes. The checkboxes render fine, but they don't maintain their
    > checked state on postback.
    >
    > I've tried using Page_Load, Page_Init, OnInit, enabling and disabling
    > viewstate without success. Any help would be appreciated.
    >
    > Thanks,
    >
    > Paul
    >
    > ASCX:
    >
    > <asp:Repeater ID="countryList" runat="server">
    > <HeaderTemplate>
    > <table cellpadding="2" cellspacing="0">
    > </HeaderTemplate>
    > <ItemTemplate>
    > <tr>
    > <td><asp:CheckBox ID="countryID" Value='<%# Eval("CountryID") %>'
    > runat="server" /></td>
    > <td><asp:Label AssociatedControlID="countryID" runat="server"><%#
    > Eval("Name") %></asp:Label></td>
    > <td><img src="<%# string.Format("/images/flags/{0}.gif",
    > Eval("CountryID")) %>" height="13" width="24" /></td>
    > </tr>
    > </ItemTemplate>
    > <FooterTemplate>
    > </table>
    > </FooterTemplate>
    > </asp:Repeater>
    >
    > ASCX.CS:
    >
    > protected void Page_Init(object sender, EventArgs e)
    > {
    > // get countries
    > countryList.DataSource = Country.GetCountries();
    > DataBind();
    > }
    >
    Teemu Keiski, Sep 1, 2006
    #2
    1. Advertising

  3. Guest

    Hi Teemu,

    Thanks for getting back to me. That doesn't work, unfortunately. I can
    take all of the code out of the user control and put it in my page and
    the checkboxes maintain their values perfectly on postback, but
    something about having it in a UserControl stops this from working.

    Any help would be appreciated.

    Thanks,

    Paul

    Teemu Keiski wrote:
    > Hi,
    >
    > you must not run DataBind on every postback in the control since it clears
    > the selections. And second, you need to do binding in Page_Load inside
    > if(!IsPostBack check), essentially:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if(!Page.IsPostBack)
    > {
    > // get countries
    > countryList.DataSource = Country.GetCountries();
    > countryList.DataBind();
    > }
    > }
    >
    > If you need to reload the data, then do DataBind as needed but not on every
    > request.
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > http://blogs.aspadvice.com/joteke
    >
    > <> wrote in message
    > news:...
    > > Hi all,
    > >
    > > I have a user control that contains a repeater that generates a list of
    > > check boxes. The checkboxes render fine, but they don't maintain their
    > > checked state on postback.
    > >
    > > I've tried using Page_Load, Page_Init, OnInit, enabling and disabling
    > > viewstate without success. Any help would be appreciated.
    > >
    > > Thanks,
    > >
    > > Paul
    > >
    > > ASCX:
    > >
    > > <asp:Repeater ID="countryList" runat="server">
    > > <HeaderTemplate>
    > > <table cellpadding="2" cellspacing="0">
    > > </HeaderTemplate>
    > > <ItemTemplate>
    > > <tr>
    > > <td><asp:CheckBox ID="countryID" Value='<%# Eval("CountryID") %>'
    > > runat="server" /></td>
    > > <td><asp:Label AssociatedControlID="countryID" runat="server"><%#
    > > Eval("Name") %></asp:Label></td>
    > > <td><img src="<%# string.Format("/images/flags/{0}.gif",
    > > Eval("CountryID")) %>" height="13" width="24" /></td>
    > > </tr>
    > > </ItemTemplate>
    > > <FooterTemplate>
    > > </table>
    > > </FooterTemplate>
    > > </asp:Repeater>
    > >
    > > ASCX.CS:
    > >
    > > protected void Page_Init(object sender, EventArgs e)
    > > {
    > > // get countries
    > > countryList.DataSource = Country.GetCountries();
    > > DataBind();
    > > }
    > >
    , Sep 1, 2006
    #3
  4. Teemu Keiski Guest

    And how do you add the UC to the Page?

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    <> wrote in message
    news:...
    > Hi Teemu,
    >
    > Thanks for getting back to me. That doesn't work, unfortunately. I can
    > take all of the code out of the user control and put it in my page and
    > the checkboxes maintain their values perfectly on postback, but
    > something about having it in a UserControl stops this from working.
    >
    > Any help would be appreciated.
    >
    > Thanks,
    >
    > Paul
    >
    > Teemu Keiski wrote:
    >> Hi,
    >>
    >> you must not run DataBind on every postback in the control since it
    >> clears
    >> the selections. And second, you need to do binding in Page_Load inside
    >> if(!IsPostBack check), essentially:
    >>
    >> protected void Page_Load(object sender, EventArgs e)
    >> {
    >> if(!Page.IsPostBack)
    >> {
    >> // get countries
    >> countryList.DataSource = Country.GetCountries();
    >> countryList.DataBind();
    >> }
    >> }
    >>
    >> If you need to reload the data, then do DataBind as needed but not on
    >> every
    >> request.
    >> --
    >> Teemu Keiski
    >> ASP.NET MVP, AspInsider
    >> Finland, EU
    >> http://blogs.aspadvice.com/joteke
    >>
    >> <> wrote in message
    >> news:...
    >> > Hi all,
    >> >
    >> > I have a user control that contains a repeater that generates a list of
    >> > check boxes. The checkboxes render fine, but they don't maintain their
    >> > checked state on postback.
    >> >
    >> > I've tried using Page_Load, Page_Init, OnInit, enabling and disabling
    >> > viewstate without success. Any help would be appreciated.
    >> >
    >> > Thanks,
    >> >
    >> > Paul
    >> >
    >> > ASCX:
    >> >
    >> > <asp:Repeater ID="countryList" runat="server">
    >> > <HeaderTemplate>
    >> > <table cellpadding="2" cellspacing="0">
    >> > </HeaderTemplate>
    >> > <ItemTemplate>
    >> > <tr>
    >> > <td><asp:CheckBox ID="countryID" Value='<%# Eval("CountryID") %>'
    >> > runat="server" /></td>
    >> > <td><asp:Label AssociatedControlID="countryID" runat="server"><%#
    >> > Eval("Name") %></asp:Label></td>
    >> > <td><img src="<%# string.Format("/images/flags/{0}.gif",
    >> > Eval("CountryID")) %>" height="13" width="24" /></td>
    >> > </tr>
    >> > </ItemTemplate>
    >> > <FooterTemplate>
    >> > </table>
    >> > </FooterTemplate>
    >> > </asp:Repeater>
    >> >
    >> > ASCX.CS:
    >> >
    >> > protected void Page_Init(object sender, EventArgs e)
    >> > {
    >> > // get countries
    >> > countryList.DataSource = Country.GetCountries();
    >> > DataBind();
    >> > }
    >> >

    >
    Teemu Keiski, Sep 2, 2006
    #4
    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. Paul
    Replies:
    0
    Views:
    424
  2. rodchar
    Replies:
    0
    Views:
    393
    rodchar
    Nov 26, 2007
  3. Manu
    Replies:
    0
    Views:
    166
  4. Mark van Bree

    Postback problem with usercontrol in usercontrol

    Mark van Bree, May 30, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    163
  5. Replies:
    0
    Views:
    126
Loading...

Share This Page