Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > UserControl values lost on PostBack

Reply
Thread Tools

UserControl values lost on PostBack

 
 
paul.hester@gmail.com
Guest
Posts: n/a
 
      08-30-2006
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 tried
enabling viewstate everywhere without success. Could anyone shed any
light on what's going wrong?

Thanks,

Paul

Asx:

<%@ Control Language="C#" EnableViewState="true" AutoEventWireup="true"
CodeFile="DayMonthYear.ascx.cs" Inherits="Test.DayMonthYear" %>
<%@ Register TagPrefix="test" Namespace="Test" %>
<table cellpadding="1" cellspacing="0">
<tr>
<td><small>Day</small></td>
<td><small>Month</small></td>
<td><small>Year</small></td>
</tr>
<tr>
<td><aspropDownList ID="day" EnableViewState="true"
runat="server"></aspropDownList></td>
<td><aspropDownList ID="month" EnableViewState="true"
runat="server"></aspropDownList></td>
<td><aspropDownList ID="year" EnableViewState="true"
runat="server"></aspropDownList></td>
</tr>
</table>

Asx.Cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Test
{
public partial class DayMonthYear : System.Web.UI.UserControl,
INamingContainer
{
private int _yearHigh = DateTime.Now.Year;
private int _yearLow = DateTime.Now.Year - 100;

protected void Page_PreRender(object sender, EventArgs e)
{
string[] months = new string[] { "January", "February", "March",
"April", "May", "June", "July", "August", "September", "October",
"November", "December" };

// add days
day.Items.Add(new ListItem(string.Empty, "0"));
for (int i = 1; i < 32; i++)
day.Items.Add(i.ToString());

// add months
month.Items.Add(new ListItem(string.Empty, "0"));
for (int i = 0; i < months.Length; i++)
month.Items.Add(new ListItem(months[i], (i + 1) + string.Empty));

// add years
year.Items.Add(new ListItem(string.Empty, "0"));
for (int i = YearLow; i <= YearHigh; i++)
year.Items.Add(i.ToString());
}

public int YearHigh
{
get { return _yearHigh; }
set { _yearHigh = value; }
}

public int YearLow
{
get { return _yearLow; }
set { _yearLow = value; }
}
}
}

 
Reply With Quote
 
 
 
 
offwhite@gmail.com
Guest
Posts: n/a
 
      08-31-2006
In a User Control you will lose the PostBack value if you cause data
binding to happen before you can get the values.

You may want to use code like this...


if (!Page.IsPostBack) {
... bind month controls ...
}

In your case I see the binding is done manually on PreRender. For
starters that is a curious place to put the binding. Normally you want
it done in the Load event handler.

Look at the page life cycle and see where it loads the ViewState and
where the other events are handled in the life cycle.

http://msdn2.microsoft.com/en-us/library/ms178472.aspx

Brennan Stehling
http://brennan.offwhite.net/blog/

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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 tried
> enabling viewstate everywhere without success. Could anyone shed any
> light on what's going wrong?
>
> Thanks,
>
> Paul
>
> Asx:
>
> <%@ Control Language="C#" EnableViewState="true" AutoEventWireup="true"
> CodeFile="DayMonthYear.ascx.cs" Inherits="Test.DayMonthYear" %>
> <%@ Register TagPrefix="test" Namespace="Test" %>
> <table cellpadding="1" cellspacing="0">
> <tr>
> <td><small>Day</small></td>
> <td><small>Month</small></td>
> <td><small>Year</small></td>
> </tr>
> <tr>
> <td><aspropDownList ID="day" EnableViewState="true"
> runat="server"></aspropDownList></td>
> <td><aspropDownList ID="month" EnableViewState="true"
> runat="server"></aspropDownList></td>
> <td><aspropDownList ID="year" EnableViewState="true"
> runat="server"></aspropDownList></td>
> </tr>
> </table>
>
> Asx.Cs:
>
> using System;
> using System.Data;
> using System.Configuration;
> using System.Collections;
> using System.Web;
> using System.Web.Security;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.WebControls.WebParts;
> using System.Web.UI.HtmlControls;
>
> namespace Test
> {
> public partial class DayMonthYear : System.Web.UI.UserControl,
> INamingContainer
> {
> private int _yearHigh = DateTime.Now.Year;
> private int _yearLow = DateTime.Now.Year - 100;
>
> protected void Page_PreRender(object sender, EventArgs e)
> {
> string[] months = new string[] { "January", "February", "March",
> "April", "May", "June", "July", "August", "September", "October",
> "November", "December" };
>
> // add days
> day.Items.Add(new ListItem(string.Empty, "0"));
> for (int i = 1; i < 32; i++)
> day.Items.Add(i.ToString());
>
> // add months
> month.Items.Add(new ListItem(string.Empty, "0"));
> for (int i = 0; i < months.Length; i++)
> month.Items.Add(new ListItem(months[i], (i + 1) + string.Empty));
>
> // add years
> year.Items.Add(new ListItem(string.Empty, "0"));
> for (int i = YearLow; i <= YearHigh; i++)
> year.Items.Add(i.ToString());
> }
>
> public int YearHigh
> {
> get { return _yearHigh; }
> set { _yearHigh = value; }
> }
>
> public int YearLow
> {
> get { return _yearLow; }
> set { _yearLow = value; }
> }
> }
> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UserControl with databound repeater drops values on postback paul.hester@gmail.com ASP .Net Building Controls 3 09-02-2006 07:11 AM
UserControl with databound repeater drops values on postback Paul ASP .Net 0 09-01-2006 04:42 AM
UserControl values lost on PostBack paul.hester@gmail.com ASP .Net Building Controls 1 08-30-2006 11:04 AM
UserControl values lost on PostBack Paul ASP .Net 1 08-30-2006 07:15 AM
Postback problem with usercontrol in usercontrol Mark van Bree ASP .Net Web Controls 1 05-30-2006 08:27 PM



Advertisments