UserControl values lost on PostBack

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default 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 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; }
    }
    }
    }

    paul.hester@gmail.com Guest

  2. Similar Questions and Discussions

    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...
    2. Usercontrol always needs a second postback
      I folks, I have a simple page with a user control dropped on it, a textbox and a submit button. That usercontrol should display the value entered...
    3. Usercontrol Postback - can i intercept Request
      Hi I have a site that creates a template file "default.aspx" and places usercontrols in it to display the pages. The beginrequest event redirects...
    4. PostBack and UserControl...events
      Hello, You should create attribute for your UserControl. For example: DataKey ----------- WebUserControl1.ascx.cs ------------- public...
    5. Problem with UserControl Property in a DataGrid after Postback
      Hello, I have this page.aspx ---------- <%@ Register TagPrefix="uc1" TagName="Userdetails" Src="Userdetails.ascx" %> ... <asp:datagrid...
  3. #2

    Default Re: UserControl values lost on PostBack

    [email]paul.hester@gmail.com[/email] 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], (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; }
    > }
    > }
    > }
    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.

    --

    Riki


    Riki Guest

Posting Permissions

  • You may not post new threads
  • You may 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