简体   繁体   中英

DropDownList selected index change firing twice

This one is interesting. I have a dropdown that once selected goes and runs a data call, returns the data and populates some labels on the page. Here is the problem:

If I type to the selection and hit ENTER, the method fires once, just fine. If I use the mouse to scroll to the selection and choose it that way, it fires twice.

<asp:DropDownList ID="ddlPattern"  CssClass="dropdownint" runat="server" DataTextField="Pattern" DataValueField="Pattern"
                            AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="ddPattern_SelectedIndexChanged" EnableViewState="true" />

Code behind:

        SqlDataAdapter sda = new SqlDataAdapter("StoredProcedure", sc);
        sda.SelectCommand.CommandType = CommandType.StoredProcedure;
        sda.SelectCommand.Parameters.AddWithValue("@Pattern", Pattern.ToString());
        sda.SelectCommand.Parameters.AddWithValue("@fac", this.ddfac.SelectedItem.Value);
        DataSet ds = new DataSet();
        sda.Fill(ds);

        this.lnkInventory.Text = ds.Tables[0].Rows[0][0].ToString() + " (" + ds.Tables[1].Rows[0][0].ToString() + ")";

There is more to the code behind but it is really just populating more labels. I have tried many things, including setting the AutoEventWireup="false", and making sure I had no src in any tags, and even unsubscribing the event every time, but none of that works. And it is really weird how it just fires once if I type the selection but twice if I mouse select.

I figured it out. Was due to a try catch problem that wasn't being handled properly and causing a duplication error on certain items that were chosen.

This is perfectally work for my project when unschedule visit show a div

 <asp:DropDownList ID="txtvisitname" AutoPostBack="true" class="txtno" AppendDataBoundItems="true" 
                runat="server" onchange="return selectChange()">
                <asp:ListItem Text="--SELECT--" Value="0" />
                <asp:ListItem Text="VISIT1" Value="VISIT1" />
                <asp:ListItem Text="VISIT2" Value="VISIT2" />
                <asp:ListItem Text="VISIT3" Value="VISIT3" />
                <asp:ListItem Text="VISIT4" Value="VISIT4" />
                <asp:ListItem Text="VISIT5" Value="VISIT5" />
                <asp:ListItem Text="Unscheduled  VISIT" Value="Unscheduled  VISIT" />
            </asp:DropDownList>
function selectChange() {        
      if ($("[id*=txtvisitname]").val() == "Unscheduled  VISIT") {
             $(".other").show();
         } else {
             $(".other").hide();
      }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM