×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Client Javascript to sum up a datagrid based on a checkbox

Client Javascript to sum up a datagrid based on a checkbox

Client Javascript to sum up a datagrid based on a checkbox

(OP)
Hi All,

I have a databound grid with the last column as PRICE.

I want the user to check a checkbox, that will run a client javascript function to add up the PRICE column of all the other checkboxes.  

So this is a client side sum based on a datagrid.

I think this can be done.  I've done it before in classic ASP (asp3) but not show how to do this using the datagrid.

There is no control for me to find in the datagrid since its a databound column.

Any help would be appreciated.  Thanx.

RE: Client Javascript to sum up a datagrid based on a checkbox

   So, you want the user to select checkboxes that would exclude that row from the total?

RE: Client Javascript to sum up a datagrid based on a checkbox

(OP)
Actually the reverse.  If checked, add up the PRICE column through client side scripting.

RE: Client Javascript to sum up a datagrid based on a checkbox

use this:
<script>
TheElem=document.forms[0].elements
for(i=0;i<TheElem.length;i++)
{
 if(TheElem[i].type=="checkbox")
 {
   alert("Hey i am a checkbox")
 }
}
</script>

that will fetch all the checkboxes.

i would suggest u use one more concept, when u bind the grid do this:

<input type="checkbox" price="<%# PRICE FIELD%>">

now a small change in JS
<script>
TheElem=document.forms[0].elements
for(i=0;i<TheElem.length;i++)
{
 if(TheElem[i].type=="checkbox")
 {
   alert("Hey i am a checkbox and my price is "+TheElem[i].getAttribute("price"))
 }
}
</script>


using that script u can add up the prices and set it to a hidden field(which is a server control). now u can get the value in the next page...

Known is handfull, Unknown is worldfull

RE: Client Javascript to sum up a datagrid based on a checkbox

(OP)
Thanks for the reply vbkris.   


my checkbox in the datagrid is:
<asp:CheckBox ID="ReturnCheck" AutoPostBack="false" EnableViewState="false" Runat="server" />


if I add   price="<%#PRICE%>" then page gives me Compilation Error

RE: Client Javascript to sum up a datagrid based on a checkbox

(OP)
I tried:

chkB.Attributes.Add("price", e.Item.Cells(5).Text)
but asp.net keeps putting the attribute in a SPAN tag instead of inside the checkbox.

RE: Client Javascript to sum up a datagrid based on a checkbox

(OP)
HELP!!!!  Please???

RE: Client Javascript to sum up a datagrid based on a checkbox

<#PRICE%> is the bindiong command replace it with:

<%# DataBinder.Eval(Container.DataItem,"Price_FIELD")%>

Known is handfull, Unknown is worldfull

RE: Client Javascript to sum up a datagrid based on a checkbox

(OP)
If e.Item.ItemType = ListItemType.Item Or _
              e.Item.ItemType = ListItemType.AlternatingItem Then
            'Snip out the ViewCount
            Dim chkB As CheckBox = New CheckBox
            Dim chkD As CheckBox = New CheckBox
            Dim bidClm As DataColumn = New DataColumn

            chkB = CType(e.Item.FindControl("ReturnCheck"), CheckBox)
            chkD = CType(e.Item.FindControl("DamageCheck"), CheckBox)
             


        Dim JScriptValue1, JScriptValue2 As String
        'For the damage checkbox, also check the return checkbox with
        'client javascript
        JScriptValue1 = ("CheckParent('" & chkD.UniqueID & "');")
        x = InStr(JScriptValue1, "Damage")
        Mid$(JScriptValue1, x, 6) = "Return"
            chkD.Attributes.Add("onclick", JScriptValue1)
            chkB.Attributes.Add("price", "asdf")

-------------------------------------------
when the last line of this code executes (chkB.Attributes.Add("price", "asdf"))
I do a view source on the page and find that the PRICE attribute is not really an attribute of
the ReturnCheck checkbox.  PRICE is inside a Span tag so I can't reterive it.

How do I add an attribute to the checkbox control?

RE: Client Javascript to sum up a datagrid based on a checkbox

how about the onclick one???

Known is handfull, Unknown is worldfull

RE: Client Javascript to sum up a datagrid based on a checkbox

(OP)
The onclick is fine and shows up inside the control like it should.  But Price is on a SPAN tag.

RE: Client Javascript to sum up a datagrid based on a checkbox

thats strange,

where is this span tag generated? near the checkbox???

Known is handfull, Unknown is worldfull

RE: Client Javascript to sum up a datagrid based on a checkbox

(OP)
<span price="150"><input id="PaymentData__ctl2_ReturnCheck" type="checkbox" name="PaymentData:_ctl2:ReturnCheck" onclick="UncheckDamage('PaymentData:_ctl2:DamageCheck');" /></span>

RE: Client Javascript to sum up a datagrid based on a checkbox

hmm, thats strange. i dont have any clue here, it is working for one but not for another!

Known is handfull, Unknown is worldfull

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close