不,这不是这个问题的一个副本:Button in update panel is doing a full postback?更新面板内的每个按钮单击清单清单?
我有一个下拉的更新面板内,我试图得到它使用页面将用户添加到列表中允许的人这是绑定到一个gridview。该列表是一个全局变量,在page_load上,我将它设置为gridview的数据源并将其绑定。但是,无论何时我点击“添加用户”按钮,或按钮从列表中删除用户。即使所有这些元素都位于更新面板中,它看起来好像正在完整发布。
代码背后:
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
标记:
<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>
现在我想到了一个更新面板的一点是要能够更新它里面的东西没有做全套post_back和重装页。所以如果是这样的话,为什么每次点击按钮时都会调用page_load?我运行了这段代码并进行了调试,我发现即使在任何与按钮关联的代码触发之前,page_load都会再次运行。
我试图把gvAssociatedUser.Datasource = associatedFaculty
和下面一行放在Page.IsPostBack
检查中,这就阻止了页面的工作。我尝试了更新面板的各种设置的ChildrenAsTriggers和UpdateMode的组合,并没有一个工作。
我知道这是简单的,但我尝试过的所有组合都不会使它工作。我怎样才能使这件事情工作?
被修改:这并没有导致完整的回发,所以我错了原因。我认为这个页面正在做一个完整的回传,因此重新设置了我的associatedFaculty列表全局变量,但是它没有做一个完整的回发。
我遇到的问题是每次点击btnAddUser
它都会在associatedFaculty
列表中添加一个元素,从而绑定到gvAssociatedusers
。这是第一次,但第二次点击它,它会覆盖第一个元素。所以看起来我每次点击按钮时,我的相关学院名单都会重置?
好吧我错了,它没有做一个完整的回发,通过你的方法测试。所以这不是导致问题的原因,我遇到的问题是每次点击'btnAddUser'时,我的相关学院列表都会重置。所以我可以在第一次点击它的时候添加一个人到gridview(并且删除它没有问题),但是当我第二次点击它时,它只是覆盖第一个人,而不是添加到第一个人。更新原文更清晰。 – SventoryMang 2010-04-22 18:45:36