日期:2014-05-19  浏览次数:20753 次

DropDownList始终无法选定
<%@   Page   Language= "C# "   AutoEventWireup= "true "     CodeFile= "Default.aspx.cs "   Inherits= "_Default "   %>
<%@Import   Namespace= "System.IO "%>  
<%
string[]   achDrives=Directory.GetLogicalDrives();
int   nNumOfDrives=achDrives.Length;
for   (int   i=0;i <nNumOfDrives;i++)
        DropDownList1.Items.Add(achDrives[i]);
DropDownList1.SelectedIndex   =   0;           //这里选定第一个
%>


protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
              Label2.Text   =   DropDownList1.Text;
//这里DropDownList1.Text始终为空,就算在这之前再设置选择一次也一样
          }


为什么?怎么解决?




------解决方案--------------------
DropDownList degree=new DropDownList();
DataTable dt=new DataTable();
dt.Columns.Add( "DegreeText ");
dt.Columns.Add( "DegreeValue ");
DataRow dr=dt.NewRow();
dr[ "DegreeText "]= "大专 ";
dr[ "DegreeValue "]=1;
dt.Rows.Add(dr);
dr=dt.NewRow();
dr[ "DegreeText "]= "本科 ";
dr[ "DegreeValue "]=2;
dt.Rows.Add(dr);

degree.DataSource=dt;
degree.DataFieldText= "DegreeText ";
degree.DataFieldValue= "DegreeValue ";
degree.DataBind();
degree.SelectValue= "2 ";
------解决方案--------------------
DropDownList1.selectedText 吧
------解决方案--------------------
把你的代码写到 CS文件中,因为你点击按钮时,事件的顺序是
服务器的Page_load --> button click --> 客户端画面load,
所以你原来的写法是不可以的.也就是还没初始化下拉列表

cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] achDrives = Directory.GetLogicalDrives();
int nNumOfDrives = achDrives.Length;
for (int i = 0; i < nNumOfDrives; i++)
DropDownList1.Items.Add(achDrives[i]);
DropDownList1.SelectedIndex = 0; //这里选定第一个
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DropDownList1.Text;
}
}

------解决方案--------------------
Label2.Text =this.DropDownList1.SelectedValue