Solved

DropDownList SelectedIndexChanged event fires twice on few Dropdowns

Posted on 2012-03-26
5
2,193 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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

622 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