列表清除每个按钮在更新面板内单击?

时间:2010-04-22 17:39:21

标签: .net vb.net button updatepanel postback

这不是这个问题的副本: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>
            &nbsp;<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。这是第一次工作,但第二次单击它时,它会覆盖第一个元素。因此,每次单击按钮时,我的关联的教师列表都会重置?

2 个答案:

答案 0 :(得分:2)

Welp我找到了答案:

即使在部分回发中,整个页面类也会被销毁并重新创建,因此所有全局变量都会被再次调用,因此New()变量将为空,因此只能保存一个项目。我认为这是错误的,因为那里的UpdatePanel内容与该数据有关,并且会通过回发持续存在。

除非存储在ViewState或Session中,否则任何内容都不会通过回发持续存在。

答案 1 :(得分:1)

你是否肯定会做一个“全面”回发?

你提到的帖子有一个非常简单的测试方法..

<script type="text/javascript">
   var count=0;
    function incrementCounter()
    {
        count ++;
        alert(count);
    }
</script>

并在您的asp按钮上添加此

OnClientClick="incrementCounter();"

如果您完整回复提醒,则应始终显示“1”...

如果您的值正在递增,那么您将获得预期的ajax功能。