asp.net 中ListBox选中值的问题??高手求解
<asp:ListBoxID="ListBox1"runat="server"Height="368px"Width="189px"OnSelectedIndexChan...
<asp:ListBox ID="ListBox1" runat="server" Height="368px" Width="189px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
AutoPostBack="True"></asp:ListBox> 选中事件始终触发不了,为什么??感觉是点一次刷新一次,要是 AutoPostBack="false"的话,又会没反应了,怎么弄都不行,紧急求解,谢谢大家!!! 展开
AutoPostBack="True"></asp:ListBox> 选中事件始终触发不了,为什么??感觉是点一次刷新一次,要是 AutoPostBack="false"的话,又会没反应了,怎么弄都不行,紧急求解,谢谢大家!!! 展开
3个回答
2013-08-08
展开全部
我做过无刷新ajax的ListBox,是两个ListBox互动的。贴下代码吧。
前台:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ajaxDirectory.ascx.cs"
Inherits="DOC.Site.UserControls.doc.ajaxDirectory" %>
<script src="../../UI/JS/Cookie.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
//以XML求取ListBox2的数据
function XmlPost2(obj,childlistid)
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
var svalue = obj.value;
var webFileUrl = "?ParentID=" +escape(svalue);
SetCookie("UpLoad_Dir_Parent",escape(svalue));
var result = "";
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send("");
result = xmlHttp.responseText;
if(result != "")
{
var listobj = document.getElementById(childlistid);
listobj.length=0;
var piArray = result.split(",");//两次分割字符串来实现
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
listobj.options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
else
{
alert(result);
}
}
//取ChildID
function GetSelectId(listid)
{
var svalue = escape(listid.value);
SetCookie("UpLoad_Dir_Child",escape(svalue));
}
</script>
<asp:HiddenField ID="childid" runat="server" />
<asp:ListBox ID="ListBox1" runat="server" Height="300px" Width="135px"></asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" Height="300px" Width="135px"></asp:ListBox>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
using DOC.Common;
namespace DOC.Site.UserControls.doc
{
public partial class ajaxDirectory : System.Web.UI.UserControl
{
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
//绑定父种类
ParentBind();
//给父类 ListBox添加事件
this.ListBox1.Attributes.Add("onchange", "XmlPost2(this,'" + this.ListBox2.ClientID + "')");
this.ListBox2.Attributes.Add("onchange", "GetSelectId(this)");
}
if (ParentID != "")
{
ChildBind(ParentID);
}
}
#region 设置或获取选中的父类的值
public string ParentID
{
get
{
if (ViewState["ParentID"] != null && ViewState["ParentID"].ToString() != "")
{
return ViewState["ParentID"].ToString();
}
else
{
if (Request["ParentID"] != null && Request["ParentID"].ToString() != "")
{
return Request["ParentID"];
}
else
{
return "";
}
}
}
set
{
ViewState["ParentID"] = value;
}
}
#endregion
#region 设置或获取选中的 选中【子类】 值
public string ChildID
{
get
{
return this.childid.Value;
}
}
#endregion
#region 绑定父种类
private void ParentBind()
{
DOC.ServerControl.Common.ListBoxDatabind.BinParentDocType(this.ListBox1);
ListBox2.ClearSelection();
}
#endregion
#region 绑定子种类
private void ChildBind(string ParentID)
{
string mystr = string.Empty;
DataTable dt = BLL.BLLClasses.Directory.GetList(" ParentID =" + ParentID);
if (dt.Rows.Count != 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
mystr += "," + dt.Rows[i]["DirectoryID"].ToString() + "|" + dt.Rows[i]["DirectoryName"].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}
#endregion
public void ReBind()
{
if (!string.IsNullOrEmpty(NetCookies.GetCookies("UpLoad_Dir_Parent")))
{
DOC.ServerControl.Common.ListBoxDatabind.BinParentDocType(this.ListBox1);
ListBox1.SelectedValue = NetCookies.GetCookies("UpLoad_Dir_Parent");
if (!string.IsNullOrEmpty(NetCookies.GetCookies("UpLoad_Dir_Child")))
{
DOC.ServerControl.Common.ListBoxDatabind.BinChildDocType(this.ListBox2, DataConvert.ToInt32(ListBox1.SelectedValue));
ListBox2.SelectedValue = NetCookies.GetCookies("UpLoad_Dir_Child");
}
}
}
}
}
你自己看一下吧
前台:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ajaxDirectory.ascx.cs"
Inherits="DOC.Site.UserControls.doc.ajaxDirectory" %>
<script src="../../UI/JS/Cookie.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
//以XML求取ListBox2的数据
function XmlPost2(obj,childlistid)
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
var svalue = obj.value;
var webFileUrl = "?ParentID=" +escape(svalue);
SetCookie("UpLoad_Dir_Parent",escape(svalue));
var result = "";
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send("");
result = xmlHttp.responseText;
if(result != "")
{
var listobj = document.getElementById(childlistid);
listobj.length=0;
var piArray = result.split(",");//两次分割字符串来实现
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
listobj.options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
else
{
alert(result);
}
}
//取ChildID
function GetSelectId(listid)
{
var svalue = escape(listid.value);
SetCookie("UpLoad_Dir_Child",escape(svalue));
}
</script>
<asp:HiddenField ID="childid" runat="server" />
<asp:ListBox ID="ListBox1" runat="server" Height="300px" Width="135px"></asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" Height="300px" Width="135px"></asp:ListBox>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
using DOC.Common;
namespace DOC.Site.UserControls.doc
{
public partial class ajaxDirectory : System.Web.UI.UserControl
{
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
//绑定父种类
ParentBind();
//给父类 ListBox添加事件
this.ListBox1.Attributes.Add("onchange", "XmlPost2(this,'" + this.ListBox2.ClientID + "')");
this.ListBox2.Attributes.Add("onchange", "GetSelectId(this)");
}
if (ParentID != "")
{
ChildBind(ParentID);
}
}
#region 设置或获取选中的父类的值
public string ParentID
{
get
{
if (ViewState["ParentID"] != null && ViewState["ParentID"].ToString() != "")
{
return ViewState["ParentID"].ToString();
}
else
{
if (Request["ParentID"] != null && Request["ParentID"].ToString() != "")
{
return Request["ParentID"];
}
else
{
return "";
}
}
}
set
{
ViewState["ParentID"] = value;
}
}
#endregion
#region 设置或获取选中的 选中【子类】 值
public string ChildID
{
get
{
return this.childid.Value;
}
}
#endregion
#region 绑定父种类
private void ParentBind()
{
DOC.ServerControl.Common.ListBoxDatabind.BinParentDocType(this.ListBox1);
ListBox2.ClearSelection();
}
#endregion
#region 绑定子种类
private void ChildBind(string ParentID)
{
string mystr = string.Empty;
DataTable dt = BLL.BLLClasses.Directory.GetList(" ParentID =" + ParentID);
if (dt.Rows.Count != 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
mystr += "," + dt.Rows[i]["DirectoryID"].ToString() + "|" + dt.Rows[i]["DirectoryName"].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}
#endregion
public void ReBind()
{
if (!string.IsNullOrEmpty(NetCookies.GetCookies("UpLoad_Dir_Parent")))
{
DOC.ServerControl.Common.ListBoxDatabind.BinParentDocType(this.ListBox1);
ListBox1.SelectedValue = NetCookies.GetCookies("UpLoad_Dir_Parent");
if (!string.IsNullOrEmpty(NetCookies.GetCookies("UpLoad_Dir_Child")))
{
DOC.ServerControl.Common.ListBoxDatabind.BinChildDocType(this.ListBox2, DataConvert.ToInt32(ListBox1.SelectedValue));
ListBox2.SelectedValue = NetCookies.GetCookies("UpLoad_Dir_Child");
}
}
}
}
}
你自己看一下吧
2013-08-08
展开全部
不要弄什么破事件 这个控件取值有两个状态 没注意?一种就是你所谓的状态更改时取值 也就是你的事件激发后你靠这个取值,还有一种被人忽视的就是,事件改变控件的状态了,那就是直接取他的text
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-08
展开全部
这个方法回执行后台的事件当然会刷新页面,你要不刷新只能用Ajax
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询