Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I created a custom Templated User Control, for the purpose of having a standard "container" to use around our web app, but allow whatever controls you want inside of it -- pretty much like a Template Column of a GridView/DataGrid. Problem I'm having is that when I place a DropDownList control inside my custom control, the SelectedIndexChanged method of that DDL doesn't always fire, and I am stumped as to why.
 
I created a small test page with my custom control and a single DDL, which has some hard coded values, to replicate the problem:
 
<uc1:ExpandCollapseRegion ID="xpcColor"  runat="server" Title="Pick a Color" AlwaysOpen="true">
    <layouttemplate>
        
        <table>
            <tr>
                <td>Color: </td>
                <td>
                    <asp:DropDownList ID="ddlColor" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlColor_SelectedIndexChanged">
                        <asp:ListItem Text="" Value="" />
                        <asp:ListItem Text="Red" Value="1" />
                        <asp:ListItem Text="Orange" Value="2" />
                        <asp:ListItem Text="Yellow" Value="3" />
                        <asp:ListItem Text="Green" Value="4" />
                        <asp:ListItem Text="Blue" Value="5" />
                        <asp:ListItem Text="Indigo" Value="6" />
                        <asp:ListItem Text="Violet" Value="7" />
                    
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblColorChoice" runat="server" />
                </td>
            </tr>
        </table>
        
    </layouttemplate>
 
The code-behind looks like this:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ((Label)xpcColor.FindControl("lblColorChoice")).Text = "";
            }
        }
        protected void ddlColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DropDownList ddlColor = ((DropDownList)xpcColor.FindControl("ddlColor")))
            {
                if (!String.IsNullOrEmpty(ddlColor.SelectedValue))
                {
                    ((Label)xpcColor.FindControl("lblColorChoice")).Text = "You chose the color " + ddlColor.SelectedItem.Text;
                }
                else
                {
                    ((Label)xpcColor.FindControl("lblColorChoice")).Text = "";
                }
            }
        }
 
All that is supposed to happen here is to show in the Label what color was picked, but if no color was picked to then just clear the label. Very simple, no biggie, however....
 
100% of the time, when I pick a color the SelectedIndexChanged method fires, and the Label control is updated with the text. I can pick one color, the another, then another, and so forth over and over and the thing works great. However, if after choosing a color, I select the blank item of the DDL, the SelectedIndexChanged method *does not* fire, ever.
 
I wanted to see if this had something to do with the value being selected, so I added a new ListItem before the blank one (making the blank ListItem the second option):
 
<asp:ListItem Text="White" Value="0" />
 
Now when I run the page, I can choose a color, the label is updated, choose the blank one and the label is cleared, but if I select "White", the page does a PostBack yet the SelectedIndexChanged method once again *does not* fire.
 
I have never run into this before and admit I am a bit stumped as to the cause.
The problem may well be in my custom control, but I am hesitant to think so as the DDL functions correctly for all selections, except for the first one. Also the DDL choice as well as the Label text survives a PostBack, so I am not sure this is a ViewState issue either.
 

I'm still Googl'ing this, but I am pretty much stumped here what's going on. If anyone else has seen this, run across this, or may have some input of possible fixes, I am all ears. Much appreciated.
 
-- Andrew
Posted 25-Jul-11 12:50pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

@mahabubur rahman:
 
My apologies, your suggestion was spot on. It turned out that the using() statement was **exactly** the problem. I was able to solve the problem by changing the code to the following:
 
protected void ddlColor_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddlColor = (DropDownList)sender;
    if (!String.IsNullOrEmpty(ddlColor.SelectedValue))
    {
        ((Label)xpcColor.FindControl("lblColorChoice")).Text = "You chose the color " + ddlColor.SelectedItem.Text;
    }
    else
    {
        ((Label)xpcColor.FindControl("lblColorChoice")).Text = "";
    }
}
 

Thank you very much!
 
-- Andrew
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

protected void ddlColor_SelectedIndexChanged(object sender, EventArgs e)
        {
           if (!String.IsNullOrEmpty(ddlColor.SelectedValue))
           {
             ((Label)xpcColor.FindControl("lblColorChoice")).Text = "You chose the color " + ddlColor.SelectedItem.Text;
           }
           else
           {
             ((Label)xpcColor.FindControl("lblColorChoice")).Text = "";
           }
            
        }
 

Try this code and let me know the result.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

@mahabubur rahman:
 
Simply removing the the using (DropDownList ddlColor = ((DropDownList)xpcColor.FindControl("ddlColor"))) statement will have absolutely no effect on the problem I stated. Actually, doing this would in fact have the effect of introducing a failure: the SelectedIndexChanged() event is set in the HTML Source of the DropDownList control, yet because that DDL is inside the LayoutTemplate the FindControl() must be used method to access the DDL properties and values.
 
Please check out again what I was posted is the problem here. The SelectedIndexChanged() handler method does not fire when selecting the first ListItem of the DropDownList control after a previous choice. I can place a BreakPoint on the method and it hits every time I make a choice except when choosing the first ListItem.
 
That's the problem, and that is what's got me stumped.
 
-- Andrew
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 325
1 Sergey Alexandrovich Kryukov 289
2 CPallini 275
3 DamithSL 260
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 26 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100