UserControl values lost on PostBack

Discussion in 'ASP .Net Web Controls' started by paul.hester@gmail.com, Aug 30, 2006.

  1. Guest

    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><asp:DropDownList ID="day" EnableViewState="true"
    runat="server"></asp:DropDownList></td>
    <td><asp:DropDownList ID="month" EnableViewState="true"
    runat="server"></asp:DropDownList></td>
    <td><asp:DropDownList ID="year" EnableViewState="true"
    runat="server"></asp:DropDownList></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 + 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; }
    }
    }
    }
    , Aug 30, 2006
    #1
    1. Advertising

  2. Guest

    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/

    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><asp:DropDownList ID="day" EnableViewState="true"
    > runat="server"></asp:DropDownList></td>
    > <td><asp:DropDownList ID="month" EnableViewState="true"
    > runat="server"></asp:DropDownList></td>
    > <td><asp:DropDownList ID="year" EnableViewState="true"
    > runat="server"></asp:DropDownList></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 + 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; }
    > }
    > }
    > }
    , Aug 31, 2006
    #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. Paul
    Replies:
    1
    Views:
    392
    =?Utf-8?B?UHJlbSBLdW1hcg==?=
    Aug 30, 2006
  2. Paul
    Replies:
    0
    Views:
    420
  3. UserControl values lost on PostBack

    , Aug 30, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    147
  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:
    159
  5. Bryan

    Dynamic gridview values lost on postback

    Bryan, Aug 22, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    1,045
    Vadivel Kumar
    Aug 23, 2006
Loading...

Share This Page