Solved

URGENT: DropDownList in custom server control not holding value on postback

Posted on 2009-04-06
3
741 Views
Last Modified: 2012-05-06
Hello All,

I have a problem where a DropDownList contained within one of my custom server controls is not holding its value on postback.  I've tried several different things but can't seem to find why it's dropping its value.  ViewState is enabled on the page and I've not specifically disabled ViewState on any of the controls.

Below is the code specific to the dropdownlist in the custom control.

Desperate to solve this quickly...
Anubis
public class DatePicker : WebControl

  {
 

    // CREATE CONTROLS

    public DropDownList Month = new DropDownList();

    

    // SET PROPERTIES

    protected string _MonthDefault = "";

    protected string _MonthCssClass = "";
 

    // RENDER CLIENT CONTROLS

    protected override void CreateChildControls()

    {
 

	// CREATE MONTH CONTROL

	Month.ID = this.ID + "Month";

	Month.CssClass = _MonthCssClass;
 

	string[] _MonthOptions = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

	string[] _MonthOptionsValues = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };

	for (int i = 0; i < _MonthOptions.Length; i++)

	{

	  ListItem _MonthFieldItem = new ListItem();

	  _MonthFieldItem.Value = _MonthOptionsValues[i];

	  _MonthFieldItem.Text = _MonthOptions[i];

	  Month.Items.Add(_MonthFieldItem);

	}
 

	// CHECK FOR DEFAULT SELECTION

	if (!String.IsNullOrEmpty(_MonthDefault) && !Page.IsPostBack)

	{

	  if (_MonthDefault == "NOW")

	  {

	    foreach (ListItem i in Month.Items)

	    {

	      if (i.Value == _MonthOptionsValues[(DateTime.Now.Month - 1)])

	      {

		i.Selected = true;

		break;

	      }

	    }

	  }

	  else

	  {

	    foreach (ListItem i in Month.Items)

	    {

	      if (i.Value == _MonthDefault)

	      {

		i.Selected = true;

		break;

	      }

	    }

	  }

	}
 

	// APPEND CONTROL

	this.Controls.Add(Month);
 

      } // END IF
 
 

    } // END VOID
 
 

  } // END CLASS

Open in new window

0
Comment
Question by:Anubis2005
  • 2
3 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24084149
is that custom control a dynamic control
if yes then you need to handle the custom controls in a little different way
http://www.4guysfromrolla.com/articles/092904-1.aspx
0
 
LVL 1

Author Comment

by:Anubis2005
ID: 24084261
Hello ragi0017,

Thanks for the comment.  I had thought that solved my problem but once I had read through the document, I think I'm still missing something.

In my asp.net page, I'm calling the 'DatePicker' class directly in the ASP.NET code (not the code behind).

As:

<as:DatePicker runat="server" ID="MonthSelection" />

As far as I can tell, shouldn't calling the control this way place it's contents in the Init of the page?  If so, then there must still be something missing?

I have seen (using the disassembler on the .net assembleys) that most of the controls are generated using the Render method, could it be a problem that I'm using CreateChildControls and not the Render method?  If so, how could I convert the above code to function in Render?

Thanks
Anubis.

0
 
LVL 1

Accepted Solution

by:
Anubis2005 earned 0 total points
ID: 24121080
Solved the problem myself.  As I was including several drop-down's in the one control, I needed to inherit from CompositeControl instead of WebControl.

Once inheriting from CompositeControl everything worked as expected.

Thanks
Anubis.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now