Ask a Question related to ASP.NET Building Controls, Design and Development.
-
paul.hester@gmail.com #1
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
-
UserControl has no DataBound event?
I have a UserControl that is used inside a datalist and I am databinding a could of the UserControl's properties to the data displayed in the... -
UserControl values lost on PostBack
Hi all, I have a very simple user control that contains 3 drop downs. Whenever there's a postback the values of these drop downs are lost. I've... -
UserControls and DataBound Repeater
I've built a few UserControls that have worked perfectly for a while. Now, for the first time, I want to use these controls inside a Data-Bound... -
Newbie:Access custom Itemplate(not datagrid/repeater/datalist) control values on postback
Newbie:Access custom Itemplate(not datagrid/repeater/datalist) control values on postback Hi, I have simple Itemplate implementation with 2... -
.NET databound radiolistbutton in a repeater
I have a databound radiobuttonlist in a repeater and I am at a loss to figure out how to set a selected item at runtime? Any help would be... -
Teemu Keiski #2
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@gmail.com> wrote in message
news:1157085701.475148.52270@e3g2000cwe.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
-
paul.hester@gmail.com #3
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@gmail.com> wrote in message
> news:1157085701.475148.52270@e3g2000cwe.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
-
Teemu Keiski #4
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@gmail.com> wrote in message
news:1157145498.028319.319360@h48g2000cwc.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@gmail.com> wrote in message
>> news:1157085701.475148.52270@e3g2000cwe.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



Reply With Quote

