简体   繁体   English

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

[英]ASP.NET listbox selected item change textbox text

So I got a listbox with 5 different persons in it. 因此,我得到了一个列表框,其中包含5个不同的人。 And when I click on the first person in the list a text appears in a textbox. 当我单击列表中的第一个人时,文本将出现在文本框中。 An if I click on another person, we get another text in the textbox etc. 如果我单击另一个人,我们将在文本框中等其他文本。

Here is my list: 这是我的清单:

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();

So, if I click of Person1 we get the text "Andersson" in the textbox. 因此,如果单击Person1,我们将在文本框中获得文本“ Andersson”。 If we click on Person2 we get the text "Smith" in the textbox. 如果单击Person2,则会在文本框中获得文本“ Smith”。

I've tried this: 我已经试过了:

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

And so on, but that didn't work. 依此类推,但这没有用。 I have tried quite a few other ways of doing it, but sadly no luck with that either. 我尝试了许多其他方式来实现此目的,但不幸的是,这也没有运气。 This may seem silly, but it is just an exercise. 这似乎很愚蠢,但这只是一种练习。

C# or JQuery works for me. C#或JQuery对我有用。 Thanks in advance. 提前致谢。

Your current code isn't checking what's currently selected, just each item in the list. 您当前的代码不会检查当前选中的内容,而只是检查列表中的每个项目。 You need to create a SelectedIndexChanged event to handle when you select something different. 您需要创建一个SelectedIndexChanged事件,以在选择其他内容时进行处理。 Something like this example on MSDN . 类似于MSDN上的示例

Basically, add the event to your asp.net ListBox control and then create the event in your code behind with the same name something like this: 基本上,将事件添加到您的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...
   }
}

UPDATE UPDATE

Just saw the comments from @Banana and @PhilipB. 刚刚看到@Banana和@PhilipB的评论。 As mentioned you need to ensure you wrap the listbox initialization in if(!IsPostback) in your Page_Load event to ensure you don't lose the fact you've selected an item. 如前所述,您需要确保将列表框初始化包装在Page_Load事件的if(!IsPostback)中,以确保不会丢失选择项目的事实。

The above code which @sr28 suggested works fine I have tried it. @ sr28建议的上面的代码可以正常工作,我已经尝试过了。 This is the view: 这是视图:

<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();   
}`

And this on the onSelectedIndexChanged: 然后在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;
        }
  }

Code Behind 背后的代码

    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;
    }

Page

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

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

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