asp.net dropdownlist问题

来源:百度知道 编辑:UC知道 时间:2024/07/04 19:28:46
请问为什么没有办法取出DropDownList1.SelectedValue的值?
标记:
<asp:DropDownList ID="DropDownList1" runat="server">

</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"
Width="73px" />
代码:
public partial class Default2 : System.Web.UI.Page
{
SqlConnection sqlCn;
SqlCommand sqlCmd;
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.AutoPostBack = false;
sqlCn = new SqlConnection("server=.;database=votes;uid=sa;pwd=");
sqlCn.Open();

SqlDataAdapter sqlDa = new SqlDataAdapter("select *From voteItem", sqlCn);
DataSet ds = new DataSet();
sqlDa.Fill(ds);

DropDownList1.DataTextField = "voteItem";
Dr

在Page_Load里加入if (!IsPostBack){}把你的加载dropdownlist1的代码加入到里面。

页面加载完以后,有没有在DDL里面显示出来数据?

请问为什么没有办法取出DropDownList1.SelectedValue的值

你赋上值了吗?

如果赋值了 没取得值
你把绑定的语句写在if(! ispostback){这里面试试}

你把绑定的代码改改:
DropDownList1.DataSource = ds.ds.Tables[0].DefaultView;
DropDownList1.DataTextField = ds.Tables[0].Columns["voteItem"].ToString();
DropDownList1.DataValueField = ds.Tables[0].Columns["voteID"].ToString();
DropDownList1.DataBind();

Button1.Text = DropDownList1.SelectedValue.tostring();

这样就可以了!

如果有值
this.ddl.selectValues.tostring();
如果是整型的 自己转化int.Parse(this.ddl.selectValues.tostring());