3

我有一个绑定到 ObjectDataSource 的 DropDown。在其数据绑定事件中,我在 0 索引上添加“--select--”值。我在页面上有一个 LinkBut​​ton,在其客户端单击时,我在下拉菜单中选择不同的项目(使用 JavaScript)。假设有 3 个项目,如 --select--、option1、option2 和 option3,现在在链接按钮的客户端单击我选择了 option3,现在如果我选择默认值“--select--”,它不会触发 SelectedIndexChanged 事件. 如果我选择任何其他值,则会触发。为什么它不适用于默认值?

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;        
}
4

4 回答 4

7

更新:

基于修改后的问题 -

  1. 为什么不在下拉列表中设置 AppendDataBoundItems?该属性将允许下拉列表将项目附加到现有项目。

    <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. Page_Load 方法不会做你想做的事。即使其中一个为真,它的 else 部分也会被执行 ..ex:如果“回发为真”或“回调为真”,它将进入 else 部分。但正如(1)步骤中所建议的,设置 AppendDataBoundItems 并删除代码以添加“--select--”。


最可能的问题是 ViewState,设置 EnableViewState="true"

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

如果您使用的是 Master Pages,您也​​必须启用它。

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

并在下拉 Web 控件中 AutoPostback="true"

<asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true' 
    OnSelectedIndexChanged='HandleOnDropDownListSelectedIndexChanged'>
</asp:DropDownList>
于 2011-02-10T07:32:53.200 回答
3

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

于 2011-05-24T02:41:06.143 回答
1

造成这种情况的另一个原因是,如果页面上有多个表单...我在页面上放置了第二个表单,但该表单还没有 ID 或操作。此表单干扰了包含我试图触发 onselectedindexchanged 处理程序的控件的表单...

我想如果一切都失败了,请确保您的标记中只有一个单一的表单。

于 2013-10-29T19:33:54.777 回答
0

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)..

... but I also noticed that there is some jscript in .aspx which is still contain the old references of control I deleted. The compiler didnt prompt any error as this is javascript which are caught only at runtime. Hence I concluded in my case that the javascript issue is preventing autopostback event.

于 2013-02-21T18:54:54.047 回答