服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - DropDownList获取的SelectIndex一直为0的问题

DropDownList获取的SelectIndex一直为0的问题

2019-12-04 10:57ASP.NET教程网 ASP.NET教程

由于初始化判断出错导致每次传到服务器的时候会初始化一次,这就导致每次获取DropDownList的SelectIndex的时候只能是0

1.想要DropDownList自动提交必须设置AutoPostBack="true"属性,下面是代码: 

复制代码代码如下:


<asp:DropDownList ID="ddlNameList" runat="Server" Height="30" 
AutoPostBack="True" onselectedindexchanged="ddlNameList_SelectedIndexChanged" ></asp:DropDownList> 


2.在服务端处理的时候,尤其是初始化DropDownList的时候,没注意结果写错了,下面是错误代码: 

复制代码代码如下:


protected void Page_Load(object sender, EventArgs e) 


if (!Page.IsCallBack) 

this.fillIntoNameList(); 


这个初始化判断出错了,每次传到服务器的时候会初始化一次,这就导致每次获取DropDownList的SelectIndex的时候只能是0 

正确代码,如下: 

复制代码代码如下:


protected void Page_Load(object sender, EventArgs e) 


if (!Page.IsPostBack) 

this.fillIntoNameList(); 

延伸 · 阅读

精彩推荐