C# 按所选项目设置DropDownList值

C# 按所选项目设置DropDownList值,c#,asp.net,C#,Asp.net,我不熟悉asp.net和c。我试图将dropdownlist框中的文本设置为显示当前页面标题,但无法使其正常工作。有人能根据下面的代码给出建议吗?谢谢 if (!Page.IsPostBack) { string path = @"C:\Websites\TaxMapCS"; DirectoryInfo di = new DirectoryInfo(path); FileSystemInfo[] fi = di.GetFiles("

我不熟悉asp.net和c。我试图将dropdownlist框中的文本设置为显示当前页面标题,但无法使其正常工作。有人能根据下面的代码给出建议吗?谢谢

    if (!Page.IsPostBack)
    {
        string path = @"C:\Websites\TaxMapCS";
        DirectoryInfo di = new DirectoryInfo(path);
        FileSystemInfo[] fi = di.GetFiles("*.aspx");
        var result = string.Join(",", fi.OrderByDescending(f => f.CreationTime).Select(i => i.ToString()).ToArray());

        DropDownList1.DataSource = result.Replace(".aspx", "").Split(',');

        DropDownList1.DataBind();

        DropDownList1.Items.Insert(0, new ListItem("Select Edition", ""));

    }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Redirect(DropDownList1.SelectedItem.Value + ".aspx");
}

我不确定ASP.NET,但在常规C#中,我认为您可以尝试以下内容:

DropDownList1.Items.Add(this.Page.Title);
感谢Cubble.Jockey帮我编写代码。

试试这个

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Redirect(DropDownList1.SelectedItem.Text + ".aspx");
}


您不需要拆分和连接字符串。相反,您可以将单个ListItem添加到DropDownList

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

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string path = @"C:\DevelopmentArchive\TelerikDemo\TelerikDemo";
        DirectoryInfo di = new DirectoryInfo(path);
        FileInfo[] files = di.GetFiles("*.aspx");

        foreach (var file in files.OrderByDescending(f => f.CreationTime))
            DropDownList1.Items.Add(new ListItem(file.Name.Replace(".aspx", ""), file.Name));

        DropDownList1.Items.Insert(0, new ListItem("Select Edition", ""));
    }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Redirect(DropDownList1.SelectedItem.Value);
}

受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
字符串路径=@“C:\DevelopmentArchive\TelerikDemo\TelerikDemo”;
DirectoryInfo di=新的DirectoryInfo(路径);
FileInfo[]files=di.GetFiles(“*.aspx”);
foreach(files.OrderByDescending(f=>f.CreationTime)中的var文件)
DropDownList1.Items.Add(新列表项(file.Name.Replace(“.aspx”,”),file.Name));
DropDownList1.Items.Insert(0,新列表项(“选择版本”);
}
}
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
重定向(DropDownList1.SelectedItem.Value);
}
试试这个:

if (!Page.IsPostBack)
    {
        string path = @"C:\Websites\TaxMapCS";
        DirectoryInfo di = new DirectoryInfo(path);
        FileSystemInfo[] fi = di.GetFiles("*.aspx");
        var result = string.Join(",", fi.OrderByDescending(f => f.CreationTime).Select(i => i.ToString()).ToArray());

        DropDownList1.DataSource = result.Replace(".aspx", "").Split(',');

        DropDownList1.DataBind();

        DropDownList1.Items.Insert(0, new ListItem("Select Edition", ""));
        DropDownList1.Items.Insert(0, new ListItem(Page.Title, ""));

    }
}


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(DropDownList1.SelectedIndex > 0)//do not redirect if 'Selected Edition' is selected
        {
            Response.Redirect(DropDownList1.SelectedItem.Text + ".aspx");
        }
    }

只是旁注。我会使用string.Empty来代替使用“”。如果您使用“”而代码中出现其他人,他们不知道这是故意的还是有人忘记输入值。string.Empty表示我要传递一个空字符串。哪个部分不起作用?谢谢你。Jockey,这是一个很好的观点。D Stanley-它总是默认回到列表中的第一项。其他的都有用。我想是这个。页面。标题关于标题。但是我已经有一段时间没有使用ASP.NET aspx样式了。MVC是,如果它在的话。;)谁投了更高的票?ASP.Net DropDownList中没有“选择文本”(SelectedText)这样的东西。你说得对。我的错误。我已按原样编辑了这篇文章。谢谢,但这并不能解决我的问题。@eric为了检索
SelectedItem.Value
,您需要将文本和值分配给
ListItem
。看看我答案中的foreach循环。好吧,我的问题似乎不清楚。除了在下拉列表中显示为选中的当前页面外,其他所有功能都正常工作。SelectedValue确实有效。您的建议不会将下拉列表设置为显示当前项目。谢谢你。
if (!Page.IsPostBack)
    {
        string path = @"C:\Websites\TaxMapCS";
        DirectoryInfo di = new DirectoryInfo(path);
        FileSystemInfo[] fi = di.GetFiles("*.aspx");
        var result = string.Join(",", fi.OrderByDescending(f => f.CreationTime).Select(i => i.ToString()).ToArray());

        DropDownList1.DataSource = result.Replace(".aspx", "").Split(',');

        DropDownList1.DataBind();

        DropDownList1.Items.Insert(0, new ListItem("Select Edition", ""));
        DropDownList1.Items.Insert(0, new ListItem(Page.Title, ""));

    }
}


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(DropDownList1.SelectedIndex > 0)//do not redirect if 'Selected Edition' is selected
        {
            Response.Redirect(DropDownList1.SelectedItem.Text + ".aspx");
        }
    }