Solved

DropDownList SelectedIndexChanged event fires twice on few Dropdowns

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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