Last post Jun 02, 2010 06:12 AM by spoida
Apr 23, 2010 04:18 PM|sah302|LINK
I've got a button inside the gridview that isn't firing the onCommand, ONLY when I uncomment some seemingly unrelated code having to do with session in the page load.
<asp:UpdatePanel ID="upAssociatedFaculty" runat="server"
<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" OnClientClick="incrementCounter();" /></p>
<asp:GridView ID="gvAssociatedUsers" runat="server" AutoGenerateColumns="false"
<asp:BoundField DataField="Id" HeaderText="Id" Visible="False" />
<span style="margin-left: 15px;">
<asp:Button ID="btnUnassignUser" runat="server" CausesValidation="false"
CommandArgument='<%# Eval("Id") %>' CommandName="Delete" OnCommand="deleteUser" Text='Remove' /></p>
<em>There are currently no faculty associated with this accomplishment.</em>
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()
Page.Title = "Add a New Faculty Accomplishment"
ddlAccomplishmentType.DataSource = accomplishmentTypeDao.getEntireTable()
ddlAccomplishmentType.DataTextField = "Name"
ddlAccomplishmentType.DataValueField = "Id"
facultyList = userDao.getListOfUsersByUserGroupName("Faculty")
For Each faculty As User In facultyList
facultyDictionary.Add(faculty.Id, faculty.LastName & ", " & faculty.FirstName)
If Not Page.IsPostBack Then
ddlFacultyList.DataSource = facultyDictionary
ddlFacultyList.DataTextField = "Value"
ddlFacultyList.DataValueField = "Key"
gvAssociatedUsers.DataSource = associatedFaculty
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()
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))
Now here is the odd thing. If I comment out the If Not Session block in the page_load....it works and deleteUser will fire upon a button click. If I leave it uncommented as now.....it won't work.
Anyone have a clue??
The weird part is when I step through the code in debug, upon clicking the remove button, it still registers that as being clicked and runs page_load, etc again. But won't run deleteUser.
I am confused.
Apr 23, 2010 10:19 PM|MetalAsp.Net|LINK
I suspect the event is triggering, but maybe associatedFacultyArray is empty or something like that because you have the check in page_load on the session, and you only set the associatedFacultyArray value if the if condition is true. Put a breakpoint in
the deleteuser method to confirm.
Apr 24, 2010 05:25 AM|venkatu2005|LINK
Write the code on Gridview_RowCommand Event
check like this
if( e.CommandName == "Delete")
// delete the record
Jun 02, 2010 06:12 AM|spoida|LINK
page was ALWAYS posting back when I wanted it to (so I KNEW the hidden button's click method had been activated) but the corresponding server-side event wouldn't always get raised. And, like the OP, if I commented out a particular line in my code to do with
the Session the server-side events always seemed to fire??? Very confusing...
To cut a long story short, in my case it turns out that I was also dynamically generating a control (a Table) from Session information and adding it to a placeholder on the page. And, here's the kicker...if I didn't give the Table an ID (so, the ASP.NET
engine auto-generated one for me) then the button click event would not fire consistently. However, if I programmatically assigned an ID to my Table (so that it was the same ID across post-backs) then the button click event always fired correctly.
So, my solution is to check all of your controls and make sure the important ones (especially those that will have postback data associated with them) have an ID that is consistent across page post-backs.
This may not solve your problem , but it's worth looking in to
Incidentally, if someone knows why events are not triggered if a page can't match up its post-back data I would be very happy to learn something.