if(this.mylist1.SelectedItem.Value=="1")
this.mylist2.Items[1].Selected=true;
else
this.mylist2.Items[2].Selected=true;
this.mylist2.Items[1].Selected=true;
else
this.mylist2.Items[2].Selected=true;
解决方案 »
- 无刷新技术
- 怎么给DataGridView的某一标题删掉换成ChenkBox按钮(winForm)
- Access 插入问题
- asp.net中怎么把sqlserver中存储的二进制图像在"image"控件中中显示出来
- 64位操作系统 WIN7 IIS7 发布.NET4.0 WCF 的问题(在线等~~~)
- 求助:PowerDesiger11
- 关于asp.net程序的发布
- 后台编程添加的Button怎样给它添加事件
- 那位兄弟见过这样的情况 SQL数据插入失败
- web应用中如何实现数字签名技术??---绝对困难的问题
- 为刚刚建成的社区送分100,想要的朋友来呀
- 怎样向DataGrid Web服务器控件添加超链接列??
Case "AA"
Me.MyList2.SelectedIndex = 0
MyList2.DataBind()
Case "BB"
Me.MyList2.SelectedIndex = 1
MyList2.DataBind()
End Select
你的代碼當選擇時mylist2是會變,但mylist1卻還是不變。
“擇時mylist2是會變,但mylist1卻還是不變”
this.mylist1.SelectedIndex=(this.mylist2.SelectedItem.Value=="1")?0:1;
}
我把你的改了一下,還是不行!
改動如下:
public void mylist2_SelectedChanged(object sender,EventArgs e){
this.mylist1.SelectedIndex=(Convert.ToInt32(this.mylist2.SelectedItem.Value));
}
public void mylist2_SelectedChanged(object sender,EventArgs e){
this.mylist1.SelectedIndex=(this.mylist2.SelectedItem.Value==1)?0:1;
}
可以将DropDownList:mylist1的设置属性
AutoPostBack=true
即可实现预定功能
=========================================
aspx:
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="NW.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebForm2" method="post" runat="server">
<asp:DropDownList id="d1" style="Z-INDEX: 101; LEFT: 261px; POSITION: absolute; TOP: 175px" runat="server" AutoPostBack="True">
<asp:ListItem Value="a">a</asp:ListItem>
<asp:ListItem Value="b">b</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="d2" style="Z-INDEX: 102; LEFT: 361px; POSITION: absolute; TOP: 175px" runat="server">
<asp:ListItem Value="x">x</asp:ListItem>
<asp:ListItem Value="y">y</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</HTML>
===========================================
aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace NW
{
/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList d1;
protected System.Web.UI.WebControls.DropDownList d2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.d1.SelectedIndexChanged += new System.EventHandler(this.d1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void d1_SelectedIndexChanged(object sender, System.EventArgs e)
{
d2.SelectedIndex=d1.SelectedIndex;
}
}
}
<asp:DropDownList id="mylist2" AutoPostBack="true"/>
改成
<asp:DropDownList id="mylist2" OnSelectedIndexChanged="mylist2_SelectedChanged" AutoPostBack="true"/>
if(this.mylist2.SelectedItem.Value=="1")
{
if(this.mylist1.SelectedItem.Value!="1")
this.mylist1.Items[1].Selected=true;
}
else
{
if(this.mylist1.SelectedItem.Value!="2")
this.mylist1.Items[2].Selected=true;
}
.....
public void Page_Load(object sender,EventArgs e){
//Bind mylist1------------------------------
myconn=new SqlConnection(conn1.connstr);
mycmd=new SqlCommand("requestClass_01",myconn);
mycmd.CommandType=CommandType.StoredProcedure;
myconn.Open();
mydr=mycmd.ExecuteReader();
mylist1.DataSource=mydr;
mylist1.DataBind();
myconn.Close();
//Bind mylist2--------------------------------
myconn.Open();
mycmd1=new SqlCommand("requestClass_02",myconn);
mycmd1.CommandType=CommandType.StoredProcedure;
mydr1=mycmd1.ExecuteReader();
mylist2.DataSource=mydr1;
mylist2.DataBind();
}
public void mylist1_SelectedChanged(object sender,EventArgs e){
if(this.mylist2.SelectedItem.Value=="1")
{
if(this.mylist1.SelectedItem.Value!="1")
this.mylist1.Items[1].Selected=true;
}
else
{
if(this.mylist1.SelectedItem.Value!="2")
this.mylist1.Items[2].Selected=true;
}
}
</script>
......
<asp:DropDownList ID="mylist1" runat="server" DataTextField="C005_002" DataValueField="C005_001" OnSelectedIndexChanged="mylist1_SelectedChanged" AutoPostBack="true"/><asp:DropDownList ID="mylist2" runat="server" DataTextField="C007_002" DataValueField="C007_001"/>
......
if (!page.postback()){ //**************加上试试
//Bind mylist1------------------------------
myconn=new SqlConnection(conn1.connstr);
mycmd=new SqlCommand("requestClass_01",myconn);
mycmd.CommandType=CommandType.StoredProcedure;
myconn.Open();
mydr=mycmd.ExecuteReader();
mylist1.DataSource=mydr;
mylist1.DataBind();
myconn.Close();
//Bind mylist2--------------------------------
myconn.Open();
mycmd1=new SqlCommand("requestClass_02",myconn);
mycmd1.CommandType=CommandType.StoredProcedure;
mydr1=mycmd1.ExecuteReader();
mylist2.DataSource=mydr1;
mylist2.DataBind();
}//**************加上试试
}
......
public void Page_Load(object sender,EventArgs e){
if(!Page.IsPostBack){
//Bind mylist1------------------------------
myconn=new SqlConnection(conn1.connstr);
mycmd=new SqlCommand("requestClass_01",myconn);
mycmd.CommandType=CommandType.StoredProcedure;
myconn.Open();
mydr=mycmd.ExecuteReader();
mylist1.DataSource=mydr;
mylist1.DataBind();
myconn.Close();
//Bind mylist2--------------------------------
myconn.Open();
mycmd1=new SqlCommand("requestClass_02",myconn);
mycmd1.CommandType=CommandType.StoredProcedure;
mydr1=mycmd1.ExecuteReader();
mylist2.DataSource=mydr1;
mylist2.DataBind();
}
}
public void mylist1_SelectedChanged(object sender,EventArgs e){
if(this.mylist1.SelectedItem.Value=="1")
this.mylist2.Items[0].Selected=true;
else
this.mylist2.Items[1].Selected=true;
}
public void mylist2_SelectedChanged(object sender,EventArgs e){
if(this.mylist2.SelectedItem.Value=="1")
{
if(this.mylist1.SelectedItem.Value!="1")
this.mylist1.Items[0].Selected=true;
}
else
{
if(this.mylist1.SelectedItem.Value!="2")
this.mylist1.Items[1].Selected=true;
}
}
.....
<asp:DropDownList ID="mylist1" runat="server" DataTextField="C005_002" DataValueField="C005_001" OnSelectedIndexChanged="mylist1_SelectedChanged" AutoPostBack="true"/><asp:DropDownList ID="mylist2" runat="server" DataTextField="C007_002" DataValueField="C007_001" OnSelectedIndexChanged="mylist2_SelectedChanged" AutoPostBack="true"/>
......
if(!Page.IsPostBack){
//Bind mylist1------------------------------
myconn=new SqlConnection(conn1.connstr);
mycmd=new SqlCommand("requestClass_01",myconn);
mycmd.CommandType=CommandType.StoredProcedure;
myconn.Open();
mydr=mycmd.ExecuteReader();
mylist1.DataSource=mydr;
mylist1.DataBind();
myconn.Close();
//Bind mylist2--------------------------------
myconn.Open();
mycmd1=new SqlCommand("requestClass_02",myconn);
mycmd1.CommandType=CommandType.StoredProcedure;
mydr1=mycmd1.ExecuteReader();
mylist2.DataSource=mydr1;
mylist2.DataBind();
}
}
public void mylist1_SelectedChanged(object sender,EventArgs e){
if(this.mylist1.SelectedItem.Value=="1")
this.mylist2.Items[1].Selected=true;
else
this.mylist2.Items[2].Selected=true;
}
</script>
我寫成
public void mylist1_SelectedChanged(object sender,EventArgs e){
if(this.mylist1.SelectedItem.Value=="1")
this.mylist2.Items[1].Selected=true;
else
this.mylist2.Items[2].Selected=true;
}
報錯:
Server Error in '/' Application.
--------------------------------------------------------------------------------Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: indexSource Error:
Line 40: this.mylist2.Items[1].Selected=true;
Line 41: else
Line 42: this.mylist2.Items[2].Selected=true;
Line 43: }所以我改成:Items[0],Items[1]
chinchy(人民需要人民币)的方法在靜態下可以,但我的數據從數據庫裡取出時就不行。