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.
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
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 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.
I had just the same issue this morning. That is, I had a (hidden) button on a page which was wired up to a code-behind event handler and I was using javascript to fire the click() method for the button to cause a post back. The interesting thing was, the
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.
sah302
Member
5 Points
31 Posts
Button onCommand not firing inside a gridview...because of session code???
Apr 23, 2010 03:18 PM|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.
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> <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>Code Behind:
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 SubNow 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.
button click grid view session asp.net
MetalAsp.Net
All-Star
112231 Points
18268 Posts
Moderator
Re: Button onCommand not firing inside a gridview...because of session code???
Apr 23, 2010 09:19 PM|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.
venkatu2005
All-Star
32487 Points
6742 Posts
Re: Button onCommand not firing inside a gridview...because of session code???
Apr 24, 2010 04:25 AM|LINK
Write the code on Gridview_RowCommand Event
check like this
if( e.CommandName == "Delete") { // delete the record }Thanks.
spoida
Member
4 Points
2 Posts
Re: Button onCommand not firing inside a gridview...because of session code???
Jun 02, 2010 05:12 AM|LINK
I had just the same issue this morning. That is, I had a (hidden) button on a page which was wired up to a code-behind event handler and I was using javascript to fire the click() method for the button to cause a post back. The interesting thing was, the 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.
Cheers,
Tim