Jose Saiz

Jose Saiz

  • 1.8k
  • 161
  • 39k

Help with ASP.NET Server Custom Control

Sep 25 2014 7:01 AM

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

my ASP.NET page .aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Namespace="myFirstListingControl" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Testing</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>                           
        <cc1:YearListing ID="ddlYear" runat="server" />
        <asp:Button ID="butSubmit" Text="Submit" OnClick="butSubmit_Click" runat="server" /> 
        <asp:Label ID="lblShow" runat="server"></asp:Label>   
    </div>
    </form>
</body>
</html>
 
my Code behind .aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //this.ddlYear.DataBind();
        }
    }
    protected void butSubmit_Click(object sender, EventArgs e)
    {
        //- the .Text property does not store the value selected it is always blank. Why?
        lblShow.Text = ddlYear.Text;
        if (lblShow.Text.Trim() == "")
        {
            //- This one works fine using the form value
            lblShow.Text = "You've Seleted " + Request.Form["ddlYear"].ToString();
        }
    }
 
Please help this is driving nuts
 
Thanks
 

Brought to you by: