textbox value greater than zero? Client-side check?

  • Thread starter Thread starter HockeyFan
  • Start date Start date
H

HockeyFan

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?
 
Hi,
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
 
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 said:
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.
 
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.
 
Hi,

Mark said:
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
 
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').value) == 0);
}

</script>

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

<input type="button" id="MyButton" value="Save" />
 
Hi,

Mark said:
??? 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("EnableGo") 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
 
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...
 
Hi,

Mark said:
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
 
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("EnableGo") 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);")
 
Hi,

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

If Not ClientScript.IsClientScriptBlockRegistered("EnableGo") 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
 
Back
Top