onCommand on button不在gridView中触发?

时间:2010-04-23 15:46:33

标签: .net vb.net session gridview button

这真让我疯狂。我在gridview中有一个按钮,用于从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

    'If Not Session("associatedFaculty") Is Nothing Then'
    '   Dim associatedFacultyArray As User() = DirectCast(Session("associatedFaculty"), User())'
    '   associatedFaculty = associatedFacultyArray.ToList()'
    'End If'

    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))
    Session("associatedFaculty") = associatedFaculty.ToArray()
    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))
    Session.Add("associatedFaculty", associatedFaculty.ToArray())
    gvAssociatedUsers.DataBind()
    upAssociatedFaculty.Update()
End Sub

Protected Sub Delete(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)

End Sub

结束班

标记:

<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" OnClientClick="incrementCounter();" /></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>

现在这里是我疯狂的部分,如果我取消注释page_load的If Not Session...块,那么点击deleteUserbtnUnassignUser永远不会触发。如果我保持它被注释掉...它没有问题,但当然我的列表永远不会有多个项目,因为我没有将会话中保存的列表加载到gridview中,而只是一个新的。但是正在注册按钮单击,因为当我在调试模式下查看时,page_load正在再次执行,只是deleteUser永远不会触发。

为什么会这样?我该如何解决?

1 个答案:

答案 0 :(得分:1)

您不想继续重新绑定网格。您希望网格再次拥有数据源,但不能重置。

   gvAssociatedUsers.DataSource = associatedFaculty
   if (!Page.IsPostBack)
       gvAssociatedUsers.DataBind()