当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - Populating a listbox from a DataTable and want the highlighted userID to

问题描述:

populate a few Textbox like first name.

if (dt.Rows.Count > 0) //This is how I populate the Listbox from the DT

{

ListBox1.Items.Clear();

ListBox1.DataSource = dt;

ListBox1.DataValueField = "samAccountName"; //column name in DT

ListBox1.DataBind();

//FirstBox.Text =

}

List box code:

 <asp:ListBox id="ListBox1"

Rows="7"

Width="100px"

SelectionMode="Single"

runat="server">

</asp:ListBox>

So far I tried to populate the textbox fields (like first Name) from the DT but yea when I highlight another userID in the Listbox, the first name field doesn't update.

网友答案:

You should handle the ListBox' SelectedIndexChanged event:

private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   // Get the currently selected item in the ListBox.
   string accName = ListBox1.SelectedItem.ToString();
   TxtAccountName.Text = accName;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: