级联下拉列表仅两次触发OnSelectedIndexChanged事件.... [英] Cascading Drop Down List fires OnSelected​IndexChang​ed event only once in two times....

查看:70
本文介绍了级联下拉列表仅两次触发OnSelectedIndexChanged事件....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在一个由页面调用的用户控件中有一个ASP.NET AJAX Codeplex工具包级联下拉列表控件.问题在于,在将数据填充到下拉列表中之后,用户从一个下拉列表中选择了一个项目,但是它每两次仅触发一次其选择的索引更改事件处理程序.这意味着基于此下拉列表的选定索引来填充其他控件所需的代码仅每两次填充一次!

定义方式如下:

用户控制:
-包含由单独的第五个主下拉列表控件启动的四个下拉列表控件.
-四个级联下拉列表控件分别附加到一个单独的ASP.NET Codeplex AJAX级联下拉列表扩展器(在附加到级联下拉列表扩展器的所有下拉列表上,AutoPostBack = false).
-使用与级联下拉列表扩展器相关联的网络服务填充下拉列表.
-下拉列表和它们各自的扩展符包装在div和span中,但是,尽管包装在div和span中,但有问题的下拉列表(我在本文中指的是一个)也包装在面板中:

Hi guys,

I have an ASP.NET AJAX Codeplex toolkit Cascading Dropdown list control in a user control called by a page. The problem is that after population of data into the drop down lists, the user selects an item from one of the drop down lists, but it fires it’s selected index changed event handler only once every two times. That means the code needed to be run in order to populate other controls based on the selected index of this drop down list only gets populated once every two times!

Here is how it’s defined:

User Control:
- Contains four drop down list controls initiated by a separate fifth primary drop down list control.
- The four cascading drop down list controls are each attached to an individual ASP.NET Codeplex AJAX Cascading Drop Down List Extender (AutoPostBack = false on all drop down lists attached to cascading drop down list extenders).
- The drop down lists are populated using a web service that’s tied to the Cascading Drop Down List Extender.
- The drop down lists and their respective extenders are wrapped in divs and spans, however, the problematic drop down list (the one I’m referring to in this post) although wrapped in divs and spans are also wrapped in a panel:

<asp:Panel id="rowProgram" Visible="false" runat="server" style="display: none">
                        <div class="ElementContainer" captureelement="true" runat="server">
                              <span class="LabelContainer">
                                    <asp:label id="lblProgram" runat="server" CssClass="Label" />
                              </span>
                              <span class="ControlContainer">
                                    <asp:dropdownlist id="lstProgram" runat="server" CssClass="DropDownList"/>
                                    <cc1:CascadingDropDown ID="lstProgram_CascadingDropDown"

                                      runat="server"

                                      ServicePath="~/WebServices/AjaxService.asmx"

                                      ServiceMethod="AJAXLoadPrograms"

                                      TargetControlID="lstProgram"

                                          ParentControlID="lstTransSubType3"

                                      Category="Program"

                                      UseContextKey="true" BehaviorID="lstProgram_Behaviour">
                    </cc1:CascadingDropDown>
                              </span>
                              <span class="InfoPopContainer">
                                    <exos:infopop id="popProgram" Width="300" Height="40" runat="server" />
                              </span>
                              <input id="inProgramDescription" type="hidden" value="" runat="server" />
                              <input id="inProgramValue" type="hidden" value="" runat="server" />
                        </div>
</asp:Panel>




ASPX页面:
-将用户控件嵌入其标记中,并为相关用户控件的下拉列表设置选定的索引更改事件处理程序.

当用户控件的下拉列表(从现在开始,我将其称为"lstProgram")被调用时,这里发生的事情是OnSelectedIndexChanged事件处理程序,它始终在第一次执行,而从不执行第二次....它将执行第三次而不是第四次,依此类推.

我的解决方法不起作用!

在阅读了许多论坛并实现了此功能之后,我尝试了一种解决方法,但是这种方法无效:

用户控制:
-创建了要通过页面调用的公共事件




ASPX Page:
- Embeds the user control into its markup and sets the selected index changed event handler for the user control’s drop down list in question.

What happens here is when the user control’s drop down list’s (I’ll call this ‘lstProgram’ from now on) OnSelectedIndexChanged event handler has been invoked, it always executes the first time, and never the second....it will execute the third time and not the fourth, and so on.

My workaround which doesn’t work!

I attempted a work around after reading many forums and implemented this, but it doesn’t work:

User Control:
- Created public event to be called by page

public event EventHandler OnProgramChange;



在lstProgram下拉列表控件上更改了"AutoPostBack = True".

-在标记中添加了以下JavaScript函数:



Changed "AutoPostBack = True" on the lstProgram drop down list control.

- Added this JavaScript function in the markup:

function callServer()
{
   setTimeout(function(){__doPostBack('ctl00$PageBody$EXOSTransTypeSubType$lstProgram','')}, 0);
}



-在Page_Load中添加了此逻辑:



- Added this logic within the Page_Load:

if (Page.IsPostBack && _doPostBackSection) 
            { 
                if (_programPostBack) ß This boolean flag is set to true when I want this dorp down list’s  
event handler to execute. 
                { 
                    if (lstProgram.Items.Count > 0) 
                    { 
                        lstProgram.Attributes.Add("onchange", "callServer()"); 
                    } 
                    else 
                    { 
                        Page.ClientScript.RegisterStartupScript(typeof(string), "progPB", "<script language=\"javascript\" type=\"text/javascript\">setTimeout(" + ''"'' + "callServer()" + ''"'' + ", 500);</script>"); 
                    } 
                }



-在InitializeComponent()函数中添加了以下代码行,并将代码添加到了lstProgram的SelectedIndexChanged事件处理程序中:



- Added this line of code in the InitializeComponent() function and added code to lstProgram’s SelectedIndexChanged event handler:

this.lstProgram.SelectedIndexChanged += new EventHandler(lstProgram_SelectedIndexChanged);

protected void lstProgram_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (OnProgramChange != null)
            {
                OnProgramChange(sender, e);
            }
  }



ASPX页面:
-将这行代码添加到InitalizeComponent,并将代码添加到OnProgramChange事件处理程序,以在从用户控件引发事件时执行:



ASPX Page:
- Added this line of code to InitalizeComponent and added code to the OnProgramChange event handler to execute when an event has been raised from the user control:

this.EXOSTransTypeSubType.OnProgramChange += new EventHandler(EXOSTransTypeSubType_OnProgramChange); 
  
void EXOSTransTypeSubType_OnProgramChange(object sender, EventArgs e) 
      { 
            // I want these two lines of code to execute in order to populate  
               other controls based on the drop down list selection. 
            _programID = EXOSTransTypeSubType.GetProgramID(); 
            loadProgramRelatedDropDowns();  
      }   



因此,总结起来,级联的下拉列表可以正常工作;它们会相应地填充数据,但是问题出在下拉列表中的数据填充之后,并且用户从lstProgram中选择了一个项目(填充后). "OnProgramChange"事件每两次仅触发一次.

请帮忙!

谢谢,

Ben.



So, to wrap things up, the cascading drop down lists work fine; they populate the data accordingly, however the problem lies after population of data within the drop down lists and the user selects an item from lstProgram (after it has been populated). The "OnProgramChange" event gets fired only once every two times.

Please help!

Thanks,

Ben.

推荐答案

PageBody


EXOSTransTypeSubType
EXOSTransTypeSubType


lstProgram', ' ')}, 0 ); }
lstProgram','')}, 0); }



-在Page_Load中添加了此逻辑:



- Added this logic within the Page_Load:

if (Page.IsPostBack && _doPostBackSection) 
            { 
                if (_programPostBack) ß This boolean flag is set to true when I want this dorp down list’s  
event handler to execute. 
                { 
                    if (lstProgram.Items.Count > 0) 
                    { 
                        lstProgram.Attributes.Add("onchange", "callServer()"); 
                    } 
                    else 
                    { 
                        Page.ClientScript.RegisterStartupScript(typeof(string), "progPB", "<script language=\"javascript\" type=\"text/javascript\">setTimeout(" + ''"'' + "callServer()" + ''"'' + ", 500);</script>"); 
                    } 
                }



-在InitializeComponent()函数中添加了以下代码行,并将代码添加到了lstProgram的SelectedIndexChanged事件处理程序中:



- Added this line of code in the InitializeComponent() function and added code to lstProgram’s SelectedIndexChanged event handler:

this.lstProgram.SelectedIndexChanged += new EventHandler(lstProgram_SelectedIndexChanged);

protected void lstProgram_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (OnProgramChange != null)
            {
                OnProgramChange(sender, e);
            }
  }



ASPX页面:
-将这行代码添加到InitalizeComponent,并将代码添加到OnProgramChange事件处理程序,以在从用户控件引发事件时执行:



ASPX Page:
- Added this line of code to InitalizeComponent and added code to the OnProgramChange event handler to execute when an event has been raised from the user control:

this.EXOSTransTypeSubType.OnProgramChange += new EventHandler(EXOSTransTypeSubType_OnProgramChange); 
  
void EXOSTransTypeSubType_OnProgramChange(object sender, EventArgs e) 
      { 
            // I want these two lines of code to execute in order to populate  
               other controls based on the drop down list selection. 
            _programID = EXOSTransTypeSubType.GetProgramID(); 
            loadProgramRelatedDropDowns();  
      }   



因此,总结起来,级联的下拉列表可以正常工作;它们会相应地填充数据,但是问题出在下拉列表中的数据填充之后,并且用户从lstProgram中选择了一个项目(填充后). "OnProgramChange"事件每两次仅触发一次.

请帮忙!

谢谢,

本.



So, to wrap things up, the cascading drop down lists work fine; they populate the data accordingly, however the problem lies after population of data within the drop down lists and the user selects an item from lstProgram (after it has been populated). The "OnProgramChange" event gets fired only once every two times.

Please help!

Thanks,

Ben.


这篇关于级联下拉列表仅两次触发OnSelectedIndexChanged事件....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆