Reputation: 57
I'm new to asp.net and c#. I'm trying to set the text in the dropdownlist box to display the current page title, but I cannot get it to work. Can someone advise as to how to do it based on the code below? Thanks!
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");
}
Upvotes: 0
Views: 2555
Reputation: 7943
Try this:
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");
}
}
Upvotes: 0
Reputation: 813
Try this
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect(DropDownList1.SelectedItem.Text + ".aspx");
}
Or
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect(DropDownList1.SelectedValue + ".aspx");
}
Upvotes: 2
Reputation: 62260
You do not need to split and join strings. Instead you can add individual ListItem to 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);
}
Upvotes: 0
Reputation: 2114
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.
Upvotes: 0