Fire validation when a selectbox item is selected
-
Hi, I have a web form that has some textboxes and a submit button that saves the form. I also have two selectboxes (drop down list) on the top of the page. The content of the form is changed when you select a different item in any of those two selectboxes (year and month). I have validators on the form that are working nice when I click the button (if an error accrues the submitting is canceled and a validationcontrol is shown). But when I change a item on a selectbox (AutoPostBack = true) the submit happens even dough there are errors on the form. What I would like to accomplish basically is that when you change the item validation accurse and if there are any errors cancel the submit (and show validation control). I have "hacked" a little and used this code to accomplish this (but this is a hack, and I am sure there is a right way to do this)
Me.ddlMonth.Attributes.Add("onchange", "if (typeof(Page_ClientValidate) == 'function'){if (Page_ClientValidate()==false)return false;}") Me.ddlMonth.Attributes.Add("language", "javascript") Me.ddlYear.Attributes.Add("onchange", "if (typeof(Page_ClientValidate) == 'function'){if (Page_ClientValidate()==false)return false;}") Me.ddlYear.Attributes.Add("language", "javascript")
Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us! -
Hi, I have a web form that has some textboxes and a submit button that saves the form. I also have two selectboxes (drop down list) on the top of the page. The content of the form is changed when you select a different item in any of those two selectboxes (year and month). I have validators on the form that are working nice when I click the button (if an error accrues the submitting is canceled and a validationcontrol is shown). But when I change a item on a selectbox (AutoPostBack = true) the submit happens even dough there are errors on the form. What I would like to accomplish basically is that when you change the item validation accurse and if there are any errors cancel the submit (and show validation control). I have "hacked" a little and used this code to accomplish this (but this is a hack, and I am sure there is a right way to do this)
Me.ddlMonth.Attributes.Add("onchange", "if (typeof(Page_ClientValidate) == 'function'){if (Page_ClientValidate()==false)return false;}") Me.ddlMonth.Attributes.Add("language", "javascript") Me.ddlYear.Attributes.Add("onchange", "if (typeof(Page_ClientValidate) == 'function'){if (Page_ClientValidate()==false)return false;}") Me.ddlYear.Attributes.Add("language", "javascript")
Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us! -
can't you just check for error inside the submit and cancel there? 1 line of code equals many bugs. So don't write any!!
-
Well that is what I am doing now... And that is a hack. Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!
Not actually. Validator aren't required. There many ways to validate. And it can be argued that validators are not the best way to validate. My classes validate data not validators. So it depends on one's perception and programming style. Nick 1 line of code equals many bugs. So don't write any!! -- modified at 12:07 Thursday 16th February, 2006
-
Not actually. Validator aren't required. There many ways to validate. And it can be argued that validators are not the best way to validate. My classes validate data not validators. So it depends on one's perception and programming style. Nick 1 line of code equals many bugs. So don't write any!! -- modified at 12:07 Thursday 16th February, 2006
-
Well we are using client side validation (don't ask me why there is no server side), and so I need a solution for that... Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!
-
Not my fault for that really... Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!
-
Hi, I have a web form that has some textboxes and a submit button that saves the form. I also have two selectboxes (drop down list) on the top of the page. The content of the form is changed when you select a different item in any of those two selectboxes (year and month). I have validators on the form that are working nice when I click the button (if an error accrues the submitting is canceled and a validationcontrol is shown). But when I change a item on a selectbox (AutoPostBack = true) the submit happens even dough there are errors on the form. What I would like to accomplish basically is that when you change the item validation accurse and if there are any errors cancel the submit (and show validation control). I have "hacked" a little and used this code to accomplish this (but this is a hack, and I am sure there is a right way to do this)
Me.ddlMonth.Attributes.Add("onchange", "if (typeof(Page_ClientValidate) == 'function'){if (Page_ClientValidate()==false)return false;}") Me.ddlMonth.Attributes.Add("language", "javascript") Me.ddlYear.Attributes.Add("onchange", "if (typeof(Page_ClientValidate) == 'function'){if (Page_ClientValidate()==false)return false;}") Me.ddlYear.Attributes.Add("language", "javascript")
Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!Hi there, Your hack is nice and IMHO it should be your choice. As you probably know that by design the snippet of client side validation script is automatically injected into the client side event
onclick
of the button, but not for theonchange
event of the dropdownlist. The ASP.NET uses the static methodGetClientValidateEvent
of the internal classSystem.Web.UI.Util
to get the script, then inject into theonclick
attribute of the button when it is rendered. So you can see that the ASP.NET also uses the similar way like yours. For the dropdownlist, this thing does not happen so you have to do it on your own, so if you want, you can customize the built-in DropDownList control by overriding theAddAttributesToRender
method, put your sample code in there after you check if the dropdownlist has theAutoPostBack
enabled and there are validators in the page. -
Hi there, Your hack is nice and IMHO it should be your choice. As you probably know that by design the snippet of client side validation script is automatically injected into the client side event
onclick
of the button, but not for theonchange
event of the dropdownlist. The ASP.NET uses the static methodGetClientValidateEvent
of the internal classSystem.Web.UI.Util
to get the script, then inject into theonclick
attribute of the button when it is rendered. So you can see that the ASP.NET also uses the similar way like yours. For the dropdownlist, this thing does not happen so you have to do it on your own, so if you want, you can customize the built-in DropDownList control by overriding theAddAttributesToRender
method, put your sample code in there after you check if the dropdownlist has theAutoPostBack
enabled and there are validators in the page.