DropDownList问题(急) 两个DropDownList,第一个从数据库中绑定全国的省市,第二个根据第一个选择的省市从数据库中动态的绑定该省市的所有地区,我想问的是,怎样才能使得第一个DropDownList选定的时候不要刷新页面? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最简便得方法就是用Ajax.Net中的UpdatePanel 1、用Ajax2、使用客户端的回调。(前提是熟悉Js) 用AJAX就行了。如果想方便快捷就装一个AJAX.NET上去吧,使用里面提供的UpdatePanelhttp://www.asp.net/ajax/ dropdownlist本身就是web控件 你想用他做出联动效果 必须触发他的autopostback属性 要不你说怎么能让他动态的把值传给第二个dropdownlist你要是想不刷新就用HTML的 select就得了呗,那就需要用AJAX了给你一个AJAX的动态从库绑定HTML的select我做的例子:quickroomJS.js:--------------------------------------------------------------//页面加载时绑定下拉列表function selectBindPG(){ var ds = QuickRoom.getData().value; //用户订单填写下拉列表 var select = document.getElementById("roomType"); var optionItem = document.createElement("option"); optionItem.text = "--------------请选择--------------"; optionItem.value = "-1"; select.add(optionItem); for(var i = 0; i < ds.Tables[0].Rows.length; i++) { var optionItem = document.createElement("option"); optionItem.text = ds.Tables[0].Rows[i]["i_room_name"]; optionItem.value = ds.Tables[0].Rows[i]["i_room_id"]; select.add(optionItem); }}HTML:--------------------------------------------------<head><script type="text/javascript" src="js/quickroomJS.js"></script></head><body onload="selectBindPG();"><form id="form1" runat="server">...<select id="roomType" style="position: relative"></select>...</form>C#:-->QuickRoom.aspx.cs---------------------------------------------------protected void Page_Load(object sender, EventArgs e) { AjaxPro.Utility.RegisterTypeForAjax(typeof(QuickRoom)); if (!IsPostBack) { } }#region//得到房间类型 [AjaxPro.AjaxMethod] public DataSet getData() { string connstr = "server=.;uid=sa;pwd=;database=JinBaiLiHotel"; con = new SqlConnection(connstr); da = new SqlDataAdapter("select i_room_id,i_room_name from hotel_rooms", con); ds = new DataSet(); da.Fill(ds); return ds; } #endregionweb.config:-->配置AJAXpro.DLL------------------------------------------------------<system.web> <httpHandlers> <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/> </httpHandlers>...现在以上代码已经就把第一个下拉列表的值从数据库中绑定好了下面你就做连动就可以啦用第一个select下拉列表中的 onchange事件,自己写一个JS方法声明第二个select接受值取select值的方法:Select1.options[Select1.selectedIndex].text;Select1.options[Select1.selectedIndex].value;这些是给你的提示,你自己考虑一下吧 要用ajax不然就要刷新页面,如果你觉得麻烦的话,还不如就让它刷新页面也没什么关系 下面的是CSDN上一个帖子中的1. 去http://sourceforge.net/projects/magicajax下载MagicAjax的Dll了。 2. 将该dll加入到webForm所在项目的引用中。 3. 在Web.Config文件中 <configuration> 节点下添加如下子节点: <configSections> <section name="magicAjax" type="MagicAjax.Configuration.MagicAjaxSectionHandler, MagicAjax" /> </configSections> 4. 在Web.Config文件中 < system.web> 节点下添加如下子节点: <httpModules><add name="MagicAjaxModule" type="MagicAjax.MagicAjaxModule, MagicAjax" /> </httpModules> 5. 在希望采用Ajax的webForm页面HTML开头引入Ajax的注册语句: <%@ Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax" %> 将你需要采用无刷新操作的控件包在MagicAjax的标签中,类似如下代码: <ajax:AjaxPanel ID="AjaxPanel1" runat="Server"> <asp:DropDownList id="DropDownList1" runat="server"> </asp:DropDownList></ajax:AjaxPanel> 这样就可以了 求教:复杂的表格显示怎么去实现... C#如何把listbox的内容输入未excel文件? DataGrid中如何隐藏掉一列,但是仍要能访问到这一列呢?可不可以做到? 整理了一个google的日历控件(js) 数据提交问题 ,提交不上去也没有任何的显示 C#“没有为命令对象设置命令”。求大神指点迷津!! 存储过程调用问题 奇怪的问题(在线等) 在网页中引用数据库的问题 bootstrap如何在子页面点击链接的时候在当前框架打开 关于路径的小问题 如何mock void这些方法?
2、使用客户端的回调。(前提是熟悉Js)
你要是想不刷新就用HTML的 select就得了呗,那就需要用AJAX了
给你一个AJAX的动态从库绑定HTML的select我做的例子:
quickroomJS.js:
--------------------------------------------------------------
//页面加载时绑定下拉列表
function selectBindPG()
{
var ds = QuickRoom.getData().value;
//用户订单填写下拉列表
var select = document.getElementById("roomType");
var optionItem = document.createElement("option");
optionItem.text = "--------------请选择--------------";
optionItem.value = "-1";
select.add(optionItem);
for(var i = 0; i < ds.Tables[0].Rows.length; i++)
{
var optionItem = document.createElement("option");
optionItem.text = ds.Tables[0].Rows[i]["i_room_name"];
optionItem.value = ds.Tables[0].Rows[i]["i_room_id"];
select.add(optionItem);
}
}HTML:
--------------------------------------------------
<head>
<script type="text/javascript" src="js/quickroomJS.js"></script>
</head>
<body onload="selectBindPG();">
<form id="form1" runat="server">
...
<select id="roomType" style="position: relative">
</select>
...
</form>C#:-->QuickRoom.aspx.cs
---------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(QuickRoom));
if (!IsPostBack)
{ }
}#region//得到房间类型
[AjaxPro.AjaxMethod]
public DataSet getData()
{
string connstr = "server=.;uid=sa;pwd=;database=JinBaiLiHotel";
con = new SqlConnection(connstr);
da = new SqlDataAdapter("select i_room_id,i_room_name from hotel_rooms", con);
ds = new DataSet();
da.Fill(ds);
return ds;
}
#endregionweb.config:-->配置AJAXpro.DLL
------------------------------------------------------
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
...
现在以上代码已经就把第一个下拉列表的值从数据库中绑定好了
下面你就做连动就可以啦
用第一个select下拉列表中的 onchange事件,自己写一个JS方法
声明第二个select接受值
取select值的方法:
Select1.options[Select1.selectedIndex].text;
Select1.options[Select1.selectedIndex].value;
这些是给你的提示,你自己考虑一下吧
1. 去http://sourceforge.net/projects/magicajax下载MagicAjax的Dll了。
2. 将该dll加入到webForm所在项目的引用中。
3. 在Web.Config文件中 <configuration> 节点下添加如下子节点:
<configSections>
<section name="magicAjax" type="MagicAjax.Configuration.MagicAjaxSectionHandler, MagicAjax" />
</configSections>
4. 在Web.Config文件中 < system.web> 节点下添加如下子节点:
<httpModules>
<add name="MagicAjaxModule" type="MagicAjax.MagicAjaxModule, MagicAjax" />
</httpModules>
5. 在希望采用Ajax的webForm页面HTML开头引入Ajax的注册语句:
<%@ Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax" %>
将你需要采用无刷新操作的控件包在MagicAjax的标签中,类似如下代码:
<ajax:AjaxPanel ID="AjaxPanel1" runat="Server">
<asp:DropDownList id="DropDownList1" runat="server"> </asp:DropDownList>
</ajax:AjaxPanel>
这样就可以了