Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Disable Validation For Dynamically Created Control

Reply
Thread Tools

Disable Validation For Dynamically Created Control

 
 
Steve Harclerode
Guest
Posts: n/a
 
      06-24-2009

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/


 
Reply With Quote
 
 
 
 
Steve Harclerode
Guest
Posts: n/a
 
      06-24-2009

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



 
Reply With Quote
 
 
 
 
Joy
Guest
Posts: n/a
 
      06-25-2009

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
>
>
>
>

 
Reply With Quote
 
Joy
Guest
Posts: n/a
 
      06-25-2009

Please also refer to the following Url:

http://msdn.microsoft.com/en-us/libr...alidation.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
> >
> >
> >
> >

 
Reply With Quote
 
Steve Harclerode
Guest
Posts: n/a
 
      06-25-2009

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:
>


 
Reply With Quote
 
Joy
Guest
Posts: n/a
 
      06-25-2009

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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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:
> >

>
>

 
Reply With Quote
 
Steve Harclerode
Guest
Posts: n/a
 
      06-25-2009

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
Dynamically created validators - server side validation not working. Tony ASP .Net 1 12-12-2003 11:58 AM
disable viewstate of dynamically created checkboxes Anton Sommer ASP .Net Building Controls 1 10-05-2003 07:31 AM
returning values from dynamically created user control Duncan Welch ASP .Net 2 07-02-2003 03:23 PM



Advertisments