0

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

  • Did you run in debug mode yet? What does List `applicationName` have when you set Datasource for your Listview? Only 1 Item or more? – Pham X. Bach May 15 '16 at 15:39
  • @PhamX.Bach Constantly one Item. Maybe there is is en error with inserting the Item. Any Ideas? –  May 15 '16 at 15:52
  • 1
    Then I dout that List `applicationName` is `a global variable`. You should place a debug flag in it definition line and check if it is reset everytime you change your dropdownlist (= new List<....). Can you confirm where it is declare? A member of codebehind class? – Pham X. Bach May 15 '16 at 15:55
  • @PhamX.Bach You are right! Thank you. But how can I prevent the variable to redefine? Should I cache it? Or what is the best way? –  May 15 '16 at 16:02
  • 1
    You should save the `applicationName` list in a ViewState or a Session variable, so that it persists across postbacks. – ConnorsFan May 15 '16 at 16:04
  • 2
    You can use `static List` so when postback it will not be reset. Anyway, could you check if my code run ok in your case, the line `if (!applicationName.Any(item => item.ApplicationName == value)) ` ? – Pham X. Bach May 15 '16 at 16:14

1 Answers1

1

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).

Community
  • 1
  • 1
Pham X. Bach
  • 5,284
  • 4
  • 28
  • 42
  • Dear @Manuel Bürge, I have just found that my last answer is a `bad` solution. I have editted and change solution with explanation. You should read and change your implementation to use `Session` i think. – Pham X. Bach May 15 '16 at 17:45