【问题标题】:Pass asp.net listbox items to a textbox将 asp.net 列表框项传递给文本框
【发布时间】:2014-08-29 11:14:40
【问题描述】:

我在 Default.aspx.cs 类中,我想将列表框中的项目传递给文本框。列表框中的项目未被选中。所以我会有一个按钮,当我点击它时,我希望列表框中的项目也显示在文本框中。请帮帮我。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    使用 foreach 循环并遍历您的列表框,获取每个项目的文本/值并将其附加到您的文本框。在您的按钮单击事件中,

     protected void Button1_Click(object sender, EventArgs e)
        {
            txtItems.Text = "";
            string result ="";
            foreach (ListItem li in lbItems.Items)
            {
                result += li.Value + ",";
            }
    
            // Remove the trailing comma in the end  
            txtItems.Text = result.Remove(result.LastIndexOf(","),1);
        }
    

    【讨论】:

    • 谢谢您的回复,但代码不起作用,是否有其他解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多