开发者

onCommand on button not firing inside gridView?

开发者 https://www.devze.com 2022-12-28 03:21 出处:网络
This is really driving me crazy. I\'ve got a button inside a gridview to remove that item from the gridview (its datasource is a list). I\'ve got the list being saved to session anytime a change is be

This is really driving me crazy. I've got a button inside a gridview to remove that item from the gridview (its datasource is a list). I've got the list being saved to session anytime a change is being made to it, and on page_load check if that session variable is empty, if not, then set that list to bind to the gridview.

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

    '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

End Class

Markup:

<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>

Now here is the crazy part I am simply boggled by, if I uncomment the If Not Session... block of page_load, then deleteUser will never fire when btnUnassignUser is clicked. If I keep it commented out...it fires no problem, but then of course my list can never have more than one item since I am not loading the saved list from session into the gridview but just a fresh one. But the button click is being registered, because page_load is being stepped through again when I am viewing in debug mode, just deleteUser never fires.

Why is this happening?? And how can I fix it??


You don't want to keep rebinding the grid. You want the grid to have the datasource again, but not to be reset.

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

精彩评论

暂无评论...
验证码 换一张
取 消