No this isn't a copy of this question: http://stackoverflow.com/questions/654423/button-in-update-panel-is-doing-a-full-postback
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.
Code Behind:
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 & ", " & 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
Markup:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="upAssociatedFaculty" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<p><b>Created By:</b> <asp:Label ID="lblCreatedBy" runat="server"></asp:Label></p>
<p><b>Accomplishment Type: </b><asp:DropDownList ID="ddlAccomplishmentType" runat="server"></asp:DropDownList></p>
<p><b>Accomplishment Applies To: </b><asp:DropDownList ID="ddlFacultyList" runat="server"></asp:DropDownList>
<asp:Button ID="btnAddUser" runat="server" Text="Add Faculty" /></p>
<p>
<asp:GridView ID="gvAssociatedUsers" runat="server" AutoGenerateColumns="false"
GridLines="None" ShowHeader="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" Visible="False" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<span style="margin-left: 15px;">
<p><%#Eval("LastName")%>, <%#Eval("FirstName")%>
<asp:Button ID="btnUnassignUser" runat="server" CausesValidation="false"
CommandArgument='<%# Eval("Id") %>' CommandName="Delete" OnCommand="deleteUser" Text='Remove' /></p>
</span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<em>There are currently no faculty associated with this accomplishment.</em>
</EmptyDataTemplate>
</asp:GridView>
</p>
</ContentTemplate>
</asp:UpdatePanel>
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.
I tried putting the gvAssociatedUser.Datasource = associatedFaculty
and the line below inside the Page.IsPostBack
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.
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?
Edited: 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.
The issue I am having is everytime I click btnAddUser
it will add one element to the associatedFaculty
list and thus bound to gvAssociatedusers
. 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?