How to register validation data with 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 sending the information to the server later, you will get an error.

But if I turn off event checking, the data present / selected in the controls is not available in the event handlers in the code.

So how to solve such problems?

Also, are there any good articles that explain the problem and solution in detail? I tried to search. Found many articles. But nothing was in line with my expectations.

Little progress.

If I do the following, the event verification error will disappear, but I won’t be able to get the selected value in the code behind when I click the button (after selecting β€œ1” in the drop-down list, since at the moment I registered only this value)

I get a runtime error. It is not possible to convert from a string to a double when I try to access the selected value in the drop-down list of code (the reason why I believe that the value is not passed in the first place).

Any idea what might be wrong here !? Thank!

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
+3
source share
1 answer

You pretty much got it. DropDownList.SelectedValue will always be a type of System.String.

String Integer btnId_Click.

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
0

Source: https://habr.com/ru/post/1749454/


All Articles