简体   繁体   中英

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

I would like to know the method of using RegisterForEventValidation in asp.net

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.

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)

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.

You must convert the String to an Integer in your btnId_Click method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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