简体   繁体   English

ListBox SelectedIndexChanged事件是否需要在ASP.NET中回发?

[英]Does the ListBox SelectedIndexChanged event require postback in ASP.NET?

I'm trying to add a JavaScript function to show all selected items from a ListBox as concatentated strings in a Label on the page. 我正在尝试添加JavaScript函数,以将ListBox中的所有选定项目显示为页面上Label中的合并字符串。 It's needed because AutoPostBack="true" will cause the ListBox to scroll all the way back to the first selected item. 这是必需的,因为AutoPostBack =“ true”将导致ListBox一直滚动回到第一个选定的项目。

So this code works: 因此,此代码有效:

 <script type="text/javascript">
    function Updatelist() { 
        var sel = document.getElementById('<%=lstbxStuff.ClientID%>'); 
        var lbl = document.getElementById('ctl00_cph_lblSelectedStuff');
        var listLength = sel.options.length; 
        var textForListbox = "";
        var list2length = 0;
        for (var i = 0; i < listLength; i++) { 
            if (sel.options[i].selected) { 
               if(list2length == 0) {
                    textForListbox = sel.options[i].text; 
                } else {
                    textForListbox = textForListbox + ", " + sel.options[i].text; 
                }
                list2length++; 
            } 
        } 
        lbl.innerText=textForListbox;

        return textForListbox;
    } 
</script>

Unfortunately I still need the code behind SelectedIndexChanged delegate. 不幸的是,我仍然需要SelectedIndexChanged委托背后的代码。 Is there a way to use both of these without doing a PostBack? 有没有一种方法可以同时使用这两种方法而无需执行PostBack? When I set AutoPostBack="false", my delegate never seems to be reached. 当我设置AutoPostBack =“ false”时,似乎永远无法到达我的委托。

I don't think AutoPostBack is the way to go for you if that isn't the behaviour you want. 我认为,如果这不是您想要的行为,那么AutoPostBack并不是您的理想之选。 When ASP.Net does a full post back, it is the same as the "traditional" HTML form post, sending the whole content of the form back to ther server and waiting for a response (which happens to be the same page because of how Asp.Net responds). 当ASP.Net执行完整的回发时,它与“传统的” HTML表单过帐相同,将表单的全部内容发送回服务器,并等待响应(由于方式不同,它恰好在同一页面上) Asp.Net响应)。 Hence why position in the listbox is lost - it's a brand new listbox you're getting back. 因此,为什么列表框中的位置会丢失-这是您要返回的全新列表框。

Have you looked at ASP.Net Ajax (UpdatePanels) as one possible option? 您是否将ASP.Net Ajax(UpdatePanels)视为一种可能的选择? This will behave the same as a postback in that it'll send data back to the server and call your methods, but only posts back part of the page. 这与回发的行为相同,它将回发数据到服务器并调用您的方法,但仅回发页面的一部分。

If you want to call a server side deligate then you have to do a PostBack. 如果要调用服务器端的lign,则必须执行回发。

What is the code on the server that needs to be ran? 服务器上需要运行的代码是什么? You should be able to do all the work in JavaScript, then have a different trigger (not selectedIndexChange) to run the server side code once all the list items are selected. 您应该能够使用JavaScript完成所有工作,然后在选择了所有列表项之后,使用另一个触发器(不是selectedIndexChange)来运行服务器端代码。

Have you also seen, Ajax UpdatePanel and maintainScrollPositionOnPostBack="true" so that the page retains it's scroll position after postbacks. 您是否也看到过,Ajax UpdatePanel和maintainScrollPositionOnPostBack =“ true”,以便页面在回发后保留其滚动位置。 However this will only affect the pages scroll bar not the selectbox. 但是,这只会影响页面滚动条,而不会影响选择框。

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

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