Solved

DropDownList SelectedIndexChanged event fires twice on few Dropdowns

Posted on 2012-03-26
5
1,978 Views
Last Modified: 2012-03-27
Could anyone explain me why txtPreppin_SelectedIndexChanged event executes twice after txtPreppin value changed, then change value in second dropdownlist txtScanner


<asp:DropDownList
                    ID="txtPreppin" runat="server" CssClass="padding-input"
                    AutoPostBack="True" onselectedindexchanged="txtPreppin_SelectedIndexChanged">
                    <asp:ListItem Text="-Select-" Value=''>-Select-</asp:ListItem>
                    <asp:ListItem>Acevedo, Noe</asp:ListItem>
.
.
.
.
.
                                   </asp:DropDownList>
                <br />
                <label for="recordtype">
                                 Date:</label><asp:TextBox ID="txtprepDate" runat="server"
                    Enabled="False" ReadOnly="True"></asp:TextBox>
                <asp:CalendarExtender ID="txtprepDate_CalendarExtender" runat="server"
                    Enabled="True" TargetControlID="txtprepDate" Format="MM/dd/yyyy">
                </asp:CalendarExtender>
               
               
               
        </td>
        <td style="width: 306px">
           
        <label for="recordtype">
                                 Scanner:</label><asp:DropDownList
                ID="txtScanner" runat="server" CssClass="padding-input"
                AutoPostBack="True" onselectedindexchanged="txtScanner_SelectedIndexChanged">
                    <asp:ListItem Text="-Select-" Value=''>-Select-</asp:ListItem>
                     <asp:ListItem>Bueno, Nancy</asp:ListItem>
.
.
.
.
.
                               </asp:DropDownList>
       
                <label for="recordtype">
                                 Date:</label>
                                 <br />
                                 <asp:TextBox ID="txtScanDate" runat="server"
                Enabled="False" ReadOnly="True"></asp:TextBox>
                <asp:CalendarExtender ID="txtScanDate_CalendarExtender" runat="server"
                    Enabled="True" TargetControlID="txtScanDate" Format="MM/dd/yyyy">
                </asp:CalendarExtender>
                         





 protected void txtScanner_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (Page.IsPostBack)
        {

           
            if (txtScanner.SelectedItem.Text != null)
            {
                txtScanDate.Enabled = true;
                txtScanDate.ReadOnly = false;
                txtScanDate.Text = DateTime.Today.ToString("MM/dd/yyy");
            }

            else
            {
                txtScanDate.Enabled = false;
                txtScanDate.ReadOnly = true;
            }

        }
    }
    protected void txtPreppin_SelectedIndexChanged(object sender, EventArgs e)
    {


        if (Page.IsPostBack)
        {
           
            if (txtPreppin.SelectedItem.Text != null)
            {
                txtprepDate.Enabled = true;
                txtprepDate.ReadOnly = false;
                txtprepDate.Text = DateTime.Today.ToString("MM/dd/yyy");
            }

            else
            {
                txtprepDate.Enabled = false;
                txtprepDate.ReadOnly = true;
            }
        }
    }
0
Comment
Question by:TonyReba
  • 4
5 Comments
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 500 total points
Comment Utility
You disabled the viewstate for dropdowns by setting EnableViewState="false" to them. Take a look to a note on MSDN:

A list control must persist some values between posts to the server for this event to work correctly. Be sure that view state is enabled for the list control.

Also OnSelectedIndexChanged event handler is the same for the each of you dropdowns: OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged".

All this means that after a postback your dropdowns think that the selected value was changed if the selected value is different from the first list item's value. That's why you've got your event handler fire twice after second postback or even thrice after third.

Just enable viewstate for dropdowns to prevent such a behaviour.
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
View state is enabled.
Thasts not the problem.
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
View state is enabled.
Thasts not the problem.
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
View State was enabled on the page, but not on the masterpage.....
0
 
LVL 9

Author Closing Comment

by:TonyReba
Comment Utility
Viewstate wasnt enabled on Master Page
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

13 Experts available now in Live!

Get 1:1 Help Now