Hello,
I have 2 webpages: Default.aspx and Next.aspx
Default page has a content with 2 objects: dropdownlist box and button
Next page has a content with 1 button object (link to default page)
The ideas: When I visit the 1st page (Default), it shows a list of 4 items. If I click on button object, it will delete 1 selected item and go to 2nd page( Next.aspx ). From here, I click on button, it links back to 1 st page.I got an emty list after I visit it back.
How can I have:
The 1st page should be displayed only 3 items remaining on its lists, if I select 1 item on the list, it will be deleted and link to 3rd page(test.aspx).
Next visit, it will display 2 until it's done (empty list)
Here are the codes: (Please help. Thank you so much)
1stpage content:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></asp:Content>
C# for 1st page:
namespace DropdownListSession
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["ListItems"] == null)
{
DataTable dt = DropDownItems();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind(); Session["ListItems"] = dt;
}
}
}
private DataTable DropDownItems()
{
DataTable dt = new DataTable();
dt.Columns.Add("Text");
dt.Columns.Add("Value");
dt.Rows.Add("Item1", "1");
dt.Rows.Add("Item2", "2");
dt.Rows.Add("Item3", "3");
dt.Rows.Add("Item4", "4");
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = Session["ListItems"] as DataTable;
ListItemCollection liCol = DropDownList1.Items;
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem li = liCol[i]; if (li.Selected)
{
Response.Redirect("Next.aspx"); dt.Rows[i].Delete();
}
}
Session["ListItem"] = dt;
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
}
}
}
2nd page content:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></asp:Content>
C# for 2nd page:
namespace DropdownListSession
{
public partial class Next : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
}
}