小弟新搞ASP.NET,遇到一下问题(用的VB.NET):
有两个DropDownList,我要求选择个第一个的项目时,根据选择的值来改变第二个DropDownList中的可选项目,我使用SelectedIndexChanged事件,但是无论我怎么选,这个事件也不发生,请问为什么?
是不是不应该这么做?应该如何做才能实现?谢谢!能给个例子就好了
有两个DropDownList,我要求选择个第一个的项目时,根据选择的值来改变第二个DropDownList中的可选项目,我使用SelectedIndexChanged事件,但是无论我怎么选,这个事件也不发生,请问为什么?
是不是不应该这么做?应该如何做才能实现?谢谢!能给个例子就好了
解决方案 »
- 类似QQ农场的快速登录 思路
- asp.net gridview编辑时,下拉框选择上默认的选项
- VB.NEt 中调用 FreeTextBox控件,但是这个控件显示出的内容的行距太大,有没有办法可以改善呀! 请指教! 谢谢!
- 如何在页面打开时取出浏览器前一页面?急。。
- **100分求两个问题,一是工程的引用,二是接口还是抽象
- VS从2002升级到2003后发现水晶报表只能打开却无法编辑了
- ======第2个帖100分 求cs里写类似ASP程序的代码!开两个贴,每个100分,帮助.Net半菜鸟活动! ========
- 跪求大神帮忙解决一下这个异常。
- 如何求一個子Substing在一個ParentString 中的位置呢?
- 各位帮我诊断一下
- 如何才能新建类库,而不建项目?
- *********入门问题,请教大家*********
2.在DropDownList_1_SelectedIndexChanged中写DropDownList2绑定事件
或者其他的不提交的方法
在点DropDownList 就会产生一个selectindexchage事件,接下来的 你应该知道了吧
你可以为他自定义一个ONCHANG处理事件,比如在一个留言本里边我选择一DropDownList中的一个头像名字马上显出相应的头像,你可以这个样子写,首先在它的创建代码中加上事件
<asp:dropdownlist id="DropDownList1" runat="server" Width="104px" onchange="javascript:imgshow.src='face/'+this.value">
<asp:ListItem Value="10048.gif">10048.gif</asp:ListItem>
<asp:ListItem Value="10049.gif">10049.gif</asp:ListItem>
<asp:ListItem Value="10050.gif">10050.gif</asp:ListItem>
<asp:ListItem Value="10051.gif">10051.gif</asp:ListItem>
<asp:ListItem Value="10052.gif">10052.gif</asp:ListItem>
<asp:ListItem Value="10053.gif">10053.gif</asp:ListItem>
<asp:ListItem Value="10054.gif">10054.gif</asp:ListItem>
<asp:ListItem Value="10055.gif">10055.gif</asp:ListItem>
<asp:ListItem Value="10056.gif">10056.gif</asp:ListItem>
<asp:ListItem Value="10057.gif">10057.gif</asp:ListItem>
<asp:ListItem Value="10058.gif">10058.gif</asp:ListItem>
<asp:ListItem Value="10059.gif">10059.gif</asp:ListItem>
</asp:dropdownlist></TD>
最后在他所在页面<body>中加下下边代码响应,让其当点击时马上发生:
<BODY onload="javascript:Form1.imgshow.src='face/'+Form1.DropDownList1.value">
以下为页面代码:<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
<asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList>
<SCRIPT LANGUAGE="JavaScript">
<!--
//以XML求取数据
function XmlPost(obj)
{
var svalue = obj.value;
var webFileUrl = "?brc_id=" + svalue;
var result = "";
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send("");
result = xmlHttp.responseText;
if(result != "")
{
document.all("DropDownList2").length=0;
var piArray = result.split(",");
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
//alert(ary1[0].toString());
document.all("DropDownList2").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
else
{
alert(result);
}
}
//-->
</SCRIPT>
</form>以下为后台代码:private System.Data.OleDb.OleDbConnection conn;private DataTable get_dt(string sql)
{
string connstr = "Provider=MSDAORA.1;Password=aqjc;User ID=aqjc;Data Source=aqjc";
this.conn = new OleDbConnection(connstr);
this.conn.Open();
OleDbCommand myOleDbCommand = new OleDbCommand(sql,this.conn);
OleDbDataAdapter myData = new OleDbDataAdapter(myOleDbCommand); DataSet myDataset = new DataSet();
try
{
myData.Fill(myDataset);
}
catch(Exception ex)
{
throw ex;
} this.conn.Close();
return myDataset.Tables[0];
}private void Page_Load(object sender, System.EventArgs e)
{
string brc_id = this.Request.QueryString["brc_id"];
if(brc_id + "a" != "a")
{
this.down2_bind(brc_id);
} if(!this.IsPostBack)
{
this.down1_bind();
}
}/// <summary>
/// 返回第2个下拉框需要的值给xmlhttp
/// </summary>
/// <param name="brc_id"></param>
private void down2_bind(string brc_id)
{
string mystr = "";
string sql = "select brc_id,brc_name from asm_branch where brc_parentid = '" + brc_id + "'";
DataTable mytab = this.get_dt(sql); if(mytab.Rows.Count != 0)
{
for(int i=0;i<mytab.Rows.Count;i++)
{
mystr += "," + mytab.Rows[i][0].ToString() + "|" + mytab.Rows[i][1].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}/// <summary>
/// 绑定第一个下拉框
/// </summary>
private void down1_bind()
{
string sql = "select brc_id,brc_name from asm_branch where brc_level = '1'";
DataTable mytab = this.get_dt(sql);
this.DropDownList1.DataSource = mytab;
this.DropDownList1.DataValueField = "brc_id";
this.DropDownList1.DataTextField = "brc_name";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange","XmlPost(this);");
}
只要将DropDownList1的AutoPostBack=True即可。
而如果你的数据是从数据库读出
请将代码放到
If Not IsPostBack Then
'数据绑定
End If