user4186531
user4186531

Reputation:

ASP .NET c# Add List<Object> to ListView afer DataBild

Im facing a pretty weird problem today. I created a DropDownList which adds the selected Item to a List. The List will be binded to the ListView.

This is the Dropdown:

<asp:DropDownList ID="ddlChooseNewApplication" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
        <asp:ListItem Selected="True">Bitte wählen</asp:ListItem>
        <asp:ListItem Value="windows">Windows</asp:ListItem>
        <asp:ListItem Value="mail">E-Mail</asp:ListItem>
        <asp:ListItem Value="app1">App1</asp:ListItem>
        <asp:ListItem Value="app2">App2</asp:ListItem>
        <asp:ListItem Value="app3">App3</asp:ListItem>
</asp:DropDownList>

Next, when a Item has been clicked it runs the following code:

//This is a global variable
List<NewApplicationModels> applicationName = new List<NewApplicationModels>(); 

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string value = ddlChooseNewApplication.SelectedValue.ToString();

        applicationName.Add(new NewApplicationModels(){ ApplicationName = value});
        ListViewNewApplications.DataSource = applicationName;
        ListViewNewApplications.DataBind();
    }

It will be added to a List<> which should add the selected application to the ListView which looks like this:

<asp:ListView ID="ListViewNewApplications" runat="server">
    <ItemTemplate>
        <br /><br />
        <%# Eval("ApplicationName") %><br />

        <asp:Label ID="Label3" runat="server" Text="Titel"></asp:Label><br />
        <asp:TextBox ID="tbNewTitle" runat="server"></asp:TextBox><br /><br />

        <asp:Label ID="Label4" runat="server" Text="Beschreibung"></asp:Label><br />
        <asp:TextBox ID="tbNewDescription" runat="server" TextMode="MultiLine"></asp:TextBox><br /><br />
        </div>
    </ItemTemplate>
</asp:ListView>

Adding a single Item to the ListView works. The problem is, if I select a new Item in the DropDown, the current object in the ListView will be overwritten. I want it to create a new Item under the current Item. Where is the mistake?

Many thanks in advance

Upvotes: 0

Views: 898

Answers (1)

Pham X. Bach
Pham X. Bach

Reputation: 5442

Editted: Just read here that static property is per application domain, not per user, So the solution should change to use Session State

//Add using on top of .cs
using System.Linq;

//In cs class
public partial class name_of_class : Page
{
    private List<NewApplicationModels> applicationName = new List<NewApplicationModels>(); 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
             //Do page_load stuff....
             Session.Remove("name_here");  //reset the Session when first page load
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Get List from Session
        applicationName = (List<NewApplicationModels>)Session["name_here"]; 
        if (applicationName == null)
            applicationName = new List<NewApplicationModels>(); 

        string value = ddlChooseNewApplication.SelectedValue.ToString();
        if (!applicationName.Any(item => item.ApplicationName == value))   
        {
            applicationName.Add(new NewApplicationModels(){ ApplicationName = value});
            Session["name_here"] = applicationName;

            ListViewNewApplications.DataSource = applicationName;
            ListViewNewApplications.DataBind();
        }
    }
}

For static property is per application domain, 2 stranger people from 2 different place browse the same page in same server (app domain) will all see it.

That means when userA change dropdownlist 2 times and the listview have 2 items, then userB browse and change dropdownlist, he may get 3 items in listview, 2 of userA and 1 of his recently choose (he may get nothing too).

Upvotes: 1

Related Questions