ドロップダウンリストから選択された値を取得する方法 C# ASP.NET

私はC#を使用してシンプルなウェブサイトasp.net webformを作成しました。私は次のようにページがロードされたときにドロップダウンリストにデータを表示するコードを持っています:

private void DisplayData()
{
    List<ListItem> items = new List<ListItem>();
    items.Add(new ListItem("User", "1"));
    items.Add(new ListItem("Administrator", "0"));
    DropdownList1.DataSource = items;
    DropdownList1.DataBind();
}

私はページロードでそれを呼び出します:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DisplayData()
    } 
}

btnSubmitでDropdownList1から値を取得しようとします。

protected void btnSubmit_Click(object sender, EventArgs e)
{
    lblResult.Text = DropdownList1.SelectedValue;
}

この結果は常に値 User または Administartor 文字列を取得します。DropdownList1の値から0または1の値を取得したいのです。どうすればいいですか?

ソリューション

DropDownList1のDataValueFieldを指定してみてください:

DropdownList1.DataSource = items;
DropdownList1.DataValueField = "Value";
DropdownList1.DataTextField = "Text";
DropdownList1.DataBind();
解説 (0)

1つの方法は、DropDownListSelectedItemプロパティを使用してselectedItem(ListItem)を取得し、ListItem.Valueプロパティを使用してListItemに対応する値を取得することです。

lblResult.Text = DropdownList1.SelectedItem.Value; // Value property Gets or sets the value associated with the ListItem.
解説 (0)
Convert.ToInt32(DropdownList1.selectedItem.value);
解説 (1)