当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net中关于dropdwonlist无法获得值问题

asp.net中关于dropdwonlist无法获得值问题

2017年12月12日  | 移动技术网IT编程  | 我要评论
前端控件: 复制代码 代码如下: <label>发布栏目:<asp:dropdownlist id="sectiondropdownlist" runat
前端控件:
复制代码 代码如下:

<label>发布栏目:<asp:dropdownlist id="sectiondropdownlist" runat="server"></asp:dropdownlist></label

数据绑定:
复制代码 代码如下:

sourcedb dropdwonlistdata = new sourcedb();
string dropdwonselect = "select * from [section]";
sectiondropdownlist.datasource = dropdwonlistdata.datasetdb(dropdwonselect).tables[0].defaultview;
sectiondropdownlist.datatextfield = "name";
sectiondropdownlist.datavaluefield = "code";
sectiondropdownlist.databind();

button事件:
复制代码 代码如下:

string newstitle = sectiondropdownlist.selectedvalue;
response.write(newstitle);

问题分析:

因为在page_load中每次都绑定了数据源,而去调用button事件时,实际是每次都刷新了页面的,于是每次在打印出来前都是初始化的值,于是每次都是输出的的一个值。

问题解决:

判断是否是页面回调。

前端控件:
复制代码 代码如下:

<label>发布栏目:<asp:dropdownlist id="sectiondropdownlist" runat="server"></asp:dropdownlist></label

数据绑定:
复制代码 代码如下:

if(!ispostback){
  sourcedb dropdwonlistdata
= new sourcedb();
  string dropdwonselect = "select * from [section]";
  sectiondropdownlist.datasource = dropdwonlistdata.datasetdb(dropdwonselect).tables[0].defaultview;
  sectiondropdownlist.datatextfield = "name";
  sectiondropdownlist.datavaluefield = "code";
  sectiondropdownlist.databind();
}


button事件:
复制代码 代码如下:

string newstitle = sectiondropdownlist.selectedvalue;
response.write(newstitle);

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网