Response.Redirect 不执行任何操作 (ASP.NET),如果启用,EventValidation 将返回错误

发布于 2024-10-20 19:56:09 字数 1465 浏览 3 评论 0原文

我有一个页面,可以在其中从一系列动态生成的按钮中进行选择。 ASPX 代码如下:

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

当页面加载时,以下代码用于生成按钮:

        ' 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()

当用户单击动态生成的按钮时,将触发以下代码:

    Public Sub Choose_Hall(ByVal Sender As Object, ByVal e As RepeaterCommandEventArgs)
    Session("Hall") = e.CommandName.ToString
    Response.Redirect("select_room.aspx")
    End Sub

当我第一次尝试运行该代码时,我收到一条错误消息“无效的回发/回调参数”。我将 ASPX 页面设置为具有 enableEventValidation="false" 属性,并尝试再次运行它。它生成的页面很好,但是当我单击动态生成的按钮时,它的行为就好像它正在加载某些内容,然后将我带回 select_hall.aspx (所有这些代码的页面都是一个),当(如您在上面看到的)它应该带我到 select_room.aspx。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2024-10-27 19:56:09

听起来 Container.DataItem 不是您认为的类型。请尝试使用 CommandName="<%# Container.DataItem.ToString() %>",然后单步执行调试器并查看发回的 CommandName。我的感觉是,它不是一个字符串,而是某种对象,但仅从 LINQ 查询中很难判断。

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.

巡山小妖精 2024-10-27 19:56:09

所以,看来我已经弄清楚了这个问题。在 select_hall.aspx 的 Page_Load 部分中,我调用了数据库。每次我调用该页面时,它都会重新加载这些值 - 而且我认为它们在某种程度上与以前缓存的属性不匹配(例如底层 ASP.NET 自动命名),这导致了问题。我添加了一个 If Not Page.IsPostBack Then 子句,在其中放置了数据绑定代码,现在它似乎可以正常工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文