ASP.NET 列表框选择项更改文本框文本

ASP.NET listbox selected item change textbox text

所以我得到了一个列表框,里面有 5 个不同的人。当我点击列表中的第一个人时,文本框中会出现一个文本。如果我点击另一个人,我们会在文本框中得到另一个文本等。

这是我的清单:

private List<string> personsList = new List<string>();

personsList.Add("Person1");
personsList.Add("Person2");
personsList.Add("Person3");
personsList.Add("Person4");
personsList.Add("Person5");

ListBoxPersons.DataSource = personsList;
ListBoxPersons.DataBind();

因此,如果我单击 Person1,我们会在文本框中获得文本 "Andersson"。如果我们点击 Person2,我们会在文本框中得到文本 "Smith"。

我试过这个:

foreach (ListItem item in ListBoxPersons.Items)
{
    if (item.Text == "Person1")
    {
        TextBoxPersons.Text = "Andersson";
    }
    else if (item.Text == "Person2")
    {
        TextBoxPersons.Text = "Smith";
    }
}

等等,但没有用。我已经尝试了很多其他方法来做到这一点,但遗憾的是也没有运气。 这可能看起来很傻,但这只是一个练习。

C# 或 JQuery 适合我。提前致谢。

您当前的代码未检查当前 select 编辑的内容,仅检查列表中的每一项。您需要创建一个 SelectedIndexChanged 事件来处理 select 不同的事情。像这样 example on MSDN.

基本上,将事件添加到您的 asp.net ListBox 控件,然后在后面的代码中使用相同的名称创建事件,如下所示:

private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   // Get the currently selected item in the ListBox. 
   string curItem = listBox1.SelectedItem.ToString();

   switch(curItem)
   {
       case "Person1":
           TextBoxPersons.Text = "Andersson";
       break;
       case "Person2":
           TextBoxPersons.Text = "Smith";
       break;
       //Add more cases and perhaps a default...
   }
}

更新

刚看到@Banana 和@PhilipB 的评论。如前所述,您需要确保在 Page_Load 事件中将列表框初始化包装在 if(!IsPostback) 中,以确保您不会丢失已经 select 编辑了一个项目的事实。

上面@sr28建议的代码我已经试过了,效果很好。 这是观点:

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true" 
                    onselectedindexchanged="ListBox1_SelectedIndexChanged">   </asp:ListBox>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>`

on pageload bind the listbox:
           `if (!IsPostBack)
          {
            personsList.Add("Person1");
            personsList.Add("Person2");
            personsList.Add("Person3");
            personsList.Add("Person4");
            personsList.Add("Person5");
            ListBox1.DataSource = personsList;
            ListBox1.DataBind();   
}`

这在 onSelectedIndexChanged 上:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
        string curItem = ListBox1.SelectedItem.ToString();
        switch (curItem)
        {
            case "Person1":
                TextBox1.Text = "Andersson";
                break;
            case "Person2":
                TextBox1.Text = "Smith";
                break;
        }
  }

代码隐藏

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Dictionary<string, string> personsList = new Dictionary<string, string>();

            personsList.Add("Andersson", "Person1");
            personsList.Add("Smith", "Person2");
            personsList.Add("Name 3", "Person3");
            personsList.Add("Name 4", "Person4");
            personsList.Add("Name 5", "Person5");

            ListBoxPersons.DataTextField = "Value";
            ListBoxPersons.DataValueField = "Key";
            ListBoxPersons.DataSource = personsList;
            ListBoxPersons.DataBind();
        }
    }

    protected void ListBoxPersons_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = ListBoxPersons.SelectedValue;
    }

    <asp:ListBox ID="ListBoxPersons" runat="server" OnSelectedIndexChanged="ListBoxPersons_SelectedIndexChanged" AutoPostBack="true"></asp:ListBox>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>