如何在ASP.NET webforms中获取列表框中所选项的值 [英] How do I get the value of selected item in listbox in ASP.NET webforms

查看:48
本文介绍了如何在ASP.NET webforms中获取列表框中所选项的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的项目中有一个Listbox,如下所示:

Hi ,

I have a Listbox in my project like this:

<asp:ListBox runat="server" ID="MultiSelect" SelectionMode="multiple">
                               
                            </asp:ListBox>










<p>The Selected Value is:</p><asp:TextBox  ID="listBoxValue" runat="server" ></asp:TextBox>





代码落后:





Code behind:

//on page load i fill the the Listbox with values like this:

MultiSelect.Items.Add(new ListItem {Text = "John Doe", Value = "1"});

MultiSelect.Items.Add(new ListItem {Text = "Jane Doe", Value = "2"});
// 
listBoxValue.Text = MultiSelect.SelectedValue // << i know this is wrong but this is where im stuck





我需要获得列表框中选择项的值并显示它。因此,例如当用户从列表框中选择John Doe时,我希望在文本框中填充该值,但我不确定如何操作。



PS我不想点按一下按钮,我想在用户从列表框中选择时完成它

谢谢



我尝试过:



嗯,我一直在看事件监听器,但我不确定哪一个可能是最好的完成这一点,目前我只对按钮点击感到满意但在这种情况下我不需要按钮单击事件监听器



I need to get value of the select item of the listbox and display it. So for example when a user selects "John Doe" from the listbox , I want the value to be filled in the textbox but i'm not sure how to this.

PS I don't want to do it with a button click , i would like it to be done when the user selects from the Listbox
Thank you

What I have tried:

Well, I have been looking at event listeners but i'm not sure which one might be the best to accomplish this, at the moment i'm only comfortable with button click but in this case i dont need the button click event listener

推荐答案

Markip代码



Markip code

<form id="form1" runat="server">
    <div>
    <asp:ListBox runat="server" ID="MultiSelect" AutoPostBack="true" SelectionMode="multiple" OnSelectedIndexChanged="MultiSelect_SelectedIndexChanged">
                               
                            
    </div>
        <div>
            <p>The Selected Value is:</p><asp:TextBox  ID="listBoxValue" runat="server" >
        </div>
    </form>









代码落后





Code behind

public partial class list_show : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MultiSelect.Items.Add(new ListItem { Text = "John Doe", Value = "1" });

                MultiSelect.Items.Add(new ListItem { Text = "Jane Doe", Value = "2" });
                
            } 
        }

        protected void MultiSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBoxValue.Text = MultiSelect.SelectedItem.Text;
        }
    }


这篇关于如何在ASP.NET webforms中获取列表框中所选项的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆