Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DropDownList SelectedIndexChanged event fires twice on few Dropdowns

Posted on 2012-03-26
5
Medium Priority
?
2,247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 2000 total points
ID: 37767015
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
ID: 37767089
View state is enabled.
Thasts not the problem.
0
 
LVL 9

Author Comment

by:TonyReba
ID: 37767091
View state is enabled.
Thasts not the problem.
0
 
LVL 9

Author Comment

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

Author Closing Comment

by:TonyReba
ID: 37771756
Viewstate wasnt enabled on Master Page
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 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