Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Client Javascript to sum up a datagrid based on a checkbox

Status
Not open for further replies.

vituja123

Programmer
Jun 16, 2005
110
US
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.
 
So, you want the user to select checkboxes that would exclude that row from the total?
 
Actually the reverse. If checked, add up the PRICE column through client side scripting.
 
use this:
<script>
TheElem=document.forms[0].elements
for(i=0;i<TheElem.length;i++)
{
if(TheElem.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.type=="checkbox")
{
alert("Hey i am a checkbox and my price is "+TheElem.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
 
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
 
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.
 
<#PRICE%> is the bindiong command replace it with:

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


Known is handfull, Unknown is worldfull
 
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?
 
how about the onclick one???

Known is handfull, Unknown is worldfull
 
The onclick is fine and shows up inside the control like it should. But Price is on a SPAN tag.
 
thats strange,

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

Known is handfull, Unknown is worldfull
 
<span price="150"><input id="PaymentData__ctl2_ReturnCheck" type="checkbox" name="PaymentData:_ctl2:ReturnCheck" onclick="UncheckDamage('PaymentData:_ctl2:DamageCheck');" /></span>
 
hmm, thats strange. i dont have any clue here, it is working for one but not for another!

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top