Sample usage of ClientScriptManager.RegisterForEventValidation

N

n33470

Hi all,

I have a dropDownList on a web page that is populated by client-side
script. I get an error on PostBack of the page. I've seen quite a few
posts about this, so I understand the issue, and why it's occurring. I
know that I need to remove the automatic event validation that ASP.NET
is performing on the control. One of my options is to use the page
directive enableEventValidation="false". I'd rather not do this for
the entire page, I'd rather remove the event validation for only the
dropdownlist.

I've read that ClientScriptManager.RegisterForEventValidation can be
used for this. However, I haven't been able to figure out exactly how
to use it. I have overloaded the Render() method like this (the
object cboLookup is the DropDownList):

protected override void Render(System.Web.UI.HtmlTextWriter
writer)
{
base.Render(writer);


this.Page.ClientScript.RegisterForEventValidation(cboLookup.ID);

}

However, after doing this I still have the problem when the page is
posted back. I think I'm missing an extra step here, but I can't
figure out what it is. The method name is called
"RegisterForEventValidation", however I actually want to un-register
the control from ASP.NET event validation. How do you do this?

Can someone provide a simple example of how to bypass the automatic
event validation code on postback of the page for a single control.

--steve
 
G

Guest

I added EnableEventValidation="false" to my @Page directive, and now i can
submit the form with the client-side populated listbox. In my Protected Sub
btnSave_Click event, when I iterate the listbox that was populated
client-side, I cannot see any items in the list. Any ideas?
 
N

n33470

www,

Here is the same post on a different forum with a few more comments:
http://forums.asp.net/1139510/ShowPost.aspx

With respect to your comment about reading the DDL on postback in your
click handler. The viewstate of the DDL contains the contents of the
DDL when the page was originally rendered. Thus, on post back the
viewstate get applied to the DDL, and when you iterate the list it has
the original contents restored, which doesn't do any good if the list
is client-populated. On postback, the only thing from the DDL that
you can access is the selected item, and that can be found in the
Response.Form variables.

HTH,

--steve
 
Joined
Oct 6, 2006
Messages
1
Reaction score
0
Potential Solution

You should be able to override your render method like this:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
If findGrid.Rows.Count > 0 Then
For Each row As GridViewRow In findGrid.Rows
If row.RowType = DataControlRowType.DataRow Then
row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(findGrid, "Select$" & row.RowIndex, True))
End If
Next
End If

MyBase.Render(writer)
End Sub


It has worked for me.
 
Joined
Nov 16, 2006
Messages
1
Reaction score
0
Thank you!!!!!

notmyself(gal 2:20) said:
You should be able to override your render method like this:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
If findGrid.Rows.Count > 0 Then
For Each row As GridViewRow In findGrid.Rows
If row.RowType = DataControlRowType.DataRow Then
row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(findGrid, "Select$" & row.RowIndex, True))
End If
Next
End If

MyBase.Render(writer)
End Sub


It has worked for me.

This worked BEAUTIFULLY!!!!!
 
Joined
Nov 28, 2007
Messages
1
Reaction score
0
I have the sam problem my dropdownlist does'nt get populated with data from client side
Can you wrote this for dropdownlist cause this is for datagrid
where must i put this function in asp page?

notmyself(gal 2:20) said:
You should be able to override your render method like this:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
If findGrid.Rows.Count > 0 Then
For Each row As GridViewRow In findGrid.Rows
If row.RowType = DataControlRowType.DataRow Then
row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(findGrid, "Select$" & row.RowIndex, True))
End If
Next
End If

MyBase.Render(writer)
End Sub


It has worked for me.
 
Joined
Feb 6, 2009
Messages
1
Reaction score
0
In order to get around this problem, I simply had to add CausesValidation="True" to my <asp:DropDownList> control definition.
 
Joined
Feb 4, 2010
Messages
1
Reaction score
0
One solution is to include all possible dropdownlist options, that are loaded by the javascript at sometime, in the initial (default) load for the page. I do this and then edit the values dynamically at the end of the page load using Page.ClientScript.RegisterStartupScript
 
Joined
May 30, 2010
Messages
1
Reaction score
0
Somewhat Helpful But The Error Still Comes Up

This is a really cool function (Page.ClientScript.GetPostBackEventReference)if works as advertised. I've targeted both the row and the button in the row that generate the error. I checked the page that was generated and it added the dopostback argument to the control just like I wanted (nice). But................ the error still comes up!!!! What am I missing? Here's the code I wrote. it's for ASP.Net 3.5 This thread goes back awhile so I don't know if something things have changed since 2.0......

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
For Each oGrv As GridViewRow In gvOrg.Rows
oBtn(0) = oGrv.FindControl("btnDelete")
If Not oBtn(0) Is Nothing Then
Page.ClientScript.RegisterForEventValidation(oBtn(0).UniqueID, "")
oGrv.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(gvOrg, "Select$" & oGrv.RowIndex, True))
oBtn(0).Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(gvOrg, oBtn(0).UniqueID, True))
End If
Next
MyBase.Render(writer)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top