Note that there are some explanatory texts on larger screens.

plurals
  1. POList getting cleared every button click inside Update Panel?
    text
    copied!<p>No this isn't a copy of this question: <a href="https://stackoverflow.com/questions/654423/button-in-update-panel-is-doing-a-full-postback">Button in update panel is doing a full postback?</a></p> <p>I've got a drop down inside an update panel, and I am trying to get it to allow the person using the page to add users to a list that is bound to a gridview. The list is a global variable, and on page_load I set that to the gridview's datasource and databind it. However, anytime I click the 'add a user' button, or the button to remove the user from the list. It appears like it is doing a full post back even though all these elements are inside the update Panel.</p> <p>Code Behind:</p> <pre><code>Public accomplishmentTypeDao As New AccomplishmentTypeDao() Public accomplishmentDao As New AccomplishmentDao() Public userDao As New UserDao() Public facultyDictionary As New Dictionary(Of Guid, String) Public facultyList As New List(Of User) Public associatedFaculty As New List(Of User) Public facultyId As New Guid Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Page.Title = "Add a New Faculty Accomplishment" ddlAccomplishmentType.DataSource = accomplishmentTypeDao.getEntireTable() ddlAccomplishmentType.DataTextField = "Name" ddlAccomplishmentType.DataValueField = "Id" ddlAccomplishmentType.DataBind() facultyList = userDao.getListOfUsersByUserGroupName("Faculty") For Each faculty As User In facultyList facultyDictionary.Add(faculty.Id, faculty.LastName &amp; ", " &amp; faculty.FirstName) Next If Not Page.IsPostBack Then ddlFacultyList.DataSource = facultyDictionary ddlFacultyList.DataTextField = "Value" ddlFacultyList.DataValueField = "Key" ddlFacultyList.DataBind() End If gvAssociatedUsers.DataSource = associatedFaculty gvAssociatedUsers.DataBind() End Sub Protected Sub deleteUser(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) facultyId = New Guid(e.CommandArgument.ToString()) associatedFaculty.Remove(associatedFaculty.Find(Function(user) user.Id = facultyId)) gvAssociatedUsers.DataBind() upAssociatedFaculty.Update() End Sub Protected Sub btnAddUser_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddUser.Click facultyId = New Guid(ddlFacultyList.SelectedValue) associatedFaculty.Add(facultyList.Find(Function(user) user.Id = facultyId)) gvAssociatedUsers.DataBind() upAssociatedFaculty.Update() End Sub </code></pre> <p>Markup:</p> <pre><code>&lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt; &lt;/asp:ScriptManager&gt; &lt;asp:UpdatePanel ID="upAssociatedFaculty" runat="server" UpdateMode="Conditional"&gt; &lt;ContentTemplate&gt; &lt;p&gt;&lt;b&gt;Created By:&lt;/b&gt; &lt;asp:Label ID="lblCreatedBy" runat="server"&gt;&lt;/asp:Label&gt;&lt;/p&gt; &lt;p&gt;&lt;b&gt;Accomplishment Type: &lt;/b&gt;&lt;asp:DropDownList ID="ddlAccomplishmentType" runat="server"&gt;&lt;/asp:DropDownList&gt;&lt;/p&gt; &lt;p&gt;&lt;b&gt;Accomplishment Applies To: &lt;/b&gt;&lt;asp:DropDownList ID="ddlFacultyList" runat="server"&gt;&lt;/asp:DropDownList&gt; &amp;nbsp;&lt;asp:Button ID="btnAddUser" runat="server" Text="Add Faculty" /&gt;&lt;/p&gt; &lt;p&gt; &lt;asp:GridView ID="gvAssociatedUsers" runat="server" AutoGenerateColumns="false" GridLines="None" ShowHeader="false"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="Id" HeaderText="Id" Visible="False" /&gt; &lt;asp:TemplateField ShowHeader="False"&gt; &lt;ItemTemplate&gt; &lt;span style="margin-left: 15px;"&gt; &lt;p&gt;&lt;%#Eval("LastName")%&gt;, &lt;%#Eval("FirstName")%&gt; &lt;asp:Button ID="btnUnassignUser" runat="server" CausesValidation="false" CommandArgument='&lt;%# Eval("Id") %&gt;' CommandName="Delete" OnCommand="deleteUser" Text='Remove' /&gt;&lt;/p&gt; &lt;/span&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;EmptyDataTemplate&gt; &lt;em&gt;There are currently no faculty associated with this accomplishment.&lt;/em&gt; &lt;/EmptyDataTemplate&gt; &lt;/asp:GridView&gt; &lt;/p&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; </code></pre> <p>Now I thought the point of an update panel was to be able to update things inside of it without doing a full post_back and reloading the page. So if that's the case, why is it calling page_load everytime I click the buttons? I ran this code and debug and I see that even before any of the code associated with button press fires, page_load runs again.</p> <p>I tried putting the <code>gvAssociatedUser.Datasource = associatedFaculty</code> and the line below inside the <code>Page.IsPostBack</code> check, that prevented the page from working. I tried every combination of settings of the update panel for ChildrenAsTriggers and UpdateMode, and none of them worked. </p> <p>I know this is something simple, but all the combinations I've tried won't get it to work. How can I make this thing work?</p> <p><strong>Edited:</strong> It wasn't causing a full postback so I was wrong as to the cause. I thought the page was doing a full post back thus resetting my associatedFaculty list global variable, but it isn't doing a full postback.</p> <p>The issue I am having is everytime I click <code>btnAddUser</code> it will add one element to the <code>associatedFaculty</code> list and thus bound to <code>gvAssociatedusers</code>. This works the first time, but the second time I click it, it overwrites the first element. So it appears like my associatedFaculty list is getting reset each time I click the button?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload