简体   繁体   English

如何使用asp.net中的RegisterForEventValidation注册数据以进行验证

[英]How to register data for validation using RegisterForEventValidation in asp.net

I would like to know the method of using RegisterForEventValidation in asp.net 我想知道在asp.net中使用RegisterForEventValidation的方法

My problem is this. 我的问题是这个。

If I enable eventvalidation, then changing the controls using javascript and then posting the information back to the server later on throws up an error. 如果我启用事件验证,然后使用javascript更改控件,然后将信息发送回服务器,则会引发错误。

But If I disable event validation, the data present/selected in the controls is not available in the event handlers in code behind. 但是,如果禁用事件验证,则控件中存在/选择的数据在代码后面的事件处理程序中不可用。

So, how should one resolve such issues? 那么,如何解决这些问题呢?

Also, are there are any good articles that explain the issue and a resolution in detail? 此外,是否有任何好文章能够详细解释这个问题和解决方案? Tried googling. 尝试使用谷歌搜索。 Came across many articles. 遇到过很多文章。 But nothing that matched my expectations. 但没有任何符合我的期望。

A small progress. 进步很小。

If I do the below, the event validation error goes away, but am not able to get the selected value in the code behind on button click (after selecting "1" in the dropdown, since for now I have registered only that value) 如果我执行以下操作,则事件验证错误消失,但是无法在按钮单击后面的代码中获取所选值(在下拉列表中选择“1”之后,因为现在我只注册了该值)

I get a runtime error - Unable to convert from string to double when I try accessing the selected value in drop down in code behind (The reason I believe is that no value is passed in the first place). 我得到一个运行时错误 - 当我尝试访问后面的代码中的下拉列表中的选定值时,无法从字符串转换为双精度(我相信首先没有传递任何值的原因)。

Any idea on what might be going wrong here!? 关于这里可能出现什么问题的任何想法!? Thanks! 谢谢!

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    Page.ClientScript.RegisterForEventValidation(Me.ddldobddId.UniqueID, "1")
    MyBase.Render(writer)
End Sub

Protected Sub btnId_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnId.Click
    If CType(Me.ddldobddId.SelectedValue, Integer) = 0 -> Throws the error
End Sub

You've pretty much got it. 你已经得到了它。 The DropDownList.SelectedValue is always going to be a System.String type. DropDownList.SelectedValue始终是System.String类型。

You must convert the String to an Integer in your btnId_Click method. 您必须在btnId_Click方法中将String转换为Integer。

Protected Sub btnId_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnId.Click
 Dim convertedSelectedValue As Integer
 convertedSelectedValue = Convert.ToInt32( Me.ddldobddId.SelectedValue )
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM