By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

textbox value greater than zero? Client-side check?

P: n/a
I'm not sure how to exactly ask this, but expect that it's a Javascript
solution. However, I'll ask the question and maybe someone can point
me to some existing code that will do what I need.

I have an Asp.Net app that's in 2.0 framework. I have a page that is
sort of a storefront sort of page that has a product, a quantity and a
button for "Add To Cart" and one for "Add To Wishlist".
I'd like to disable those two buttons if the quantity is not greater
than zero.
Can someone help?

Nov 17 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Hi,

HockeyFan wrote:
I'm not sure how to exactly ask this, but expect that it's a Javascript
solution. However, I'll ask the question and maybe someone can point
me to some existing code that will do what I need.

I have an Asp.Net app that's in 2.0 framework. I have a page that is
sort of a storefront sort of page that has a product, a quantity and a
button for "Add To Cart" and one for "Add To Wishlist".
I'd like to disable those two buttons if the quantity is not greater
than zero.
use compare-validators for things like this - these will validate data on
the client-side. Set your button's CausesValidation-property to True, add a
compare-validator for each textbox, linking them to another. If you need
several different validation-groups, use the ValidationGroup-properties. If
you'd rather like to display a single message (as opposed to or in addition
to showing error-messages right next to the control), use a
ValidationSummary-control.

Cheers,
Olaf
--
My .02: www.Resources.IntuiDev.com
Nov 17 '06 #2

P: n/a
bpd
Off the top of my head without seeing your code: I suppose you could
default the buttons to be disabled and fire the "TextChanged" event
when a value is entered in a quantity textbox or the
"SelectedIndexChanged" event if it is a drop down list. In these
events, you can then enable the buttons. You will probably need to put
code in the events to check if your qty is greater than 0 or not and
enable/disable accordingly (the user may remove items from cart as well
as add?).

HockeyFan wrote:
I'm not sure how to exactly ask this, but expect that it's a Javascript
solution. However, I'll ask the question and maybe someone can point
me to some existing code that will do what I need.

I have an Asp.Net app that's in 2.0 framework. I have a page that is
sort of a storefront sort of page that has a product, a quantity and a
button for "Add To Cart" and one for "Add To Wishlist".
I'd like to disable those two buttons if the quantity is not greater
than zero.
Can someone help?
Nov 17 '06 #3

P: n/a

HockeyFan wrote:
I'm not sure how to exactly ask this, but expect that it's a Javascript
solution. However, I'll ask the question and maybe someone can point
me to some existing code that will do what I need.

I have an Asp.Net app that's in 2.0 framework. I have a page that is
sort of a storefront sort of page that has a product, a quantity and a
button for "Add To Cart" and one for "Add To Wishlist".
I'd like to disable those two buttons if the quantity is not greater
than zero.
Can someone help?
Olaf's advice looks good. Validators are ideal for situations like
this.

Nov 17 '06 #4

P: n/a
bpd
I believe they want to enable/disable buttons and not display messages
when the qty is 0. Please advise on how to do this with validators.

Eric wrote:
HockeyFan wrote:
I'm not sure how to exactly ask this, but expect that it's a Javascript
solution. However, I'll ask the question and maybe someone can point
me to some existing code that will do what I need.

I have an Asp.Net app that's in 2.0 framework. I have a page that is
sort of a storefront sort of page that has a product, a quantity and a
button for "Add To Cart" and one for "Add To Wishlist".
I'd like to disable those two buttons if the quantity is not greater
than zero.
Can someone help?

Olaf's advice looks good. Validators are ideal for situations like
this.
Nov 17 '06 #5

P: n/a
"Eric" <ve****@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Olaf's advice looks good. Validators are ideal for situations like
this.
I disagree - I think bpd has the right idea...
Nov 18 '06 #6

P: n/a
Hi,

Mark Rae wrote:
>Olaf's advice looks good. Validators are ideal for situations like
this.

I disagree - I think bpd has the right idea...
thing is, you'd need server-side code in order to actually enable the
button. If there's a client-side-way of dealing with this (i.e. enabling a
control), then I'd sure like to know ...

Cheers,
Olaf
--
My .02: www.Resources.IntuiDev.com
Nov 18 '06 #7

P: n/a
"Olaf Rabbachin" <Ol*********@IntuiDev.comwrote in message
news:eA**************@TK2MSFTNGP04.phx.gbl...
thing is, you'd need server-side code in order to actually enable the
button. If there's a client-side-way of dealing with this (i.e. enabling a
control), then I'd sure like to know ...
??? You can enable / disable a control with client-side JavaScript just as
easily as you can with server-side C#.

<script type="text/javascript">

function enableButtons
{
document.getElementById('MyButton').disabled =
(parseInt(document.getElementById('MyTextBox').val ue) == 0);
}

</script>

<input type="text" id="MyTextBox" onblur="enableButtons();" />

<input type="button" id="MyButton" value="Save" />
Nov 18 '06 #8

P: n/a
Hi,

Mark Rae wrote:
??? You can enable / disable a control with client-side JavaScript just as
easily as you can with server-side C#.

<script ...
great, thanks! I simply have lots to learn concerning JS. :-)
Since we're at this - assuming the following script-code ...

If Not ClientScript.IsClientScriptBlockRegistered("Enable Go") Then
Dim strJS As String = _
"<script language=""JavaScript""" & _
"function enableGo(chk) {" & _
"document.getElementById(""" & cmdGo.ClientID & """).disabled = " & _
"(document.getElementById(chk).checked == false);" & _
"}" & _
"</script>"
ClientScript.RegisterClientScriptBlock(Me.GetType, "EnableGo", strJS)
End If

.... which is supposed to enable a button if the passed checkbox's ID
(ClientID) is checked. With controls that have been created at design-time,
this will work just fine (as with cmdGo.ClientID), but in my case the
checkboxes are created dynamically.
Hence, I'm doing the following for each checkbox being created:

dim chkBox as New CheckBox
....
chkBox.Attributes.Add("onclick", "enableGo(" & chkBox.ClientID & ")")

However, instead of the ClientID, only the ID will be passed and the script
fails. Any clue on how to get this working for dynamic controls?

Cheers,
Olaf
--
My .02: www.Resources.IntuiDev.com
Nov 18 '06 #9

P: n/a
"Olaf Rabbachin" <Ol*********@IntuiDev.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Mark Rae wrote:
>??? You can enable / disable a control with client-side JavaScript just
as
easily as you can with server-side C#.

<script ...

great, thanks! I simply have lots to learn concerning JS. :-)
JavaScript remains as important in web development now as when it first
appeared - ASP.NET hasn't changed that at all...
"<script language=""JavaScript""" & _
"<script language=""text/javascript""" & _

if you want it to be XHTML-compliant...
However, instead of the ClientID, only the ID will be passed and the
script
fails. Any clue on how to get this working for dynamic controls?
I assume you're creating the dynamic controls in Page_Init - if not, you
need to...
Nov 18 '06 #10

P: n/a
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"<script language=""text/javascript""" & _
Sorry, I mean "<script type=""text/javascript""" & _
Nov 18 '06 #11

P: n/a
Hi,

Mark Rae wrote:
>However, instead of the ClientID, only the ID will be passed and the
script fails. Any clue on how to get this working for dynamic controls?

I assume you're creating the dynamic controls in Page_Init - if not, you
need to...
I do, but still, the dynamic checkboxes won't show the ID rather than the
ClientID. Since I'm passing the button's ClientID to the script itself
within the Page_Init-event as well, I guess this should really be working,
but obviously it doesn't ...

Cheers,
Olaf
--
My .02: www.Resources.IntuiDev.com
Nov 18 '06 #12

P: n/a
"Olaf Rabbachin" <Ol*********@IntuiDev.comwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
I do, but still, the dynamic checkboxes won't show the ID rather than the
ClientID. Since I'm passing the button's ClientID to the script itself
within the Page_Init-event as well, I guess this should really be working,
but obviously it doesn't ...
In which case, don't pass the ClientID at all, as follows:

If Not ClientScript.IsClientScriptBlockRegistered("Enable Go") Then
Dim strJS As String = _
"<script language=""JavaScript""" & _
"function enableGo(chk) {" & _
"document.getElementById(""" & cmdGo.ClientID & """).disabled = " & _
"chk.checked == false);" & _
"}" & _
"</script>"
ClientScript.RegisterClientScriptBlock(Me.GetType, "EnableGo", strJS)
End If

chkBox.Attributes.Add("onclick", "enableGo(this);")
Nov 18 '06 #13

P: n/a
Hi,

Mark Rae wrote:
>I do, but still, the dynamic checkboxes won't show the ID rather than the
ClientID. Since I'm passing the button's ClientID to the script itself
within the Page_Init-event as well, I guess this should really be working,
but obviously it doesn't ...

In which case, don't pass the ClientID at all, as follows:

If Not ClientScript.IsClientScriptBlockRegistered("Enable Go") Then
Dim strJS As String = _
"<script language=""JavaScript""" & _
"function enableGo(chk) {" & _
"document.getElementById(""" & cmdGo.ClientID & """).disabled = " & _
"chk.checked == false);" & _
"}" & _
"</script>"
ClientScript.RegisterClientScriptBlock(Me.GetType, "EnableGo", strJS)
End If

chkBox.Attributes.Add("onclick", "enableGo(this);")
might be another way, I'll try that out. However, the reason for my code
not working seems to have been that the dynamic checkboxes themselves were
part of dynamic table-cells. The ClientID seems to be created only after
controls (that is, the top-container they may be part of) are actually
being added to the page. In my case, the ClientID will be right after the
table-cell - being the checkbox's parent - will have been added to the
page. Et voilá!

Cheers & thanks again,
Olaf
--
My .02: www.Resources.IntuiDev.com
Nov 18 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.