Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / August 2006

Tip: Looking for answers? Try searching our database.

UserControl values lost on PostBack

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
paul.hester@gmail.com - 30 Aug 2006 02:43 GMT
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], (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; }
        }
    }
}
Riki - 30 Aug 2006 12:04 GMT
> Hi all,
>
[quoted text clipped - 84 lines]
> }
> }

If I understand your code well, you will get 12 months the first time,
24 after the first postback etc.

If so, in Pre_Render, use an if(!Page.IsPostBack) block for adding the
items. In other words, don't add items on postback again.

Signature

Riki


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.