C# DropDownList 将所选项目设置为 true

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14396591/
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-10 11:42:39  来源:igfitidea点击:

DropDownList set selected item to true

c#asp.net

提问by sharon Hwk

I have added DropDownList in ASP.net.

我在 ASP.net 中添加了 DropDownList。

<asp:DropDownList  ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" > 
<asp:ListItem Text="Name" Value="jsh" Selected="true" /> 
<asp:ListItem Text="hhh" Value="sds"/> 
</asp:DropDownList>

Instead on saying Selected="true"in ASP.net i want to set the selected item using C# inside the

而不是Selected="true"在 ASP.net 中说我想在里面使用 C# 设置所选项目

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{
    DropDownList1 // THE SELECTED ITEM NEEDS TO BE SET TO TRUE
    Response.Redirect(Request.RawUrl);
}

UPDATE

更新

<asp:DropDownList  ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" OnTextChanged="DropDownList1_onChanged"> 

</asp:DropDownList>

C#

C#

protected void Page_Load(object sender, EventArgs e)
{

    if(!Page.IsPostBack)
{
    DropDownList1.Items.Add(new ListItem("En", "en-US"));
    DropDownList1.Items.Add(new ListItem("Fr", "fr-FR"));
}

}

// I also copied InitializeCulture()

// 我也复制了 InitializeCulture()

protected override void InitializeCulture() 
{

string Kultur = "en-US"; 
if (Session["lang"] != null)
{


    Kultur = Session["lang"].ToString();
}


this.UICulture = Kultur;
this.Culture = Kultur;
base.InitializeCulture();


}

//

//

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{
DropDownList1.Items.FindByValue(DropDownList1.SelectedValue).Selected = true;
  Response.Redirect(Request.RawUrl);
}

采纳答案by Prasad Kanaparthi

Actually, Your question doesnot make sence. If the user selected it, it is already selected. When you refresh the page the DropDownList1is again loading values. So you the DropDownList1selected item would be lost

其实,你的问题没有意义。如果用户选择了它,则它已经被选中。当您刷新页面时,DropDownList1将再次加载值。所以你DropDownList1选择的项目会丢失

You can do like this,

你可以这样做,

In aspxfile

aspx文件中

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>

In aspx.csfile

aspx.cs文件中

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            DropDownList1.Items.Add(new ListItem("Name", "jsh"));
            DropDownList1.Items.Add(new ListItem("hhh", "sds"));
        }
    }    

Below code is to select the item

下面的代码是选择项目

DropDownList1.Items.FindByValue(DropDownList1.SelectedValue).Selected = true;

or

或者

DropDownList1.Items.FindByText(DropDownList1.SelectedItem.Text).Selected = true;

or

或者

DropDownList1.SelectedItem.Selected = true;

回答by sreejithsdev

DropDownList1.SelectedValue = "value";

Or

DropDownList1.SelectedIndex= 1;

回答by Ashfaq Shaikh

you can also make selection in this way.

您也可以通过这种方式进行选择。

if (ddl.Items.FindByValue("value") != null && !string.IsNullOrEmpty("value"))
{
  ddl.Items.FindByValue("value").Selected = true;
}