Hi guys, I am having problem collecting data from the viewstate I thought I was doing the correct coding to capture the value selected from a custom dropdownlist control but apparently I am not here it goes my custom control code
using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Linq;using System.Text;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;
namespace myFirstListingControl{ [DefaultProperty("Text")] [ToolboxData("<{0}:YearListing runat=server></{0}:YearListing>")] public class YearListing : WebControl { [Bindable(true)] [Category("Appearance")] [DefaultValue("")] [Localizable(true)] public string Text { get { String s = (String)ViewState["Text"]; //return ((s == null) ? "[" + this.ID + "]" : s); return (s == null) ? String.Empty : s; } set { ViewState["Text"] = value; } } DropDownList _ddlPeriodYear = new DropDownList(); protected override void RenderContents(HtmlTextWriter output) { BuildDropDownYearListingControl(0); _ddlPeriodYear.RenderControl(output); LoadDataFromViewState(); } //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //- Return a drop-downlist control with 3 years worth of databound. (Inside the toolbar) //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- protected void BuildDropDownYearListingControl(int pNumberOfYear) { _ddlPeriodYear.EnableViewState = true; _ddlPeriodYear.ID = this.ID; _ddlPeriodYear.Text = this.Text; _ddlPeriodYear.AutoPostBack = true; //--_ddlPeriodYear.Style["width"] = "250px"; _ddlPeriodYear.Items.Clear(); //- 3 Years worth of year data. if (pNumberOfYear == 0) { _ddlPeriodYear.Items.Add(DateTime.Now.AddYears(-2).Year.ToString()); _ddlPeriodYear.Items.Add(DateTime.Now.AddYears(-1).Year.ToString()); } else { for (int iYears = 1; iYears <= pNumberOfYear; iYears++) { _ddlPeriodYear.Items.Add(DateTime.Now.AddYears(-(pNumberOfYear-iYears)).Year.ToString()); } } _ddlPeriodYear.Items.Add(DateTime.Now.Year.ToString()); _ddlPeriodYear.SelectedValue = DateTime.Now.Year.ToString(); this.Controls.Add(_ddlPeriodYear); } private void LoadDataFromViewState() { if (this.Context != null) { foreach (string key in this.Page.Request.Form.AllKeys) { if (key.ToLower().Contains(this._ddlPeriodYear.UniqueID.ToLower())) { this.Text = this.Page.Request.Form[key]; } } } } }}