1

我在 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>

而不是Selected="true"在 ASP.net 中说我想在

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

更新

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

</asp:DropDownList>

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

}

// 我也复制了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);
}
4

3 回答 3

4

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

你可以这样做,

aspx文件中

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

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

下面的代码是选择项目

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

或者

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

或者

DropDownList1.SelectedItem.Selected = true;
于 2013-01-18T10:25:06.393 回答
0
DropDownList1.SelectedValue = "value";

Or

DropDownList1.SelectedIndex= 1;
于 2013-01-18T10:15:32.583 回答
0

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

if (ddl.Items.FindByValue("value") != null && !string.IsNullOrEmpty("value"))
{
  ddl.Items.FindByValue("value").Selected = true;
}
于 2013-01-18T10:26:04.187 回答