Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...DNN Platform (o...DNN Platform (o...Dropdownlist not raising selectedindexchanged event on postbackDropdownlist not raising selectedindexchanged event on postback
Previous
 
Next
New Post
4/19/2009 11:51 AM
 

Folks,

I'm seeing a really weird behavior, and for awhile I was sure I just did something dumb (okay, I probably still did something dumb) but I can't see it.

I have a dropdown at the head of a ListView that acts as a filter.  When a user chooses a teacher name from the dropdown, the list needs to refresh and filter on that teacher's name.  The whole module is in an UpdatePanel, and the dropdown is in a Placeholder control (I need to determine how to render the header of the list at runtime).  I've tried removing both of those, and I still get the same result.

I need to capture that TeacherID from the dropdown on postback so I can instruct the ListView to return the right records.  I do that in the "Selecting" event of the LINQ datasource bound to the listview (in my case, this is LDSTalks_Selecting).  So what's the problem?  In short, I don't get the "SelectedIndexChanged" event from the dropdown list, so I can't capture that new TeacherID.

If I databind the ListView on the postback, I DO get the SelectedIndexChanged event, but only after the LDSTalks_Selecting event, so it does me no good.  This tells me I've at least wired the SelectedIndexChanged event properly.  But if I don't databind the ListView on postback, I DON'T get the SelectedIndexChanged event.  Any clues where to look?  Ever seen this before?

Somehow I doubt this is a DNN thing, or no one else's modules would work ;-)  Here's my source code edited down to only the relevant parts.

AldenG

1            protected void Page_Load(object sender, EventArgs e)
2            {
3    ...
4    
5                if (Page.IsPostBack)
6                {
7                    // if I comment the following line out, I get no selecteditemchanged event from the dropdown list
8                    LVTalks.DataBind();
9                }
10           }
11   
12           protected void LDSTalks_Selecting(object sender, System.Web.UI.WebControls.LinqDataSourceSelectEventArgs e)
13           {
14                // select the right rows based on various factors,
15 // one of which is the TeacherID that should have been set
16 // from the selecteditemindex changed event
17 } 18 19 private void RenderTeacherList(int TeacherID) 20 { 21 DharmaTalksDataContext dc = new DharmaTalksDataContext(); 22 DropDownList ddlTeachers = new DropDownList(); 23 Literal litBy = new Literal(); 24 25 ddlTeachers.Items.Add(new ListItem("All teachers", "0")); 26 27 /* get the teachers and sort them by last name */ 28 var teach = from t in dc.SIMS_Teachers 29 orderby t.LastName 30 select t; 31 32 foreach (SIMS_Teacher tch in teach) 33 { 34 ddlTeachers.Items.Add(new ListItem(tch.FirstName + " " + tch.LastName, tch.TeacherID.ToString())); 35 } 36 37 if (TeacherID > 0) 38 ddlTeachers.Items.FindByValue(TeacherID.ToString()).Selected = true; 39 40 // add the dropdownlist to the placeholder control on the title 41 litBy.Text = "by "; 42 ddlTeachers.AutoPostBack = true; 43 ddlTeachers.Enableview_state = true; 44 ddlTeachers.SelectedIndexChanged += new EventHandler(ddlTeachers_SelectedIndexChanged); 45 46 ph.Controls.Add(litBy); 47 ph.Controls.Add(ddlTeachers); 48 } 49 50 // if i databind the listview, i receive this callback AFTER the LDSTalks_Selecting event,
51 // which does me no good. if I don't databind the listview, i don't receive it at all
52 53 void ddlTeachers_SelectedIndexChanged(object sender, EventArgs e) 54 { 55 DropDownList ddl = (DropDownList)sender; 56 57 TeacherID = Convert.ToInt32(ddl.SelectedValue); 58 }
And the form looks like this:
1    <asp:UpdatePanel ID="upAjax" runat="server">
2     <ContentTemplate>
3    
4    <h2>Latest Talks <asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder> 
5    </h2>
6    <asp:ListView ID="LVTalks" runat="server" DataKeyNames="TalkID" 
7        DataSourceID="LDSTalks">
8    ... left out the listview...
9    </asp:ListView>
10   
11   <asp:LinkButton ID="LBAddTalk" runat="server" onclick="LBAddTalkClicked">Add a talk</asp:LinkButton>
12   
13    </ContentTemplate>
14   </asp:UpdatePanel>
 
New Post
4/19/2009 1:04 PM
Accepted Answer 

From your code, I do not see from where and at what point in the page life-cycle your are calling your RenderTeacherList method. As a general rule of thumb, any dynamically created controls such as ddlTeachers need to be created and added to the parent's control collection not only initially but on each and every postback and as early in the page life-cycle as possible, usually in Page_Init or CreateChildControls. You will, however, add content to the dynamically created control only during the initial Page_Load or if the content data has changed. This will mean that the creation of ddlTeachers and it's population with ListItem elements will need to be separated.


Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
New Post
4/19/2009 5:24 PM
 

Bill, thanks for this.  It would even explain the condition I see -- that I do get the callback AFTER the Selecting event, because that code path eventually calls RenderTeacherList.  I am going to restructure my code to get ddlTeachers created earlier in the page life-cycle and report results, but I bet you nailed the issue.  Many thanks.

AldenG

 
New Post
4/19/2009 7:31 PM
 

That was it.  Restructured code to move placing dynamic controls on the form to the Page_Load event and everything works as expected.  Lesson learned.

AldenG

 
Previous
 
Next
HomeHomeDevelopment and...Development and...DNN Platform (o...DNN Platform (o...Dropdownlist not raising selectedindexchanged event on postbackDropdownlist not raising selectedindexchanged event on postback


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out