gpt4 book ai didi

c# - 用户在 GridView.DataBind 调用期间单击会导致 ArgumentException

转载 作者:行者123 更新时间:2023-11-30 20:29:20 25 4
gpt4 key购买 nike

我读过很多关于流行的无效的回发或回调参数错误消息的内容。

如果在网页 GridView 完成数据绑定(bind)之前通过单击任何类型的链接/重定向(在本例中为 ASP LinkBut​​ton,但使用 ASP 按钮时会出现相同的结果)来快速打开网页​​,我会收到此错误.

绑定(bind)的数据非常大,理想情况下,我应该添加分页以使其执行速度更快。但是,除了点击之外,没有任何其他数据被修改,包括任何客户端脚本,因此我不确定为什么会发生此错误。

完整错误:

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

出于安全原因,我不想禁用事件验证,并且我知道大型 DataBind 操作是造成这种情况的原因,但我不知道为什么。

我还尝试禁用网格验证作为一个简单的测试,但这并没有解决问题:

myGrid.ValidateRequestMode = ValidateRequestMode.Disabled

LinkBut​​ton使用PostBackUrl,而是使用Click事件+Response.Redirect(我删除了所有不相关的代码)。

LinkBut​​ton 也是在 DataBind 发生之前创建的。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

Dim application As IApplication

If Not IsPostBack Then

application = Session("App")

If application IsNot Nothing Then

AddLinkButton("Test", "EntityPage", CommandType.PageLink)
ShowData(application)

End If

End If

End Sub

Private Sub AddLinkButton(label As String, commandArgument As String,
Optional command As CommandType = CommandType.Link)

Dim linkBtn As New LinkButton()
linkBtn.Text = label
linkBtn.CssClass = "myLinkBtn"
linkBtn.CommandName = command.ToString
linkBtn.CommandArgument = commandArgument

AddHandler linkBtn.Click, AddressOf LinkButton_Click

panel.Controls.Add(linkBtn)
End Sub


Private Sub LinkButton_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim linkBtn As LinkButton = CType(sender, LinkButton)

Select linkBtn.CommandName

Case "Link" :

Response.Redirect(linkBtn.CommandArgument, False)

' more cases + additional logic that I removed from the example

End Select

End Sub

Friend Sub ShowData(application As IApplication)

Dim entities As List(Of Entity)

If Not IsPostBack Then

entities = application.GetEntities()
myGridView.DataSource = entities
myGridView.DataBind()

End If

End Sub

您认为为什么会发生这种情况?有什么办法可以成功中断数据绑定(bind)吗?

编辑:还尝试使用 Button 而不是 LinkBut​​ton,并使用 UseSubmitBehavior = True,但结果相同。

最佳答案

您走错了路:您的数据及其数量没有任何问题。问题是这样的:

The LinkButton is also created before the DataBind takes place.

这根本不够。您需要在页面生命周期的早期创建动态控件(例如 LinkBut​​ton),因为 ASP.NET 也必须发挥其魔力并为动态控件设置 ViewState。请注意,ViewState 的创建发生在生命周期中早于 Load 事件。 As stated on the MSDNPreInit 事件是创建或重新创建动态控件的正确位置。因此,您需要删除 AddLinkBut​​on 子项并在代码隐藏文件中添加以下内容:

Private linkBtn As LinkButton ' Declare the LinkButton variable at a module level

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreInit
linkBtn = New LinkButton()
linkBtn.Text = "Test"
linkBtn.CssClass = "myLinkBtn"
linkBtn.CommandName = CommandType.PageLink.ToString
linkBtn.CommandArgument = "EntityPage"

AddHandler linkBtn.Click, AddressOf LinkButton_Click
End Sub

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
' During Init all the controls have been created.
' During the PreInit event the "panel" control is unavailable.
panel.Controls.Add(linkBtn)
End Sub

并修改加载处理程序:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim application As IApplication

If Not IsPostBack Then
application = Session("App")
linkBtn.Visible = application IsNot Nothing

If linkBtn.Visible Then
' Remove that next line
' AddLinkButton("Test", "EntityPage", CommandType.PageLink)
ShowData(application)
End If
End If
End Sub

关于c# - 用户在 GridView.DataBind 调用期间单击会导致 ArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46238270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com