简体   繁体   English

使用Javascript检测用户何时在ASP.NET列表框中选择了一个项目

[英]Using Javascript to detect when a user has selected an item in an ASP.NET listbox

I am developing an ASP.NET web application that incorporates google maps. 我正在开发一个包含谷歌地图的ASP.NET Web应用程序。 I have an ASP.NET listbox on my page which contains a list of items. 我的页面上有一个ASP.NET列表框,其中包含一个项目列表。 When the user selects one of the items, I'd like to show this item on the map. 当用户选择其中一个项目时,我想在地图上显示此项目。 The main complication lies in the fact that google maps uses javascript to control it and the listbox is a server control. 主要的复杂性在于谷歌地图使用javascript来控制它,而列表框是服务器控件。

I can think of two ways to do this. 我可以想到两种方法来做到这一点。 The first would involve the listbox calling a javascript function when the user selects a new item. 第一个涉及列表框在用户选择新项目时调用javascript函数。 I can then write this function to perform the necessary map operations. 然后我可以编写此函数来执行必要的映射操作。 Unfortunately, the OnSelectedIndexChanged property of the listbox doesn't seem to support javascript functions. 不幸的是,列表框的OnSelectedIndexChanged属性似乎不支持javascript函数。

The second involves wrapping an UpdatePanel around the listbox and getting the listbox to perform a postback. 第二个涉及在列表框周围包装UpdatePanel并使列表框执行回发。 In the SelectedIndexChanged event in VB/C#, I would the need to somehow make a call to a javascript function which would then update the map. 在VB / C#中的SelectedIndexChanged事件中,我需要以某种方式调用javascript函数,然后更新映射。

Which solution can I use? 我可以使用哪种解决方案?

Thanks 谢谢

--Amr --Amr

In your codebehind (in your pageload) just add a javascript handler to the OnChange attribute that points to your javascript function. 在您的代码隐藏(在您的页面加载中)中,只需向指向您的javascript函数的OnChange属性添加一个javascript处理程序。 Eg: 例如:

lbYourListBox.Attributes.Add("onChange", "UpdateYourMap();");

You could also add this to your control using inline javascript as well but I prefer to do it in the codebehind so that the framework can handle matching up the names. 您也可以使用内联javascript将其添加到控件中,但我更喜欢在代码隐藏中执行此操作,以便框架可以处理匹配的名称。

You could simply embed javascript into your page, avoidig relying on ASP with it. 您可以简单地将javascript嵌入到您的页面中,避免依赖ASP。 Put the code into your document body: 将代码放入您的文档正文:

<script language="javascript">
body.onload=function(){
    var lb=document.getElementById("yourlistboxid");
    lb.onChange = function(){
       // put your handling code here
    }
}
</script>

to demo the other approach, here's a rough guide: 演示另一种方法,这是一个粗略的指南:

void listbox_SelectedIndexChanged( ... ) {

    string js = string.Format("callToMapsFunction({0});", listbox.SelectedValue);
    string scriptKey = "mapscript";

    ScriptManager.RegisterStartupScript( listbox, typeof(ListBox), scriptKey
        , js, true);
}

RegisterStartupScript runs whatever javascript you give it after the partial postback completes. RegisterStartupScript在部分回发完成后运行您提供的任何javascript。 you don't necessarily have to pass the listbox value- just whatever data you want to provide to the maps api. 您不一定要传递列表框值 - 只要您想要提供给地图api的任何数据。 the first 3 items are for preventing the script from registering a bunch of times (as far as I know). 前三项是为了防止脚本多次注册(据我所知)。 the true at the end tells the scriptmanager to automagically add opening and closing tags around your js code. 最后的真实告诉脚本管理员在你的js代码周围自动添加开始和结束标签。

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

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