每次在更新面板中单击按钮时列表都会被清除?

发布于 2024-08-30 06:16:23 字数 4977 浏览 4 评论 0原文

不,这不是这个问题的副本:更新中的按钮面板正在执行完整的回发?

我在更新面板中有一个下拉菜单,我试图让它允许使用该页面的人将用户添加到绑定到 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 时,它都会向 linkedFaculty 列表添加一个元素,从而绑定到 gvAssociatedusers。这第一次有效,但第二次单击它时,它会覆盖第一个元素。所以每次我单击按钮时,我的关联学院列表都会被重置?

No this isn't a copy of this question: 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如日中天 2024-09-06 06:16:23

Welp 我找到了这个问题的答案:

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

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

Welp I found out the answer to this:

Even on partial postbacks, the entire page class is destroyed and re-recreated, so all the global variables get called again, so the New() variables will be empty thus why only one item can be saved. I was wrong in thinking that since it was in an UpdatePanel that stuff in there and related to that data would persist through a postback.

Nothing persists through postbacks unless stored in the ViewState or Session.

拥抱没勇气 2024-09-06 06:16:23

您确定它会进行“完整”回发吗?

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

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

并且在你的asp按钮上添加这个

OnClientClick="incrementCounter();"

如果你做了一个完整的回发,你的警报应该总是显示“1”......

如果你的值正确增加,那么你会得到预期的ajax功能。

Are you positive its doing a "FULL" postback?

The post you mentioned has a pretty easy way of testing..

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

and on your asp button add this

OnClientClick="incrementCounter();"

If your doing a full post back your alert should always show "1"...

If your value is incrementing properly then your getting your expected ajax functionality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文