Response.Redirect Does Nothing (ASP.NET) and EventValidation returns error if enabled
I have a page in which one chooses from a selection of dynamically generated buttons. The ASPX code is as follows:
<div>
<asp:Repeater ID="rptrHalls" runat="server" OnItemCommand="Choose_Hall">
<ItemTemplate>
<asp:Button ID="btnChooseHall" runat="server"
CommandName="<%# Container.DataItem %>" Text="<%# Container.DataItem %>"
/>
</ItemTemplate>
</asp:Repeater>
</div>
When the page is loaded the following code is used to generate the buttons:
' Show which halls they are eligible for.
Dim dbHalls As New pbu_housingDataContext
'Dim gender As String = Session("gender").ToString
Dim selectedHalls = (From sh In dbHalls.Rooms _
Where sh.gender = Session("gender").ToString _
Where sh.current_occupancy < sh.max_occupancy _
Where sh.is_available = True _
Select sh.building_name).Distinct()
rptrHalls.DataSource = selectedHalls
rptrHalls.DataBind()
When the use开发者_如何学JAVAr clicks on a dynamically generated button the following code is triggered:
Public Sub Choose_Hall(ByVal Sender As Object, ByVal e As RepeaterCommandEventArgs)
Session("Hall") = e.CommandName.ToString
Response.Redirect("select_room.aspx")
End Sub
When I first tried running the code I received an error message of "Invalid postback/callback argument". I set the ASPX page to have a enableEventValidation="false" property and tried running it again. It generates the page fine but when I click on a dynamically generated button it acts as if it is loading something and then just brings me back to select_hall.aspx (the page all this code is one), when (as you can see above) it should take me to select_room.aspx.
It sounds like Container.DataItem
isn't the type you think it is. Try CommandName="<%# Container.DataItem.ToString() %>"
instead, then step through the debugger and look at the CommandName that gets posted back. My feeling is that it isn't a string but some kind of object, but it's hard to tell from your LINQ query alone.
So, it appears I figured out the issue. In the Page_Load section of select_hall.aspx I had a call to the database. Every time I called the page it was reloading these values - and I think they were not matching up to the previously cached properties in some way (e.g. underlying ASP.NET auto-naming), this was causing the issue. I added a If Not Page.IsPostBack Then clause inside which I placed the data binding code and now it appears to work correctly.
精彩评论