Professional Web Applications Themes

UserControl with databound repeater drops values on postback - ASP.NET Building Controls

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

  1. #1

    Default UserControl with databound repeater drops values on postback

    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();
    }

    paul.hester@gmail.com Guest

  2. #2

    Default Re: UserControl with databound repeater drops values on postback

    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
    [url]http://blogs.aspadvice.com/joteke[/url]

    <paul.hester> wrote in message
    news:1157085701.475148.52270e3g2000cwe.googlegrou ps.com...
    > 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 Guest

  3. #3

    Default Re: UserControl with databound repeater drops values on postback

    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
    > [url]http://blogs.aspadvice.com/joteke[/url]
    >
    > <paul.hester> wrote in message
    > news:1157085701.475148.52270e3g2000cwe.googlegrou ps.com...
    > > 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();
    > > }
    > >
    paul.hester@gmail.com Guest

  4. #4

    Default Re: UserControl with databound repeater drops values on postback

    And how do you add the UC to the Page?

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

    <paul.hester> wrote in message
    news:1157145498.028319.319360h48g2000cwc.googlegr oups.com...
    > 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
    >> [url]http://blogs.aspadvice.com/joteke[/url]
    >>
    >> <paul.hester> wrote in message
    >> news:1157085701.475148.52270e3g2000cwe.googlegrou ps.com...
    >> > 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 Guest

Similar Threads

  1. UserControl has no DataBound event?
    By crpietschmann in forum ASP.NET Building Controls
    Replies: 1
    Last Post: September 20th, 07:21 PM
  2. UserControl values lost on PostBack
    By paul.hester@gmail.com in forum ASP.NET Building Controls
    Replies: 1
    Last Post: August 30th, 11:04 AM
  3. UserControls and DataBound Repeater
    By Alex Maghen in forum ASP.NET Building Controls
    Replies: 2
    Last Post: June 15th, 01:08 PM
  4. Newbie:Access custom Itemplate(not datagrid/repeater/datalist) control values on postback
    By Luhar Powell via .NET 247 in forum ASP.NET Building Controls
    Replies: 0
    Last Post: April 1st, 04:59 PM
  5. .NET databound radiolistbutton in a repeater
    By Rod Snyder in forum Dreamweaver AppDev
    Replies: 3
    Last Post: February 18th, 11:36 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139