Disable Validation For Dynamically Created Control

Discussion in 'ASP .Net' started by Steve Harclerode, Jun 25, 2009.

  1. This is a standard .Net question, although my starting point is a Commerce
    Server 2007 application that I need to modify.

    I have a page that includes 2 user controls: a LoginView and a custom
    control that is generated dynamically which shows the line items in a
    shopping cart. The client does not need to be logged in to see the shopping
    cart.

    My client has asked me to put a trash can button next to each line item that
    can be used to delete that line item. I have everything hooked up and
    working, *except* that the postback from the trash button causes validation
    for the login form. This means that the validation controls
    (RequiredFieldValidator controls) display their error messages after I click
    the trash button.

    Is there some way to "abort" processing of the validation? It looks as
    though the validation is happening on the server side, since I am able to
    use the debugger to step into my handler function after hitting the trash
    button.

    Here's the code for a text field plus the associate validator:
    <asp:TextBox ID="UserName" Width="155" runat="server"
    CssClass="estoretextbox" ValidationGroup="Login" />
    <asp:RequiredFieldValidator ID="reqPassword"
    ControlToValidate="Password" ErrorMessage="Please enter Password"
    runat="server" Display="Dynamic"
    ValidationGroup="Login"></asp:RequiredFieldValidator>

    And here's C# code that I use to create the trash button:

    ImageButton btnTrash = new ImageButton();
    btnTrash.ID =
    this.GetUniqueImageControlName(lineItem.LineItemId.ToString());
    btnTrash.ImageUrl = "images/trashcan.jpg";
    btnTrash.Height = new Unit(15);
    btnTrash.Width = new Unit(10);
    // Some things I have tried:
    //btnTrash.ValidationGroup = "dummy";
    //btnTrash.Attributes.Add("onclick", "Page_ValidationActive=false");
    //btnTrash.CausesValidation = false;
    btnTrash.RenderControl(writer);
    btnTrash.ValidationGroup = "dummy";
    btnTrash.Attributes.Add("OnServerClick", "TrashButton_Click");


    --
    My BizTalk blog:
    http://stevestechnotes.blogspot.com/
    Steve Harclerode, Jun 25, 2009
    #1
    1. Advertising

  2. On my first attempt at this newsgroup post, I cut & pasted some code in the
    wrong order. The corrected code is below.
    ----------------------

    This is a standard .Net question, although my starting point is a Commerce
    Server 2007 application that I need to modify.

    I have a page that includes 2 user controls: a LoginView and a custom
    control that is generated dynamically which shows the line items in a
    shopping cart. The client does not need to be logged in to see the shopping
    cart.

    My client has asked me to put a trash can button next to each line item that
    can be used to delete that line item. I have everything hooked up and
    working, *except* that the postback from the trash button causes validation
    for the login form. This means that the validation controls
    (RequiredFieldValidator controls) display their error messages after I click
    the trash button.

    Is there some way to "abort" processing of the validation? It looks as
    though the validation is happening on the server side, since I am able to
    use the debugger to step into my handler function after hitting the trash
    button.

    Here's the code for a text field plus the associate validator:
    <asp:TextBox ID="UserName" Width="155" runat="server"
    CssClass="estoretextbox" ValidationGroup="Login" />
    <asp:RequiredFieldValidator ID="reqPassword"
    ControlToValidate="Password" ErrorMessage="Please enter Password"
    runat="server" Display="Dynamic"
    ValidationGroup="Login"></asp:RequiredFieldValidator>

    And here's C# code that I use to create the trash button:

    ImageButton btnTrash = new ImageButton();
    btnTrash.ID =
    this.GetUniqueImageControlName(lineItem.LineItemId.ToString());
    btnTrash.ImageUrl = "images/trashcan.jpg";
    btnTrash.Height = new Unit(15);
    btnTrash.Width = new Unit(10);
    // Some things I have tried:
    //btnTrash.ValidationGroup = "dummy";
    //btnTrash.Attributes.Add("onclick", "Page_ValidationActive=false");
    //btnTrash.CausesValidation = false;
    btnTrash.Attributes.Add("OnServerClick", "TrashButton_Click");

    btnTrash.RenderControl(writer);


    --
    Steve
    Steve Harclerode, Jun 25, 2009
    #2
    1. Advertising

  3. Steve Harclerode

    Joy Guest

    Hi Steve,

    Looking at your code it seems that you did try setting "CausesValidation"
    Property (of trash button) to false (though i must say it is not a good idea
    if the button in question is not a "Reset" or "Clear" button), so was curious
    to know did it not work for you?

    Can you also paste the code that is executed when a PostBack is triggered?

    regards,
    Joy


    "Steve Harclerode" wrote:

    > On my first attempt at this newsgroup post, I cut & pasted some code in the
    > wrong order. The corrected code is below.
    > ----------------------
    >
    > This is a standard .Net question, although my starting point is a Commerce
    > Server 2007 application that I need to modify.
    >
    > I have a page that includes 2 user controls: a LoginView and a custom
    > control that is generated dynamically which shows the line items in a
    > shopping cart. The client does not need to be logged in to see the shopping
    > cart.
    >
    > My client has asked me to put a trash can button next to each line item that
    > can be used to delete that line item. I have everything hooked up and
    > working, *except* that the postback from the trash button causes validation
    > for the login form. This means that the validation controls
    > (RequiredFieldValidator controls) display their error messages after I click
    > the trash button.
    >
    > Is there some way to "abort" processing of the validation? It looks as
    > though the validation is happening on the server side, since I am able to
    > use the debugger to step into my handler function after hitting the trash
    > button.
    >
    > Here's the code for a text field plus the associate validator:
    > <asp:TextBox ID="UserName" Width="155" runat="server"
    > CssClass="estoretextbox" ValidationGroup="Login" />
    > <asp:RequiredFieldValidator ID="reqPassword"
    > ControlToValidate="Password" ErrorMessage="Please enter Password"
    > runat="server" Display="Dynamic"
    > ValidationGroup="Login"></asp:RequiredFieldValidator>
    >
    > And here's C# code that I use to create the trash button:
    >
    > ImageButton btnTrash = new ImageButton();
    > btnTrash.ID =
    > this.GetUniqueImageControlName(lineItem.LineItemId.ToString());
    > btnTrash.ImageUrl = "images/trashcan.jpg";
    > btnTrash.Height = new Unit(15);
    > btnTrash.Width = new Unit(10);
    > // Some things I have tried:
    > //btnTrash.ValidationGroup = "dummy";
    > //btnTrash.Attributes.Add("onclick", "Page_ValidationActive=false");
    > //btnTrash.CausesValidation = false;
    > btnTrash.Attributes.Add("OnServerClick", "TrashButton_Click");
    >
    > btnTrash.RenderControl(writer);
    >
    >
    > --
    > Steve
    >
    >
    >
    >
    Joy, Jun 25, 2009
    #3
  4. Steve Harclerode

    Joy Guest

    Please also refer to the following Url:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx

    regards,
    Joy

    "Joy" wrote:

    > Hi Steve,
    >
    > Looking at your code it seems that you did try setting "CausesValidation"
    > Property (of trash button) to false (though i must say it is not a good idea
    > if the button in question is not a "Reset" or "Clear" button), so was curious
    > to know did it not work for you?
    >
    > Can you also paste the code that is executed when a PostBack is triggered?
    >
    > regards,
    > Joy
    >
    >
    > "Steve Harclerode" wrote:
    >
    > > On my first attempt at this newsgroup post, I cut & pasted some code in the
    > > wrong order. The corrected code is below.
    > > ----------------------
    > >
    > > This is a standard .Net question, although my starting point is a Commerce
    > > Server 2007 application that I need to modify.
    > >
    > > I have a page that includes 2 user controls: a LoginView and a custom
    > > control that is generated dynamically which shows the line items in a
    > > shopping cart. The client does not need to be logged in to see the shopping
    > > cart.
    > >
    > > My client has asked me to put a trash can button next to each line item that
    > > can be used to delete that line item. I have everything hooked up and
    > > working, *except* that the postback from the trash button causes validation
    > > for the login form. This means that the validation controls
    > > (RequiredFieldValidator controls) display their error messages after I click
    > > the trash button.
    > >
    > > Is there some way to "abort" processing of the validation? It looks as
    > > though the validation is happening on the server side, since I am able to
    > > use the debugger to step into my handler function after hitting the trash
    > > button.
    > >
    > > Here's the code for a text field plus the associate validator:
    > > <asp:TextBox ID="UserName" Width="155" runat="server"
    > > CssClass="estoretextbox" ValidationGroup="Login" />
    > > <asp:RequiredFieldValidator ID="reqPassword"
    > > ControlToValidate="Password" ErrorMessage="Please enter Password"
    > > runat="server" Display="Dynamic"
    > > ValidationGroup="Login"></asp:RequiredFieldValidator>
    > >
    > > And here's C# code that I use to create the trash button:
    > >
    > > ImageButton btnTrash = new ImageButton();
    > > btnTrash.ID =
    > > this.GetUniqueImageControlName(lineItem.LineItemId.ToString());
    > > btnTrash.ImageUrl = "images/trashcan.jpg";
    > > btnTrash.Height = new Unit(15);
    > > btnTrash.Width = new Unit(10);
    > > // Some things I have tried:
    > > //btnTrash.ValidationGroup = "dummy";
    > > //btnTrash.Attributes.Add("onclick", "Page_ValidationActive=false");
    > > //btnTrash.CausesValidation = false;
    > > btnTrash.Attributes.Add("OnServerClick", "TrashButton_Click");
    > >
    > > btnTrash.RenderControl(writer);
    > >
    > >
    > > --
    > > Steve
    > >
    > >
    > >
    > >
    Joy, Jun 25, 2009
    #4
  5. Hi Joy,

    Thanks. Here's the method signature of the callback function (plus a few
    lines of code). I'm not 100% sure how LoadPostData gets wired up, but
    setting breakpoints here in the debugger makes it clear that it is in fact
    being called:
    public bool LoadPostData(string postDataKey, NameValueCollection
    postCollection)
    {
    if (postCollection == null)
    {
    throw new ArgumentNullException("postCollection");
    }

    IEnumerable<LineItem> lineItems = this.Helper.GetAllLineItems();
    // first check to see if trash icon was clicked, and if so set
    the quantity to 0 for the item
    foreach (LineItem lineItem in lineItems)
    {
    string value =
    postCollection[this.GetUniqueImageControlName(lineItem.LineItemId.ToString())
    + ".x"];
    if (!String.IsNullOrEmpty(value))
    {
    lineItem.Quantity = 0;
    }
    }
    ....

    Here's the method that I'm overriding in order to create the WebControl
    contents. It occurs to me the rendering might be happening after the
    validation is done for the control that contains the LoginView. Not sure
    what I can do about that, without re-writing the entire WebControl to be a
    bit less dynamic:
    protected override void RenderContents(HtmlTextWriter writer)
    {
    // rendering stuff here
    }

    Here's the class signature for the WebControl:
    public class BasketDetail : WebControl, IPostBackDataHandler,
    IPostBackEventHandler

    "Joy" <> wrote in message
    news:...
    > Hi Steve,
    >
    > Looking at your code it seems that you did try setting "CausesValidation"
    > Property (of trash button) to false (though i must say it is not a good
    > idea
    > if the button in question is not a "Reset" or "Clear" button), so was
    > curious
    > to know did it not work for you?
    >
    > Can you also paste the code that is executed when a PostBack is triggered?
    >
    > regards,
    > Joy
    >
    >
    > "Steve Harclerode" wrote:
    >
    Steve Harclerode, Jun 25, 2009
    #5
  6. Steve Harclerode

    Joy Guest

    Hi Steve,

    Thanks for pasting the code. However i wanted to see if you were calling
    "Validate" method on any of the controls and also wanted to see if you are
    checking for "Page.IsValid" somewhere in your postback method.

    Could you please confirm the following things:

    1. Is Validate methode being called?
    2. Is Page.IsValid being called?
    3. Did setting "CausesValidation=false" for the Trash button help?

    regards,
    Joy

    "Steve Harclerode" wrote:

    > Hi Joy,
    >
    > Thanks. Here's the method signature of the callback function (plus a few
    > lines of code). I'm not 100% sure how LoadPostData gets wired up, but
    > setting breakpoints here in the debugger makes it clear that it is in fact
    > being called:
    > public bool LoadPostData(string postDataKey, NameValueCollection
    > postCollection)
    > {
    > if (postCollection == null)
    > {
    > throw new ArgumentNullException("postCollection");
    > }
    >
    > IEnumerable<LineItem> lineItems = this.Helper.GetAllLineItems();
    > // first check to see if trash icon was clicked, and if so set
    > the quantity to 0 for the item
    > foreach (LineItem lineItem in lineItems)
    > {
    > string value =
    > postCollection[this.GetUniqueImageControlName(lineItem.LineItemId.ToString())
    > + ".x"];
    > if (!String.IsNullOrEmpty(value))
    > {
    > lineItem.Quantity = 0;
    > }
    > }
    > ....
    >
    > Here's the method that I'm overriding in order to create the WebControl
    > contents. It occurs to me the rendering might be happening after the
    > validation is done for the control that contains the LoginView. Not sure
    > what I can do about that, without re-writing the entire WebControl to be a
    > bit less dynamic:
    > protected override void RenderContents(HtmlTextWriter writer)
    > {
    > // rendering stuff here
    > }
    >
    > Here's the class signature for the WebControl:
    > public class BasketDetail : WebControl, IPostBackDataHandler,
    > IPostBackEventHandler
    >
    > "Joy" <> wrote in message
    > news:...
    > > Hi Steve,
    > >
    > > Looking at your code it seems that you did try setting "CausesValidation"
    > > Property (of trash button) to false (though i must say it is not a good
    > > idea
    > > if the button in question is not a "Reset" or "Clear" button), so was
    > > curious
    > > to know did it not work for you?
    > >
    > > Can you also paste the code that is executed when a PostBack is triggered?
    > >
    > > regards,
    > > Joy
    > >
    > >
    > > "Steve Harclerode" wrote:
    > >

    >
    >
    Joy, Jun 25, 2009
    #6
  7. Hi Joy,

    I have put my answers inline below.

    When I can find time, I'll created a stripped down version of the issue and
    post it. I don't want to put too much code into a text newsgroup, most of
    which isn't relevant to the issue.

    regards,
    Steve

    "Joy" <> wrote in message
    news:...
    > 1. Is Validate methode being called?

    No. I checked all controls, the ASPX file, and the .master page.

    > 2. Is Page.IsValid being called?

    No, I checked all the relevant files.

    > 3. Did setting "CausesValidation=false" for the Trash button help?

    Nope. This was what I tried first.
    Steve Harclerode, Jun 25, 2009
    #7
    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. buran
    Replies:
    2
    Views:
    1,798
    buran
    Sep 26, 2003
  2. Tony
    Replies:
    1
    Views:
    856
  3. msimmons
    Replies:
    0
    Views:
    452
    msimmons
    Jul 16, 2009
  4. Anton Sommer

    disable viewstate of dynamically created checkboxes

    Anton Sommer, Sep 29, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    240
    Teemu Keiski
    Oct 5, 2003
  5. buran
    Replies:
    2
    Views:
    173
    buran
    Sep 26, 2003
Loading...

Share This Page