C# 如何使用 DropDownList.text 进行选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10242010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:01:33  来源:igfitidea点击:

how to select with DropDownList.text

c#asp.netc#-4.0

提问by user1292656

I am working on an Asp.NET project and I am trying to set the selected value of a dropdown list with a text property. For example i have i.e an item in the dropdown list with text test. Programmatically can i set it to selecteditemby Text?. I am using the follwing code but is not working.

我正在处理一个 Asp.NET 项目,我正在尝试使用文本属性设置下拉列表的选定值。例如,我在下拉列表中有一个带有文本的项目test。编程我可以将其设置为selecteditem通过Text?我正在使用以下代码但无法正常工作。

protected void Page_Load(object sender, EventArgs e)
{
    string t = "test";
    drpFunction.Text = t; 
}

But is not working. Any suggestions ?

但不工作。有什么建议 ?

采纳答案by ionden

 string t = "test";
 drpFunction.ClearSelection();
 drpFunction.Items.FindByText(t).Selected = true;

回答by Sadaf

This Linkmight help you

链接可能对您有所帮助

public static void SelectText(this DropDownList bob, string text)
{
    try
    {
        if (bob.SelectedIndex >= 0)
            bob.Items[bob.SelectedIndex].Selected = false;
        bob.Items.FindByText(text).Selected = true;
    }
    catch
    {
        throw new GenericDropDownListException("value", text);
    }
}

回答by Gaurav Agrawal

Use this...

用这个...

protected void Page_Load(object sender, EventArgs e)
{
    string t = "test";
    drpFunction.SelectedItem.Text = t;
}

or

或者

protected void Page_Load(object sender, EventArgs e)
{
    string t = "test";
    drpFunction.SelectedItem.Value = t;
}

this is proper way.......

这是正确的方法......

回答by Malice

I think the SelectedValueproperty should do what you need.

我认为SelectedValue属性应该可以满足您的需求。

回答by Mark Macneil Bikeio

This works in Web

这适用于网络

ListItem li=new ListItem(); 

li.Text="Stringxyz";
li.Value="Stringxyz";       // Create object of item first and find its index.

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(li);

This also works fine.

这也很好用。

回答by stephen

Setting the itm.Selected = true; only works if you drp.ClearSelection() first. I prefer the following:

设置 itm.Selected = true; 仅当您先使用 drp.ClearSelection() 时才有效。我更喜欢以下内容:

drpFunction.SelectedValue = drpFunction.Items.FindByText(t).Value;

回答by JimmyBytes

protected void Page_Load(object sender, EventArgs e)
{
    string t = "test";
    drpFunction.SelectedValue = t;
}

The SelectedValue property can be used to select an item in the list control by setting it with the value of the item. However, an exception will be thrown during postback if the the selected value doesn't match the list of values in the dropdown list.

SelectedValue 属性可用于通过设置项目的值来选择列表控件中的项目。但是,如果所选值与下拉列表中的值列表不匹配,则在回发期间将引发异常。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue(v=vs.110).aspx

回答by Ms. Sonia

drpFunction.SelectedValue = drpFunction.Items.FindByText(t).Value;

This is better way to select text. By ioden's way it will show an error

这是选择文本的更好方法。通过ioden的方式它会显示一个错误

"Multiple Items Cannot be selected in DropDownList"

“无法在 DropDownList 中选择多个项目”

回答by Nitin Luhar

string t = "test";
drpFunction.ClearSelection();
drpFunction.Items.FindByText(t).Selected = true;