简体   繁体   English

为什么DropDownList.SelectedIndexChanged事件不会触发?

[英]Why DropDownList.SelectedIndexChanged event does not fire?

I have a DropDown which is bounded to an ObjectDataSource. 我有一个DropDown,它绑定到ObjectDataSource。 On its data bound event, I am adding "--select--" value on the 0 index. 在它的数据绑定事件中,我在0索引上添加“--select--”值。 I have a LinkButton on the page and on its client click, i am selecting different item on drop down (using JavaScript). 我在页面上有一个LinkBut​​ton,并在其客户端点击,我在下拉菜单中选择不同的项目(使用JavaScript)。 Suppose there are 3 items like --select--, option1, option2 and option3 and now on link button's client click i selected option3, now if I select the default value "--select--", it does not fire the SelectedIndexChanged event. 假设有3个项目,如--select--,option1,option2和option3,现在在链接按钮的客户端上单击我选择的选项3,现在如果我选择默认值“--select--”,它不会触发SelectedIndexChanged事件。 If I select any other value then it fires. 如果我选择任何其他值,则它会触发。 Why does it not work for the default value? 为什么它不适用于默认值?

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack && !IsCallback)
    {
      this.FillDropDown("--Select--");
    }
    else
    {                            
        if (this.drp.SelectedItem != null)
            this.FillDropDown(this.drp.SelectedItem.Text);
        else
            this.FillDropDown("--Select--");
    }
}

protected void FillDropDown(string viewName)
{       
    this.obJectDataSource.Select();

    this.drp.Items.Clear();
    this.drp.SelectedIndex = -1;
    this.drp.DataBind();

    if (this.drp.Items.Count > 0)
    {           
        ListItem item = this.drp.Items.FindByText(viewName);
        if (item == null)
        {
            item = this.drp.Items.FindByText("--Select--");
        }
        if (item != null)
        {
            int selectedIndex = this.drp.Items.IndexOf(item);
            this.drp.Items[selectedIndex].Selected = true;
            this.drp.SelectedIndex = selectedIndex;                        
        }
    }
}

protected void drp_OnDataBound(object sender, EventArgs e)
{
    if (this.drp.Items.Count > 0)
    {               
        this.drp.Items.Insert(0, new ListItem("--Select--", "-1"));                
    }                        
}

protected void drp_SelectedIndexChanged(object sender, EventArgs e)
{            
    if (drp.SelectedValue != "-1")
    {
        Session["SelectedItem"] = this.drp.SelectedItem.Text;

    }            
}
/// The button which do callback not postback

<dx:ASPxCallback ID="ASPxCallback1" runat="server" ClientInstanceName="Callback1" OnCallback="SaveFilter_Click">
    <ClientSideEvents CallbackComplete="function(s,e){Callback1Success(s,e);}" />
</dx:ASPxCallback>

<dx:ASPxButton ID="btn_Save" runat="server" CausesValidation="False" Height="20px" Text="Save" AutoPostBack="false" UseSubmitBehavior="false">
    <ClientSideEvents Click="function(s, e) {
            var isValid =  Validate(this, txt1.GetText());
            if(isValid==true) {
                Callback1.PerformCallback('Save');                               
            }  
            else {
                e.processOnServer = false;
            }}">
    </ClientSideEvents>
</dx:ASPxButton>

protected void SaveFilter_Click(object sender, CallbackEventArgs e)
{
    if (e.Parameter.ToString() == "Save")
    {
        if (!string.IsNullOrEmpty(txt_SaveSaveSearch.Text))
        {
            // saving data into data base.
            this.FillDropDown(txt.Text);                    
            e.Result = ASPxCallback.GetRenderResult(this.drp);
        }
    }
}

function Callback1Success(s,e) {
     var ctrl = document.getElementById('ctl00_ContentHolder_drp');
     ctrl.outerHTML = e.result;        
}

Update: 更新:

Based on the revised question - 根据修订后的问题 -

  1. Why don't you set AppendDataBoundItems on the dropdownlist? 为什么不在下拉列表中设置AppendDataBoundItems? The property would allow the dropdownlist to append items to the existing ones. 该属性允许下拉列表将项目附加到现有项目。

     <asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true' EnableViewState='true' AppendDataBoundItems='true'> <asp:ListItem Selected='True' Text='--Select--' Value='1'></asp:ListItem></asp:DropDownList> 
  2. The Page_Load method doesn't do what you want to. Page_Load方法无法执行您想要的操作。 The else part of it will be executed even if one of them is true ..ex: if "Postback is true" or "callback is true" it would go into the else part. 即使其中一个是真的,它的else部分也将被执行..ex:如果“Postback为true”或“callback为true”,它将进入else部分。 But as suggested in the (1) step, set the AppendDataBoundItems and remove code to add "--select--". 但是如(1)步骤所示,设置AppendDataBoundItems并删除代码以添加“--select--”。


Most likely issue be with ViewState, Set EnableViewState="true" 最有可能的问题是ViewState,Set EnableViewState =“true”

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Test.aspx.cs" EnableViewState="true"%>

And if you are using Maste Pages you'll have to enable on it too. 如果你正在使用Maste Pages,你也必须启用它。

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" EnableViewState="true" ClassName="Site" %>

And in the dropdown web control AutoPostback="true" 并在下拉网页控件中AutoPostback =“true”

<asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true' 
    OnSelectedIndexChanged='HandleOnDropDownListSelectedIndexChanged'>
</asp:DropDownList>

我不知道是否有其他人遇到过与我相同的问题,但事实上我的值对于下拉列表中的每个项目都是相同的,并且在我更改值之前它永远不会触发事件。

Another cause of this is if you have more than one form on the page... I had placed a second form on the page that didn't have an ID or action yet. 另一个原因是如果页面上有多个表单...我在页面上放置了第二个表单,但没有ID或操作。 This form was interfering with the form that contained the control for which I was trying to fire the onselectedindexchanged handler... 这个表单干扰了包含我试图触发onselectedindexchanged处理程序的控件的表单...

I guess if all else fails, make sure you only have a SINGLE form in your markup. 我想如果所有其他方法都失败了,请确保您的标记中只有一个单一的表单。

I experienced the same issue, after a while digging in I found that the designer code is not in sync with the changes that I made in .aspx, the code behind which is still has some references to control that was deleted is causing object reference not set to instance of object error, but that's happening at some special case handling which is nothing to do with the actual issue (onselectionchanged not firing).. 我经历了同样的问题,经过一段时间的挖掘,我发现设计器代码与我在.aspx中所做的更改不同步,后面的代码仍然有一些对被删除的控件的引用导致对象引用没有设置为对象错误的实例,但这是在一些特殊情况处理中发生的,这与实际问题无关(onselectionchanged not firing)。

... but I also noticed that there is some jscript in .aspx which is still contain the old references of control I deleted. ...但我也注意到.aspx中有一些jscript仍然包含我删除的旧控件引用。 The compiler didnt prompt any error as this is javascript which are caught only at runtime. 编译器没有提示任何错误,因为这是仅在运行时捕获的javascript。 Hence I concluded in my case that the javascript issue is preventing autopostback event. 因此,我在我的案例中得出结论,javascript问题阻止了autopostback事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在后面的C#代码中选择索引时,不会触发DropDownList.SelectedIndexChanged - DropDownList.SelectedIndexChanged does not fire when selecting index in C# code behind asp.net dropdownlist的第一个选择不会触发SelectedIndexChanged事件 - asp.net dropdownlist first selection does not fire SelectedIndexChanged event 如果已在下拉列表中选择了 Item,则不会触发 SelectedIndexChanged 事件? - SelectedIndexChanged event does not fire if Item is already selected in a dropdownlist? 为什么在修改所选项目时,ListBox会触发SelectedIndexChanged事件? - Why does the SelectedIndexChanged event fire in a ListBox when the selected item is modified? DropDownList:SelectedIndexChanged不触发 - DropDownList: SelectedIndexChanged do not fire 在C#中使用Databind,DropDownList不会在SelectedIndexChanged上触发事件 - DropDownList doesn't Fire Event on SelectedIndexChanged Using Databind in C# 下拉列表 selectedindexchanged 事件未触发 - Dropdownlist selectedindexchanged event is not firing DropDownList的SelectedIndexChanged事件未触发 - SelectedIndexChanged event of DropDownList is not fired 为什么ComboBox.SelectedIndexChanged事件在加载表单之前触发三次? - Why does ComboBox.SelectedIndexChanged event fire three times before form load? DropDownList不会在SelectedIndexChanged上回发 - DropDownList does not postback on SelectedIndexChanged
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM