473,382 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Yet Another DropDownList.SelectedIndexChanged Dufus

Yep, that's me. I'll bet I've read a hundred articles somewhere or another, but I just can't get the thing to work.

I'm working on a custom solution. One of the major UIs includes a calendar-style presentation. The navigation controls at the top of the calendar include four LinkButton controls (prevYear, prevMonth, nextMonth, nextYear) and two DropDownList controls (monthSelector, yearSelector). The DropDownList webcontrols are set with AutoPostBack=true. I have attached handlers to their SelectedIndexChanged events.

All of the LinkButtons work perfectly. The DropDownLists successfully AutoPostBack. But I can't get my SelectedIndexChanged handler to execute to save my life. Nagging me back in the recesses of my brain is the suspicion that I have solved this one before, but I still just cannot seem to get this to work.

Here's the Control Tree from the Trace, so you can see that these DDLs are buried pretty deep. The object "myCal" of type "Preaching.Web.UI.PreachingCalendar" is my custom control written entirely in C# and packaged up in a DLL.
Expand|Select|Wrap|Line Numbers
  1. __Page    ASP.pcal_test_aspx
  2.     ctl05    System.Web.UI.LiteralControl
  3.     ctl00    System.Web.UI.HtmlControls.HtmlHead
  4.         ctl02    System.Web.UI.HtmlControls.HtmlMeta
  5.         ctl03    System.Web.UI.HtmlControls.HtmlMeta
  6.         ctl04    System.Web.UI.HtmlControls.HtmlMeta
  7.         pageTitle    System.Web.UI.HtmlControls.HtmlTitle
  8.     ctl06    System.Web.UI.LiteralControl
  9.     ctl01    System.Web.UI.HtmlControls.HtmlForm
  10.         ctl07    System.Web.UI.LiteralControl
  11.         datasource    Preaching.Web.UI.PreachingEventSqlPager
  12.         ctl08    System.Web.UI.LiteralControl
  13.         myCal    Preaching.Web.UI.PreachingCalendar
  14.             myCal$ctl00    System.Web.UI.WebControls.Table
  15.                 myCal$ctl56    System.Web.UI.WebControls.TableRow
  16.                     myCal$ctl57    System.Web.UI.WebControls.TableCell
  17.                         myCal$prevYear    System.Web.UI.WebControls.LinkButton
  18.                         myCal$ctl58    System.Web.UI.LiteralControl
  19.                         myCal$prevMonth    System.Web.UI.WebControls.LinkButton
  20.                     myCal$ctl59    System.Web.UI.WebControls.TableCell
  21.                         myCal$monthSelector    System.Web.UI.WebControls.DropDownList
  22.                         myCal$yearSelector    System.Web.UI.WebControls.DropDownList
  23.                     myCal$ctl60    System.Web.UI.WebControls.TableCell
  24.                         myCal$nextMonth    System.Web.UI.WebControls.LinkButton
  25.                         myCal$ctl61    System.Web.UI.LiteralControl
  26.                         myCal$nextYear    System.Web.UI.WebControls.LinkButton
  27.                         .....
  28.  
Here is the relevant C# code in the DLL.

Expand|Select|Wrap|Line Numbers
  1.             // Set up DropDownList navigators for the title
  2.             DropDownList monthSelector = new DropDownList();
  3.             monthSelector.ID = "monthSelector";
  4.             for (int i=0; i<12; i++) {
  5.                 monthSelector.Items.Add(new ListItem(DateTimeFormatInfo.InvariantInfo.MonthNames[i],
  6.                                                      (i+1).ToString()));
  7.             }
  8.             monthSelector.AutoPostBack = true;
  9.             monthSelector.EnableViewState = false;
  10.             monthSelector.SelectedIndex = VisibleDate.Month-1;
  11.             monthSelector.DataBind();
  12.  
  13.             DropDownList yearSelector = new DropDownList();
  14.             yearSelector.ID = "yearSelector";
  15.             for (int i=VisibleDate.Year-10; i<=VisibleDate.Year+10; i++) {
  16.                 yearSelector.Items.Add(new ListItem(i.ToString(), i.ToString()));
  17.             }
  18.             yearSelector.AutoPostBack = true;
  19.             yearSelector.EnableViewState = false;
  20.             yearSelector.SelectedIndex = 10;
  21.             yearSelector.DataBind();
  22.  
  23.             // Set up postback listeners for each control.
  24.             prevYear.Click += new EventHandler(OnPrevYearClick);
  25.             prevMonth.Click += new EventHandler(OnPrevMonthClick);
  26.             nextMonth.Click += new EventHandler(OnNextMonthClick);
  27.             nextYear.Click += new EventHandler(OnNextYearClick);
  28.             monthSelector.SelectedIndexChanged += new EventHandler(OnMonthSelectorChanged);
  29.             yearSelector.SelectedIndexChanged += new EventHandler(OnYearSelectorChanged);

Can anyone rescue me from my dufus-ness?
Jul 25 '07 #1
5 2162
Plater
7,872 Expert 4TB
The DropDownLists successfully AutoPostBack. But I can't get my SelectedIndexChanged handler to execute to save my life.
How do you know that they're posting back if their even never fires?
How do you know the event never fires?
Are you binding the datasource in the page_load (or analogue) function?
Jul 25 '07 #2
Plater, thanks so much for taking the time to help me. The spirit of fraternal volunteerism on these boards never ceases to amaze me. I'll try to answer your questions.

How do you know that they're posting back if their even never fires?
Answer: I know that AutoPostBack is working because whenever I change one of the DropDownLists, I witness a page reload.

How do you know the event never fires?
I know that the handler never executes because the very first line of the handler writes to the TraceContext indicating that it is beginning to execute. This message never appears in the trace. Also, of course, the desired calendar navigation never takes place.

Are you binding the datasource in the page_load (or analogue) function?
I'm calling DataBind in OnInit. The DDLs have the same value every time--they do not bind to any external datasource. As you can see in the original code, they simply present the months of the year and a list of years surrounding the present year.

The code creating the DDLs lies within a helper method (I think I named it something like CreateNavigationInterface) that is called by CreateChildControls().
Jul 25 '07 #3
Normally I would suspect that the controls hadn't been added to the Control tree when the event came looking for it, but since the LinkButton objects are directly adjacent to these DropDownList controls--even surround them--the fact that the LinkButton controls catch all their events just fine makes me relatively certain that these DDLs are also present while we're raising postback events.
Jul 25 '07 #4
AHA!

I fixed it myself. The problem was one too many DataBind()s In the Page_Load method on the actual page itself, I allow for the querystring to set the VisibleDate on the initial load. I had put the loading of the variable in an if-block, but not the DataBind() call for the PreachingCalendar. Thus, DataBind() was executing every time at Page_Load, whether a PostBack or not. The EnsureChildControls at LoadPostData was painstakingly putting all the controls in place, only to have them obliterated at the beginning of Page_Load, just before RaiseChangedEvents. LinkButtons, I have learned, do it differently (get their events at a different time.

So, am I an even bigger Dufus for puzzling so long over this when the answer was right there in front of me all along? I'll let you decide. Right now, I really don't care, because all that matters is this functional code in front of me.
Jul 25 '07 #5
Plater
7,872 Expert 4TB
I'm claiming victory
Are you binding the datasource in the page_load (or analogue) function?
Jul 26 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Donal | last post by:
I have 3 related dropdowns. When the 1st is changed, the 2nd is updated, and when the 2nd is changed, the 3rd is updated. When i change the 1st dropdown (sites), the SelectedIndexChanged fires...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
2
by: Shiju Poyilil | last post by:
Hi ! I have a requirement wherein i am binding a datalist which contains a label (Caption for the field) and some literal hidden fields and a dropdown list. When I am binding to the datalist.....
0
by: Tand35006 | last post by:
Hi, I hope some one can help with this. I have a basic webform with 2 DropDownLists and a single DataGrid. What I am trying to do is populate the first DDList from a dataset on Form_Load. I then...
1
by: sergeyr3 | last post by:
Hey guys, I am new here, so i hope this works out: I have a datagrid which I populate with data from XML file. In EditItemTemplate I have a dropdownlist. How do I fire myDataGrid_UpdateCommand...
4
by: David | last post by:
Hi all, I am doing this in a web page... I have a dropdownlist that autopostback. This sets me a filter criteria for items to display in a datalist. In the datalist, I have edit...
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
5
by: Victorious1 | last post by:
Why doesn't the the SelectedIndexchanged event for my dropdownlist ever get executed? Why do I get a page not found error when I select a new item? My dropdownlist is within a form. The items...
2
by: jnoody | last post by:
The problem I am having is with the SelectedIndexChanged event not always firing or the SelectedIndex property not being correct when the event does fire. The code is below, but here are some...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.