textbox value greater than zero? Client-side check?

Discussion in 'ASP .Net' started by HockeyFan, Nov 17, 2006.

  1. HockeyFan

    HockeyFan Guest

    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?
     
    HockeyFan, Nov 17, 2006
    #1
    1. Advertising

  2. 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
     
    Olaf Rabbachin, Nov 17, 2006
    #2
    1. Advertising

  3. HockeyFan

    bpd Guest

    Re: textbox value greater than zero? Client-side check?

    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?
     
    bpd, Nov 17, 2006
    #3
  4. HockeyFan

    Eric Guest

    Re: textbox value greater than zero? Client-side check?

    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.
     
    Eric, Nov 17, 2006
    #4
  5. HockeyFan

    bpd Guest

    Re: textbox value greater than zero? Client-side check?

    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.
     
    bpd, Nov 17, 2006
    #5
  6. HockeyFan

    Mark Rae Guest

    Re: textbox value greater than zero? Client-side check?

    "Eric" <> wrote in message
    news:...

    > Olaf's advice looks good. Validators are ideal for situations like
    > this.


    I disagree - I think bpd has the right idea...
     
    Mark Rae, Nov 18, 2006
    #6
  7. Re: textbox value greater than zero? Client-side check?

    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
     
    Olaf Rabbachin, Nov 18, 2006
    #7
  8. HockeyFan

    Mark Rae Guest

    Re: textbox value greater than zero? Client-side check?

    "Olaf Rabbachin" <> wrote in message
    news:...

    > 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" />
     
    Mark Rae, Nov 18, 2006
    #8
  9. Re: textbox value greater than zero? Client-side check?

    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("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
    --
    My .02: www.Resources.IntuiDev.com
     
    Olaf Rabbachin, Nov 18, 2006
    #9
  10. HockeyFan

    Mark Rae Guest

    Re: textbox value greater than zero? Client-side check?

    "Olaf Rabbachin" <> wrote in message
    news:%...

    > 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...
     
    Mark Rae, Nov 18, 2006
    #10
  11. HockeyFan

    Mark Rae Guest

    Re: textbox value greater than zero? Client-side check?

    "Mark Rae" <> wrote in message
    news:%23$...

    > "<script language=""text/javascript""> " & _


    Sorry, I mean "<script type=""text/javascript""> " & _
     
    Mark Rae, Nov 18, 2006
    #11
  12. Re: textbox value greater than zero? Client-side check?

    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
     
    Olaf Rabbachin, Nov 18, 2006
    #12
  13. HockeyFan

    Mark Rae Guest

    Re: textbox value greater than zero? Client-side check?

    "Olaf Rabbachin" <> wrote in message
    news:...

    > 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);")
     
    Mark Rae, Nov 18, 2006
    #13
  14. Re: textbox value greater than zero? Client-side check?

    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("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
    --
    My .02: www.Resources.IntuiDev.com
     
    Olaf Rabbachin, Nov 18, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BobRoyAce
    Replies:
    7
    Views:
    475
    Peter Blum
    Mar 3, 2005
  2. falcon
    Replies:
    10
    Views:
    19,256
    Roedy Green
    Feb 24, 2006
  3. jackie
    Replies:
    2
    Views:
    2,053
    E. R. Jacobson
    Jan 7, 2005
  4. Iain
    Replies:
    1
    Views:
    2,009
    nahid
    Jul 16, 2007
  5. Dwight Army of Champions
    Replies:
    4
    Views:
    2,903
    John H.
    Mar 17, 2010
Loading...

Share This Page