日期:2014-05-18  浏览次数:20397 次

asp.net c# 里用js实现listbox双击,应该将 tableListBox.Attributes.Add("onclick", "doubleClick()");添加在哪个事件里?
我在selectedIndexchanged()里写没反应,根本就不执行这个函数
应该放在什么事件里?

------解决方案--------------------
放在Page_Load事件里面就哦了
tableListBox是哪个控件?DropDownList的话他没有onclick事件
------解决方案--------------------
Page_Load事件
------解决方案--------------------
你可以这样试试,仅就解决你所说的问题。
一个小的测试,在CS文件中:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] list = new string[3];
list[0] = "list1";
list[1] = "list2";
list[2] = "list3";
listBox.DataSource = list;
listBox.DataBind();
listBox.Attributes.Add("ondblclick", "javascript:listBox_DoubleClick();");// 添加一个双击事件
}

protected void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
// 你的代码
}
}
页面代码:
<script language="javascript" type="text/javascript">
function listBox_DoubleClick() {
alert(document.getElementById("listBox").value);// 你双击选中的值
document.forms[0].submit(); // 到后台执行,会调用OnSelectedIndexChanged事件的函数
}
</script>
<asp:ListBox ID="listBox" runat="server" OnSelectedIndexChanged="listBox_SelectedIndexChanged"></asp:ListBox>
------解决方案--------------------
1.
注意要加到 ListItem 中

2.
<asp:listbox ...
<asp:listitem ondbclick="alert('hello')" ...

3.
// Page_Load
ListBox1.Items[0].Attributes["ondbclick"]="alert('hello')";