C#后台获取不了前台的html标签select的值
C#后台获取不了前台的html标签select的值。
前台: 公司名称:
<select id="company" runat="server" style="width: 100px;" >
<option value='-1'>-请选择-</option>
</select>
//通过js加载选项。
后台: string str = company.Items[company.SelectedIndex].Text;
//前台选择了其他项,但是str还是一直等于"-请选择-"
怎么修改才能获取前台选择的值啊???
[解决办法]
用通过js给隐藏域赋值你选择的下拉值,然后再后台通过隐藏域显示
[解决办法]
给select加个name,后台获取name里的值
[解决办法]
<select id="company" runat="server" style="width: 100px;" onchange='savetext.value=this.value'>
<option value='-1'>-请选择-</option>
</select>
<input type='hidden' id='savetext' runat='server' />
类似这样
后台就直接读取savetext的值
[解决办法]
你后台是怎么获取的?
<select id="company" runat="server" style="width: 100px;" >
用js加载 runat="server"无效
如果你把他当成控件形式肯定不行。比如用Request.QueryString["user"]的形式才可以吧。
[解决办法]
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm8.aspx.cs" Inherits="WebApplication1.WebForm8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS对select动态添加options操作[IE和FireFox兼容]</title>
<script language="javascript" type="text/javascript">
function xlbchange(s) {
switch (s) {
case "1":
document.getElementById("lb").options.length = 0;
var soojs_value = [0, 1, 2, 3];
var soojs_text = ["精神提炼", "作风设计", "目标设置", "理念提升"];
for (var i = 0; i < soojs_value.length; i++) {
var oOption = document.createElement("OPTION");
oOption.value = soojs_value[i];
oOption.text = soojs_text[i];
zpmange.lb.options.add(oOption);
}
break;
case "2":
document.getElementById("lb").options.length = 0;
var soojs_value = [0, 1, 2, 3, 4, 5, 6, 7];
var soojs_text = ["校徽", "校训", "校歌", "校名字体", "校史展室", "宣传画册", "宣传光盘", "办公用品纪念品"];
for (var i = 0; i < soojs_value.length; i++) {
var oOption = document.createElement("OPTION");
oOption.value = soojs_value[i];
oOption.text = soojs_text[i];
zpmange.lb.options.add(oOption);
}
break;
case "3":
document.getElementById("lb").options.length = 0;
var soojs_value = [0, 1, 2, 3];
var soojs_text = ["校园景观设计", "校园雕塑设计", "校园浮雕设计", "走廊文化设计"];
for (var i = 0; i < soojs_value.length; i++) {
var oOption = document.createElement("OPTION");
oOption.value = soojs_value[i];
oOption.text = soojs_text[i];
zpmange.lb.options.add(oOption);
}
break;
case "4":
document.getElementById("lb").options.length = 0;
var soojs_value = [0, 1, 2];
var soojs_text = ["学校制度", "文化活动", "行为规范"];
for (var i = 0; i < soojs_value.length; i++) {
var oOption = document.createElement("OPTION");
oOption.value = soojs_value[i];
oOption.text = soojs_text[i];
zpmange.lb.options.add(oOption);
}
break;
default:
document.getElementById("lb").options.length = 0;
var oOption = document.createElement("OPTION");
oOption.value = 0;
oOption.text = "请选择作品类别";
zpmange.lb.options.add(oOption);
}
}
</script>
</head>
<body>
<form id="zpmange" runat="server">
<p>例子:</p>
<select name="xt" id="xt" onchange="xlbchange(this.value)">
<option selected="selected">请选择作品系统</option>
<option value="1">理念视别系统</option>
<option value="2">视觉识别系统</option>
<option value="3">环境视别系统</option>
<option value="4">行为视别系统</option>
</select>
<select name="lb" id="lb">
<option selected="selected">请选择作品类别</option>
</select>
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click1" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
Response.Write(Request.Form["lb"]);
}
}
}