ASP.NET 取值 在线等啊 (select 经过javascript修改之后 服务端不能正常取值) 本帖最后由 k858k 于 2009-12-26 21:01:16 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Request.Form["selectname"] this.Select1.Items[Select1.SelectedIndex].Value; 服务器控件生成之后会加上特定的前缀,你可以运行页面后看源码,找到select的ID替换js里的Select1你也可以写client(Select1) 可能大家没明白我的意思我说下我的需求吧我需要在服务端取到 客户端select里的每一项 而select里边的 项 是通过javascript 来生成的下边的代码更接近我的需求吧<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"><title>无标题页</title><script type="text/javascript">function aa(){ for(var i= 0;i<10;i++) { var op = new Option(); op.text = "显示文本" + i; op.value = "结果" + i; document.getElementById("Select1").options.add(op); }}</script></head><body onload="aa();"> <form id="form1" runat="server"> <div> <select id="Select1" runat="server" size="4" style="width: 199px"></select> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form></body></html>public partial class Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { foreach (ListItem li in Select1.Items) { Response.Write(li.Text+"<br />"); } }} 放个隐藏域解决,测试通过。<script type="text/javascript">function aa(){ for(var i= 0;i<10;i++) { var op = new Option(); op.text = "显示文本" + i; op.value = "结果" + i; document.getElementById("Select1").options.add(op); }}function save()//保存到隐藏域{ var obj=document.getElementById("Select1"); var obj2=document.getElementById("HiddenField1"); for(var i=0;i<obj.options.length;i++) { obj2.value+=obj.options[i].value+"|"; }}</script><select id="Select1" runat="server" size="4" style="width: 199px"></select><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="save();"/><asp:HiddenField ID="HiddenField1" runat="server" /> protected void Button1_Click(object sender, EventArgs e) { string[] ss = HiddenField1.Value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in ss) { Response.Write(s + "<br />"); } } 注:此代码因为用到了 Ajax,如果没有部署服务器环境(IIS 或 Tomcat 或 Apache),请用 FireFox 查看。HTML 代码<!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script type="text/javascript"> var drpProvince;//省 var drpCity;//市 var drpCountry;//区、县市 var cityXmlDoc; $(function(){ /* 此处赋值是为了提高可用性以及可移植性,即正常情况下,只要在此处取得 省、市、区 三个 select,并在后台用 Request.Form["provinceID"] 即可获取值 */ drpProvince = $("#drpProvince");//省 drpCity = $("#drpCity");//市 drpCountry = $("#drpCountry");//区、县市 var selectOption = function (currentGrade,currentGradeValue,nextGrade,selectID){ $("#" + selectID + ">option").remove(); //当加载省列表时, currentGradeValue 是为空的 if(currentGradeValue != ""){ $(cityXmlDoc).find(currentGrade+"[name='"+ currentGradeValue +"']>" + nextGrade).each(function(){ $("#"+ selectID).append("<option value='"+ $(this).attr("name") +"'>" + $(this).attr("name") + "</option>"); }); }else{ $(cityXmlDoc).find(currentGrade+">" + nextGrade).each(function(){ $("#"+ selectID).append("<option value='"+ $(this).attr("name") +"'>" + $(this).attr("name") + "</option>"); }); } }; $.ajax({ url:"Area.xml", dataType:"xml", type:"GET", success:function(data){ cityXmlDoc=data;//$(data).find("address"); //加载省 selectOption("address","","province",drpProvince.attr("id")); drpProvince.bind("change",function(){ selectOption("province",$(this).val(),"city",drpCity.attr("id")); drpCity.change(); }); //加载市 drpProvince.change(); drpCity.bind("change",function(){ selectOption("city",$(this).val(),"country",drpCountry.attr("id")); }); //加载区、县市 drpCity.change(); selectOption("city",drpCity.val() ,"country",drpCountry.attr("id")); }, error:function(data){var len =$(data).find("address").length;alert("失败:" + len);} }); $("form").bind("submit",function(){ $(this).append("<input type='hidden' name='"+ drpProvince.attr("id") +"' value='"+ drpProvince.val() +"'/>"); $(this).append("<input type='hidden' name='"+ drpCity.attr("id") +"' value='"+ drpCity.val() +"'/>"); $(this).append("<input type='hidden' name='"+ drpCountry.attr("id") +"' value='"+ drpCountry.val() +"'/>"); alert($("form :hidden").length); }); }); </script> </head> <body> <form name="t" method="post"> 省<select id="drpProvince"></select> 市<select id="drpCity"></select> <select id="drpCountry"></select> <br/> <input type="submit" value="提交"/> </form> </body> </html>Area.xml(注:因为提交内容长度限制 部分省市区未能列出)<?xml version="1.0" encoding="utf-8" ?><address> <province name="北京市"> <city name="北京辖区"> <country name="东城区" /> <country name="西城区" /> <country name="崇文区" /> <country name="宣武区" /> <country name="朝阳区" /> <country name="丰台区" /> <country name="石景山区" /> <country name="海淀区" /> <country name="门头沟区" /> <country name="房山区" /> <country name="通州区" /> <country name="顺义区" /> <country name="昌平区" /> <country name="大兴区" /> <country name="怀柔区" /> <country name="平谷区" /> </city> <city name="北京辖县"> <country name="密云县" /> <country name="延庆县" /> </city> </province> <province name="天津市"> <city name="天津辖区"> <country name="和平区" /> <country name="河东区" /> <country name="河西区" /> <country name="南开区" /> <country name="河北区" /> <country name="红桥区" /> <country name="塘沽区" /> <country name="汉沽区" /> <country name="大港区" /> <country name="东丽区" /> <country name="西青区" /> <country name="津南区" /> <country name="北辰区" /> <country name="武清区" /> <country name="宝坻区" /> </city> <city name="天津辖县"> <country name="宁河县" /> <country name="静海县" /> <country name="蓟县" /> </city> </province> <province name="河北省"> <city name="石家庄市"> <country name="市辖区" /> <country name="长安区" /> <country name="桥东区" /> <country name="桥西区" /> <country name="新华区" /> <country name="井陉矿区" /> <country name="裕华区" /> <country name="井陉县" /> <country name="正定县" /> <country name="栾城县" /> <country name="行唐县" /> <country name="灵寿县" /> <country name="高邑县" /> <country name="深泽县" /> <country name="赞皇县" /> <country name="无极县" /> <country name="平山县" /> <country name="元氏县" /> <country name="赵县" /> <country name="辛集市" /> <country name="藁城市" /> <country name="晋州市" /> <country name="新乐市" /> <country name="鹿泉市" /> </city> <city name="唐山市"> <country name="市辖区" /> <country name="路南区" /> <country name="路北区" /> <country name="古冶区" /> <country name="开平区" /> <country name="丰南区" /> <country name="丰润区" /> <country name="滦县" /> <country name="滦南县" /> <country name="乐亭县" /> <country name="迁西县" /> <country name="玉田县" /> <country name="唐海县" /> <country name="遵化市" /> <country name="迁安市" /> </city> <city name="保定市"> <country name="市辖区" /> <country name="新市区" /> <country name="北市区" /> <country name="南市区" /> <country name="满城县" /> <country name="清苑县" /> <country name="涞水县" /> <country name="阜平县" /> <country name="徐水县" /> <country name="定兴县" /> <country name="唐县" /> <country name="高阳县" /> <country name="容城县" /> <country name="涞源县" /> <country name="望都县" /> <country name="安新县" /> <country name="易县" /> <country name="曲阳县" /> <country name="蠡县" /> <country name="顺平县" /> <country name="博野县" /> <country name="雄县" /> <country name="涿州市" /> <country name="定州市" /> <country name="安国市" /> <country name="高碑店市" /> </city> <city name="张家口市"> <country name="市辖区" /> <country name="桥东区" /> <country name="桥西区" /> <country name="宣化区" /> <country name="下花园区" /> <country name="宣化县" /> <country name="张北县" /> <country name="康保县" /> <country name="沽源县" /> <country name="尚义县" /> <country name="蔚县" /> <country name="阳原县" /> <country name="怀安县" /> <country name="万全县" /> <country name="怀来县" /> <country name="涿鹿县" /> <country name="赤城县" /> <country name="崇礼县" /> </city> <city name="承德市"> <country name="市辖区" /> <country name="双桥区" /> <country name="双滦区" /> <country name="鹰手营子矿区" /> <country name="承德县" /> <country name="兴隆县" /> <country name="平泉县" /> <country name="滦平县" /> <country name="隆化县" /> <country name="丰宁满族自治县" /> <country name="宽城满族自治县" /> <country name="围场满族蒙古族自治县" /> </city> <city name="沧州市"> <country name="市辖区" /> <country name="新华区" /> <country name="运河区" /> <country name="沧县" /> <country name="青县" /> <country name="东光县" /> <country name="海兴县" /> <country name="盐山县" /> <country name="肃宁县" /> <country name="南皮县" /> <country name="吴桥县" /> <country name="献县" /> <country name="孟村回族自治县" /> <country name="泊头市" /> <country name="任丘市" /> <country name="黄骅市" /> <country name="河间市" /> </city> <city name="廊坊市"> <country name="市辖区" /> <country name="安次区" /> <country name="广阳区" /> <country name="固安县" /> <country name="永清县" /> <country name="香河县" /> <country name="大城县" /> <country name="文安县" /> <country name="大厂回族自治县" /> <country name="霸州市" /> <country name="三河市" /> </city> <city name="衡水市"> <country name="市辖区" /> <country name="桃城区" /> <country name="枣强县" /> <country name="武邑县" /> <country name="武强县" /> <country name="饶阳县" /> <country name="安平县" /> <country name="故城县" /> <country name="景县" /> <country name="阜城县" /> <country name="冀州市" /> <country name="深州市" /> </city> </province></address> ajax 验证登录的,完了之后不能跳转,晕啊,求助 弹出对话框 关于在APP_Code中的代码使用Application变量的问题 触发器能生成一张表出来不啊?急!! 球赛文字无刷新直播怎么实现?网上都搜索不到的? 如果将一个XMLNODE 添加到另一个XML文件中 关闭页面问题 webform中的文件访问 !!!!!一个菜鸟级问题,http://localhost连不上了???? 微软Web 中的treeview又改了﹐真是。。。﹐我记得和以前的那个不一样。 谁来救救我啊,要疯了。。。 asp net如何让后台代码控制div层显示及隐藏
this.Select1.Items[Select1.SelectedIndex].Value;
你也可以写client(Select1)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function aa(){
for(var i= 0;i<10;i++)
{
var op = new Option();
op.text = "显示文本" + i;
op.value = "结果" + i;
document.getElementById("Select1").options.add(op);
}
}
</script>
</head><body onload="aa();">
<form id="form1" runat="server">
<div>
<select id="Select1" runat="server" size="4" style="width: 199px"></select>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
foreach (ListItem li in Select1.Items)
{
Response.Write(li.Text+"<br />");
}
}
}
function aa(){
for(var i= 0;i<10;i++)
{
var op = new Option();
op.text = "显示文本" + i;
op.value = "结果" + i;
document.getElementById("Select1").options.add(op);
}
}
function save()//保存到隐藏域
{
var obj=document.getElementById("Select1");
var obj2=document.getElementById("HiddenField1");
for(var i=0;i<obj.options.length;i++)
{
obj2.value+=obj.options[i].value+"|";
}
}
</script>
<select id="Select1" runat="server" size="4" style="width: 199px"></select>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="save();"/>
<asp:HiddenField ID="HiddenField1" runat="server" />
{
string[] ss = HiddenField1.Value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in ss)
{
Response.Write(s + "<br />");
} }
HTML 代码
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
var drpProvince;//省
var drpCity;//市
var drpCountry;//区、县市
var cityXmlDoc;
$(function(){
/* 此处赋值是为了提高可用性以及可移植性,即正常情况下,只要在此处取得 省、市、区 三个 select,并在后台用 Request.Form["provinceID"] 即可获取值 */
drpProvince = $("#drpProvince");//省
drpCity = $("#drpCity");//市
drpCountry = $("#drpCountry");//区、县市
var selectOption = function (currentGrade,currentGradeValue,nextGrade,selectID){
$("#" + selectID + ">option").remove();
//当加载省列表时, currentGradeValue 是为空的
if(currentGradeValue != ""){
$(cityXmlDoc).find(currentGrade+"[name='"+ currentGradeValue +"']>" + nextGrade).each(function(){
$("#"+ selectID).append("<option value='"+ $(this).attr("name") +"'>" + $(this).attr("name") + "</option>");
});
}else{
$(cityXmlDoc).find(currentGrade+">" + nextGrade).each(function(){
$("#"+ selectID).append("<option value='"+ $(this).attr("name") +"'>" + $(this).attr("name") + "</option>");
});
}
};
$.ajax({
url:"Area.xml",
dataType:"xml",
type:"GET",
success:function(data){
cityXmlDoc=data;//$(data).find("address");
//加载省
selectOption("address","","province",drpProvince.attr("id"));
drpProvince.bind("change",function(){
selectOption("province",$(this).val(),"city",drpCity.attr("id"));
drpCity.change();
});
//加载市
drpProvince.change();
drpCity.bind("change",function(){
selectOption("city",$(this).val(),"country",drpCountry.attr("id"));
});
//加载区、县市
drpCity.change();
selectOption("city",drpCity.val() ,"country",drpCountry.attr("id"));
},
error:function(data){var len =$(data).find("address").length;alert("失败:" + len);}
});
$("form").bind("submit",function(){
$(this).append("<input type='hidden' name='"+ drpProvince.attr("id") +"' value='"+ drpProvince.val() +"'/>");
$(this).append("<input type='hidden' name='"+ drpCity.attr("id") +"' value='"+ drpCity.val() +"'/>");
$(this).append("<input type='hidden' name='"+ drpCountry.attr("id") +"' value='"+ drpCountry.val() +"'/>");
alert($("form :hidden").length);
});
});
</script>
</head>
<body>
<form name="t" method="post">
省<select id="drpProvince"></select>
市<select id="drpCity"></select>
<select id="drpCountry"></select>
<br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
Area.xml(注:因为提交内容长度限制 部分省市区未能列出)<?xml version="1.0" encoding="utf-8" ?>
<address>
<province name="北京市">
<city name="北京辖区">
<country name="东城区" />
<country name="西城区" />
<country name="崇文区" />
<country name="宣武区" />
<country name="朝阳区" />
<country name="丰台区" />
<country name="石景山区" />
<country name="海淀区" />
<country name="门头沟区" />
<country name="房山区" />
<country name="通州区" />
<country name="顺义区" />
<country name="昌平区" />
<country name="大兴区" />
<country name="怀柔区" />
<country name="平谷区" />
</city>
<city name="北京辖县">
<country name="密云县" />
<country name="延庆县" />
</city>
</province>
<province name="天津市">
<city name="天津辖区">
<country name="和平区" />
<country name="河东区" />
<country name="河西区" />
<country name="南开区" />
<country name="河北区" />
<country name="红桥区" />
<country name="塘沽区" />
<country name="汉沽区" />
<country name="大港区" />
<country name="东丽区" />
<country name="西青区" />
<country name="津南区" />
<country name="北辰区" />
<country name="武清区" />
<country name="宝坻区" />
</city>
<city name="天津辖县">
<country name="宁河县" />
<country name="静海县" />
<country name="蓟县" />
</city>
</province>
<province name="河北省">
<city name="石家庄市">
<country name="市辖区" />
<country name="长安区" />
<country name="桥东区" />
<country name="桥西区" />
<country name="新华区" />
<country name="井陉矿区" />
<country name="裕华区" />
<country name="井陉县" />
<country name="正定县" />
<country name="栾城县" />
<country name="行唐县" />
<country name="灵寿县" />
<country name="高邑县" />
<country name="深泽县" />
<country name="赞皇县" />
<country name="无极县" />
<country name="平山县" />
<country name="元氏县" />
<country name="赵县" />
<country name="辛集市" />
<country name="藁城市" />
<country name="晋州市" />
<country name="新乐市" />
<country name="鹿泉市" />
</city>
<city name="唐山市">
<country name="市辖区" />
<country name="路南区" />
<country name="路北区" />
<country name="古冶区" />
<country name="开平区" />
<country name="丰南区" />
<country name="丰润区" />
<country name="滦县" />
<country name="滦南县" />
<country name="乐亭县" />
<country name="迁西县" />
<country name="玉田县" />
<country name="唐海县" />
<country name="遵化市" />
<country name="迁安市" />
</city>
<city name="保定市">
<country name="市辖区" />
<country name="新市区" />
<country name="北市区" />
<country name="南市区" />
<country name="满城县" />
<country name="清苑县" />
<country name="涞水县" />
<country name="阜平县" />
<country name="徐水县" />
<country name="定兴县" />
<country name="唐县" />
<country name="高阳县" />
<country name="容城县" />
<country name="涞源县" />
<country name="望都县" />
<country name="安新县" />
<country name="易县" />
<country name="曲阳县" />
<country name="蠡县" />
<country name="顺平县" />
<country name="博野县" />
<country name="雄县" />
<country name="涿州市" />
<country name="定州市" />
<country name="安国市" />
<country name="高碑店市" />
</city>
<city name="张家口市">
<country name="市辖区" />
<country name="桥东区" />
<country name="桥西区" />
<country name="宣化区" />
<country name="下花园区" />
<country name="宣化县" />
<country name="张北县" />
<country name="康保县" />
<country name="沽源县" />
<country name="尚义县" />
<country name="蔚县" />
<country name="阳原县" />
<country name="怀安县" />
<country name="万全县" />
<country name="怀来县" />
<country name="涿鹿县" />
<country name="赤城县" />
<country name="崇礼县" />
</city>
<city name="承德市">
<country name="市辖区" />
<country name="双桥区" />
<country name="双滦区" />
<country name="鹰手营子矿区" />
<country name="承德县" />
<country name="兴隆县" />
<country name="平泉县" />
<country name="滦平县" />
<country name="隆化县" />
<country name="丰宁满族自治县" />
<country name="宽城满族自治县" />
<country name="围场满族蒙古族自治县" />
</city>
<city name="沧州市">
<country name="市辖区" />
<country name="新华区" />
<country name="运河区" />
<country name="沧县" />
<country name="青县" />
<country name="东光县" />
<country name="海兴县" />
<country name="盐山县" />
<country name="肃宁县" />
<country name="南皮县" />
<country name="吴桥县" />
<country name="献县" />
<country name="孟村回族自治县" />
<country name="泊头市" />
<country name="任丘市" />
<country name="黄骅市" />
<country name="河间市" />
</city>
<city name="廊坊市">
<country name="市辖区" />
<country name="安次区" />
<country name="广阳区" />
<country name="固安县" />
<country name="永清县" />
<country name="香河县" />
<country name="大城县" />
<country name="文安县" />
<country name="大厂回族自治县" />
<country name="霸州市" />
<country name="三河市" />
</city>
<city name="衡水市">
<country name="市辖区" />
<country name="桃城区" />
<country name="枣强县" />
<country name="武邑县" />
<country name="武强县" />
<country name="饶阳县" />
<country name="安平县" />
<country name="故城县" />
<country name="景县" />
<country name="阜城县" />
<country name="冀州市" />
<country name="深州市" />
</city>
</province>
</address>