Sample usage of ClientScriptManager.RegisterForEventValidation

Discussion in 'ASP .Net' started by n33470, Dec 9, 2005.

  1. n33470

    n33470 Guest

    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
     
    n33470, Dec 9, 2005
    #1
    1. Advertising

  2. I have the same problem. Have you found a solution?

    "n33470" wrote:

    > 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
    >
    >
     
    =?Utf-8?B?d3d3MDUxOTA2?=, Jan 3, 2006
    #2
    1. Advertising

  3. 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?

    "n33470" wrote:

    > 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
    >
    >
     
    =?Utf-8?B?d3d3MDUxOTA2?=, Jan 3, 2006
    #3
  4. n33470

    n33470 Guest

    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


    www051906 wrote:
    > 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?
    >
    > "n33470" wrote:
    >
    > > 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
    > >
    > >
     
    n33470, Jan 3, 2006
    #4
  5. n33470

    notmyself(gal 2:20)

    Joined:
    Oct 6, 2006
    Messages:
    1
    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.
     
    notmyself(gal 2:20), Oct 6, 2006
    #5
  6. n33470

    kevonh

    Joined:
    Nov 16, 2006
    Messages:
    1
    Thank you!!!!!

    This worked BEAUTIFULLY!!!!!
     
    kevonh, Nov 16, 2006
    #6
  7. n33470

    rocky20

    Joined:
    Nov 28, 2007
    Messages:
    1
    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?

     
    rocky20, Nov 28, 2007
    #7
  8. n33470

    kennedyz

    Joined:
    Feb 6, 2009
    Messages:
    1
    In order to get around this problem, I simply had to add CausesValidation="True" to my <asp:DropDownList> control definition.
     
    kennedyz, Feb 6, 2009
    #8
  9. n33470

    DougLynn

    Joined:
    Feb 4, 2010
    Messages:
    1
    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
     
    DougLynn, Feb 4, 2010
    #9
  10. n33470

    AbercrombieLV

    Joined:
    May 30, 2010
    Messages:
    1
    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
     
    AbercrombieLV, May 30, 2010
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5kcsOp?=

    ClientScriptManager.RegisterForEventValidation

    =?Utf-8?B?QW5kcsOp?=, Nov 10, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,911
  2. stewart
    Replies:
    2
    Views:
    14,434
    stewart
    Dec 7, 2005
  3. Varangian
    Replies:
    7
    Views:
    2,857
    Varangian
    Jul 10, 2006
  4. Zac
    Replies:
    4
    Views:
    1,880
  5. DNB
    Replies:
    2
    Views:
    1,818
    Michael Nemtsev
    Dec 13, 2007
Loading...

Share This Page