ListBox SelectedIndexChanged 事件是否需要在 ASP.NET 中回发?
我正在尝试添加一个 JavaScript 函数,以将 ListBox 中的所有选定项目显示为页面上标签中的连接字符串。这是必需的,因为 AutoPostBack="true" 将导致 ListBox 一直滚动回第一个选定的项目。
所以这段代码有效:
<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>
不幸的是我仍然需要 SelectedIndexChanged 委托背后的代码。有没有办法在不进行回发的情况下使用这两者?当我设置 AutoPostBack="false" 时,我的委托似乎永远无法到达。
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. It's needed because AutoPostBack="true" will cause the ListBox to scroll all the way back to the first selected item.
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. Is there a way to use both of these without doing a PostBack? When I set AutoPostBack="false", my delegate never seems to be reached.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想调用服务器端委托,那么你必须执行回发。
服务器上需要运行的代码是什么?您应该能够在 JavaScript 中完成所有工作,然后在选择所有列表项后使用不同的触发器(不是 selectedIndexChange)来运行服务器端代码。
您是否还看到过,Ajax UpdatePanel 和维护ScrollPositionOnPostBack="true" 以便页面在回发后保留其滚动位置。然而,这只会影响页面滚动条,而不影响选择框。
If you want to call a server side deligate then you have to do a PostBack.
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.
Have you also seen, Ajax UpdatePanel and maintainScrollPositionOnPostBack="true" so that the page retains it's scroll position after postbacks. However this will only affect the pages scroll bar not the selectbox.
如果这不是您想要的行为,我认为 AutoPostBack 不适合您。当 ASP.Net 进行完整回发时,它与“传统”HTML 表单发布相同,将表单的全部内容发送回服务器并等待响应(这恰好是同一页面,因为如何Asp.Net 响应)。因此,为什么列表框中的位置丢失了——您将返回一个全新的列表框。
您是否将 ASP.Net Ajax (UpdatePanels) 视为一种可能的选择?这与回发的行为相同,它将数据发送回服务器并调用您的方法,但仅回发页面的一部分。
I don't think AutoPostBack is the way to go for you if that isn't the behaviour you want. 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). 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? 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.