DropDownList控件和Button控件的联动??
ASP.NET网页中,有一个DropDownList控件,和Button控件
DropDownList控件内显示的内容是从数据库中提取出来的
希望实现的功能是点击Button后,出现 选中的DropDownList条目 的详情
出现的问题是:每次选择好DropDownList条目后,再点击Button,最后都现在第一个条目的详情,请问这个问题怎么解决
aspx:
<asp:DropDownList ID="DropDownList1" DataTextField="NUMBER" DataValueField="NUMBER" AutoPostBack="false" runat="server"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click" />
aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.DataSource = bc.GetDataSet("select NUMBER from bus", "bus");
DropDownList1.DataBind(); }
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSource = bc.GetDataSet("select * from bus where NUMBER='" + DropDownList1.SelectedValue + "'", "bus");
GridView1.DataBind();
Panel1.Visible = true;
}
------解决方案--------------------
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
DropDownList1.DataSource = bc.GetDataSet("select NUMBER from bus", "bus");
DropDownList1.DataBind(); }
}
原因是按钮按下之后,先触发page_load事件,结果downlist重新绑定,DropDownList1.SelectedValue 总是第一项了