简体   繁体   English

如何在下拉列表中获取所选项目的值?

[英]How to get the value of the selected Item in a Drop down list?

I have a drop down list populated from a database. 我有一个从数据库填充的下拉列表。 When i use the dplTags.SelectedItem.Value it only returns the first value and not the one i selected? 当我使用dplTags.SelectedItem.Value时,它仅返回第一个值,而不返回我选择的那个值?

Could Someone tel me where i'm going wrong? 有人可以给我打电话我哪里出错了吗?

When i call it: 当我称之为:

String TagID = dplTags.SelectedItem.Value; // Will only select the first value????

It always returns the TagID of the first item, not the selected one :\\ 它总是返回第一项的TagID,而不是所选项的TagID:\\

This is how i bind it: 这是我绑定它的方式:

using (var conn = new SqlConnection(Properties.Settings.Default.DBConnectionString))
            {
                conn.Open();

                SqlDataAdapter daTags
                = new SqlDataAdapter("Select * From Tag", conn);

                DataSet dsTags = new DataSet("TagCloud");

                daTags.FillSchema(dsTags, SchemaType.Source, "Tag");
                daTags.Fill(dsTags, "Tag");

                daTags.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                daTags.Fill(dsTags, "Tag");

                DataTable tblTag;
                tblTag = dsTags.Tables["Tag"];

                dplTags.DataSource = dsTags;
                dplTags.DataMember = "Tag"; 
                dplTags.DataValueField = "TagID"; //Value Member
                dplTags.DataTextField = "Value"; // Display Member
                dplTags.DataBind();
            }

Please help, Thanks in Advance. 请帮助,在此先感谢。

Where are you binding data to these dropdown list controls? 您将数据绑定到这些下拉列表控件的位置是什么? They should be bound only in the initial loading of the page as follows. 它们应仅在初始加载页面时绑定,如下所示。 I suspect that you are binding them in every page load and therefore selected values disappear. 我怀疑您在每次页面加载时都将它们绑定了,因此所选值消失了。

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        //Please check if you are binding checkbox controls here. 
        //If not bring them in here
    }
}

仅当Page.IsPostback == false时,才启用页面的视图状态,并对下拉列表进行数据绑定

Bind the dropdown list once on first page load and do not bind it again on postback. 在第一页加载时将下拉列表绑定一次,不要在回发时再次绑定它。 Binding it again will fill the dropdown with new elements and previous selection will be lost. 再次绑定它将用新元素填充下拉列表,并且先前的选择将丢失。

if(!Page.IsPostBack)
{
using (var conn = new SqlConnection(Properties.Settings.Default.DBConnectionString))
            {
                conn.Open();

                SqlDataAdapter daTags
                = new SqlDataAdapter("Select * From Tag", conn);

                DataSet dsTags = new DataSet("TagCloud");

                daTags.FillSchema(dsTags, SchemaType.Source, "Tag");
                daTags.Fill(dsTags, "Tag");

                daTags.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                daTags.Fill(dsTags, "Tag");

                DataTable tblTag;
                tblTag = dsTags.Tables["Tag"];

                dplTags.DataSource = dsTags;
                dplTags.DataMember = "Tag"; 
                dplTags.DataValueField = "TagID"; //Value Member
                dplTags.DataTextField = "Value"; // Display Member
                dplTags.DataBind();
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM