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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解DropDownList获取的SelectIndex一直为0的问题的相关内容吗,在本文为您仔细讲解SelectIndex一直为0的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:DropDownList,SelectIndex,下面大家一起来学习吧。
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();
}
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们