Home All Groups Group Topic Archive Search About

Disable Validation For Dynamically Created Control

Author
24 Jun 2009 11:16 PM
Steve Harclerode
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");



Author
24 Jun 2009 11:49 PM
Steve Harclerode
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
Are all your drivers up to date? click for free checkup

Author
25 Jun 2009 4:57 AM
Joy
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


Show quoteHide quote
"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
>
>
>
>
Author
25 Jun 2009 5:19 AM
Joy
Please also refer to the following Url:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx

regards,
Joy

Show quoteHide quote
"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
> >
> >
> >
> >
Author
25 Jun 2009 6:10 AM
Steve Harclerode
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

Show quoteHide quote
"Joy" <J**@discussions.microsoft.com> wrote in message
news:7BC2CCD3-FE26-4E29-890C-AA105778865A@microsoft.com...
> 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:
>
Author
25 Jun 2009 8:33 AM
Joy
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

Show quoteHide quote
"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" <J**@discussions.microsoft.com> wrote in message
> news:7BC2CCD3-FE26-4E29-890C-AA105778865A@microsoft.com...
> > 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:
> >
>
>
Author
25 Jun 2009 2:32 PM
Steve Harclerode
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" <J**@discussions.microsoft.com> wrote in message
news:FEC3BF71-7536-4FBF-808B-22267458590E@microsoft.com...
> 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.

Bookmark and Share