dropdownlist默认选取问题 有两个dropdownlist属于联动的,从数据库捞值的。是个注册页面,问题在提交后如输入有误,dropdownlist就恢复默认的第一个值了,如何让之前选取的依然没变动。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 联动的方法很多,下面是一个完全asp.net的实现方法,直接拷贝代码即可看效果的。<%@ Page Language="C#" Debug="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> //测试数据 String[] A = { "A", "B", "C" }; Hashtable b = new Hashtable(); protected void Page_Load(object sender, EventArgs e) { b.Add("A", "A1:A2"); b.Add("B", "B1:B2"); b.Add("C", "C1:C2"); if (!Page.IsPostBack) { DropDownList1.DataSource = A; DropDownList1.DataBind(); DropDownList2.DataSource = b["A"].ToString().Split(':'); DropDownList2.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { Response.Write(DropDownList1.SelectedValue + ":" + DropDownList2.SelectedValue); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList2.DataSource = b[DropDownList1.SelectedValue].ToString().Split(':'); DropDownList2.DataBind(); }</script><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server"> <title></title></head><body> <form id="form1" runat="server"> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server"> </asp:DropDownList> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> </form></body></html> 因为是联动,所以要根据第一个去改变第二个,所以第一个dropdownlist的autopostback肯定为true,那么你选择一次,他页面刷新一次,dropdownlist重新填充一次,他肯定不会停留在你第一次选择的那个value上。Page_Load事件下面加上 if (!Page.IsPostBack)然后在这个if下面写实现过程。 我不是求联动的方法,联动完全OK的。比如一个注册页面,如果说你填写的用户名重复,那肯定不能通过验证,那么这个时候两个dropdownlist的都默认选择到第一个value上了,我是求如果没通过验证让他保持之前选项不变的方法~ 在验证失败后用S protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ddlMedia.SelectedIndex = Convert.ToInt32(Session["mediaIndex"]); ddlQuarter.SelectedIndex = Convert.ToInt32(Session["quarterIndex"]); } } protected void Button1_Click(object sender, EventArgs e) { //if(验证失败) Session["mediaIndex"] = ddlMedia.SelectedIndex; Session["quarterIndex"] = ddlQuarter.SelectedIndex; Response.Redirect("~/WebForm1.aspx"); }ession存起来,然后在回发的过程中重新设置就行啦 我的样式怎末设置啊,急,急,急啊 采集服务器上有可能面对并发如:{两个用户同时请求同一网址},请问有没办法解决此并发? 跪求高手解决.NET问题 jquery 谁有分页源码? 急急急,关于跨网站POST数据 有关SQL的错误! 用ASP。NET怎样做类似CSDN里面的弹出窗体啊,,填简历时 如何在程序里将一个excel文件转换成html文件 下了N个都不能接收邮件,谁给我一个可以用的jmail4.4pro(mail:[email protected]) 视频播放插件 fckeditor运行后是html代码 急。在线等。马上给分 如何将网页地址简单化到输入IP或域名即可
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
//测试数据
String[] A = { "A", "B", "C" };
Hashtable b = new Hashtable(); protected void Page_Load(object sender, EventArgs e)
{
b.Add("A", "A1:A2");
b.Add("B", "B1:B2");
b.Add("C", "C1:C2");
if (!Page.IsPostBack)
{
DropDownList1.DataSource = A;
DropDownList1.DataBind();
DropDownList2.DataSource = b["A"].ToString().Split(':');
DropDownList2.DataBind();
} } protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(DropDownList1.SelectedValue + ":" + DropDownList2.SelectedValue);
} protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.DataSource = b[DropDownList1.SelectedValue].ToString().Split(':');
DropDownList2.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
那么你选择一次,他页面刷新一次,dropdownlist重新填充一次,他肯定不会停留在你第一次选择的那个value上。Page_Load事件下面加上 if (!Page.IsPostBack)
然后在这个if下面写实现过程。
比如一个注册页面,如果说你填写的用户名重复,那肯定不能通过验证,那么这个时候两个dropdownlist的都默认选择到第一个value上了,我是求如果没通过验证让他保持之前选项不变的方法~
{
if (!IsPostBack)
{
ddlMedia.SelectedIndex = Convert.ToInt32(Session["mediaIndex"]);
ddlQuarter.SelectedIndex = Convert.ToInt32(Session["quarterIndex"]);
}
} protected void Button1_Click(object sender, EventArgs e)
{
//if(验证失败)
Session["mediaIndex"] = ddlMedia.SelectedIndex;
Session["quarterIndex"] = ddlQuarter.SelectedIndex;
Response.Redirect("~/WebForm1.aspx");
}ession存起来,然后在回发的过程中重新设置就行啦