0

我是 asp.net 和 c# 的新手。我正在尝试设置下拉列表框中的文本以显示当前页面标题,但我无法让它工作。有人可以根据下面的代码建议如何做吗?谢谢!

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

4 回答 4

2

尝试这个

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

或者

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Redirect(DropDownList1.SelectedValue + ".aspx");
}
于 2013-07-24T19:15:49.970 回答
0

您不需要拆分和连接字符串。相反,您可以将单个 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);
}
于 2013-07-24T19:39:43.607 回答
0

I'm not sure about ASP.NET, but in regular C#, I think you can try something like this:

DropDownList1.Items.Add(this.Page.Title);

Thanks to Cubicle.Jockey for helping me with the code.

于 2013-07-24T19:14:44.143 回答
0

尝试这个:

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");
        }
    }
于 2013-07-24T21:49:22.630 回答