I am new to JQuery so please help resolve this query.
I have gridview in which the first column has a radio button list with Yes and No as options. Second column has some weightage information for the row.
What i want to do is, to add the weightage numbers if user has selected "yes" as an option in the radio button llist and display the sum in a label control.
Gridview layout
Order 1 Yes .3
Order 2 No .2
Order 3 Yes .1
So whenever there is a change in the selection in the radiobutton list, i want to display the sum of 3 rd column if the selection is "Yes".
function callfn(t)
{
var ans= t.checked // get true / false as u checked/unchecked
var lableval = 0;
// give some id for you datafield inside grid
// find the grid in your document and loop all those fields of that weitage in that grid and add up in to a variable.
$('yourGridView1.ClientID').find('[id^="idofgrid"]').each(function() {
lableval =lableval + parseFloat($(this).val());
});
// after loop assgin the var to label
$('#youlblid').val(lableval)
}
the field where the weightage is getting populated in the grid.... Eg : txtlbl is ur id
and in raw html, it will be generated as .... txtlbl..1, ...txtlbl..2, ...txtlbl...3..... and this we are looping in that each function by taking all IDs starting with txtlbl ....
if you got any confusion in the logic i explained, tell it...
here you are taking your gridview [ $('yourGridView1.ClientID')] and
with in grid, you are finding your all elements with the weightage using id [ .find('[id^="idofgrid"]' ]
then you will loop throught each of those elements selected, and take the value and sum up.
did u got this ? if still you have issue, run your website, take view-source... copy all the gridview HTML portion, i will try to write you full query. :)
nganesha
Member
46 Points
51 Posts
Total of column based on radio button list selection
Jan 15, 2013 07:29 PM|LINK
Hi,
I am new to JQuery so please help resolve this query.
I have gridview in which the first column has a radio button list with Yes and No as options. Second column has some weightage information for the row.
What i want to do is, to add the weightage numbers if user has selected "yes" as an option in the radio button llist and display the sum in a label control.
Gridview layout
Order 1 Yes .3
Order 2 No .2
Order 3 Yes .1
So whenever there is a change in the selection in the radiobutton list, i want to display the sum of 3 rd column if the selection is "Yes".
Please help.
Thanks,
Navin
nganesha
Member
46 Points
51 Posts
Re: Total of column based on radio button list selection
Jan 16, 2013 10:38 AM|LINK
Any suggestions?
velambath
Member
396 Points
111 Posts
Re: Total of column based on radio button list selection
Jan 16, 2013 06:12 PM|LINK
Hi,
sum of weightage numbers you mean .. of all the rows ??
Also , Where you need the label ... hope you are in need that also in each row... am i rit ??
Can you post your HTML for this Grid, so that we may help you :)
Hopfully ur requirement can be implementing by Jq easily
- Rigin
- Rigin
nganesha
Member
46 Points
51 Posts
Re: Total of column based on radio button list selection
Jan 21, 2013 06:19 PM|LINK
Hi,
Thanks for your help. Below is my aspx:
<div> <table> <tr> <td> <asp:Label ID="lb1" runat="server" Text="Total Weightage:"></asp:Label> </td> <td> <asp:Label ID="Label1" runat="server"></asp:Label> </td> </tr> </table> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:BoundField DataField="Question" HeaderText="Question" SortExpression="Question" /> <asp:TemplateField> <HeaderTemplate> <asp:Label ID="Answer" runat="server"></asp:Label> </HeaderTemplate> <ItemTemplate> <asp:RadioButtonList ID="rd1" runat="server" DataValueField='<%# Bind("Answer") %>'> <asp:ListItem value="0" Text="No"></asp:ListItem> <asp:ListItem Value="1" Text="Yes"></asp:ListItem> </asp:RadioButtonList> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Weightage" HeaderText="Weightage" SortExpression="Weightage" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [tblQuestion]"></asp:SqlDataSource> </div>Please help.
Thanks,
Navin
velambath
Member
396 Points
111 Posts
Re: Total of column based on radio button list selection
Jan 21, 2013 07:55 PM|LINK
Hi Navin,
Hope the weitage changes/ updates automatically.....
try this
assgin a on check function for RDL
<asp:RadioButtonList ID="rd1" onclick="callfn(this)".....>
----------------------------------------------------
in script ......
function callfn(t)
{
var ans= t.checked // get true / false as u checked/unchecked
var lableval = 0;
// give some id for you datafield inside grid
// find the grid in your document and loop all those fields of that weitage in that grid and add up in to a variable.
$('yourGridView1.ClientID').find('[id^="idofgrid"]').each(function() {
lableval =lableval + parseFloat($(this).val());
});
// after loop assgin the var to label
$('#youlblid').val(lableval)
}
Get back to us, if you face any trouble ....
- Rigin
nganesha
Member
46 Points
51 Posts
Re: Total of column based on radio button list selection
Jan 21, 2013 08:58 PM|LINK
Hi,
Thanks for looking into this.
What do you mean by: // give some id for you datafield inside grid. I have alredy given an id tothe radiobutton list (rd1).
Also when i select an option in the radiobutton list, t.checked comes up as undefined.
Any ideas?
Thanks,
Navin
velambath
Member
396 Points
111 Posts
Re: Total of column based on radio button list selection
Jan 21, 2013 09:02 PM|LINK
the field where the weightage is getting populated in the grid.... Eg : txtlbl is ur id
and in raw html, it will be generated as .... txtlbl..1, ...txtlbl..2, ...txtlbl...3..... and this we are looping in that each function by taking all IDs starting with txtlbl ....
if you got any confusion in the logic i explained, tell it...
- Rigin
velambath
Member
396 Points
111 Posts
Re: Total of column based on radio button list selection
Jan 21, 2013 09:18 PM|LINK
oo ya .. its a RBL.... i forgot that .... do this this, you will get it
<asp:RadioButtonList ID="rd1" runat="server" DataValueField="ABC" OnClick="javascript:GetRadioButtonValue(this.id);">
<asp:ListItem value="0" Text="No"></asp:ListItem>
<asp:ListItem Value="1" Text="Yes"></asp:ListItem>
</asp:RadioButtonList>
-------------------------------------------------------------------
<script type="text/javascript" language="javascript">
function GetRadioButtonValue(id) {
var radio = document.getElementsByName(id);
for (var j = 0; j < radio.length; j++) {
if (radio[j].checked)
alert(radio[j].value);
}
}
</script>
try this...
- Rigin
nganesha
Member
46 Points
51 Posts
Re: Total of column based on radio button list selection
Jan 22, 2013 02:59 AM|LINK
velambath
Member
396 Points
111 Posts
Re: Total of column based on radio button list selection
Jan 22, 2013 06:47 AM|LINK
Hi...
ya i understood that.... for that i have given this method below :
$('yourGridView1.ClientID').find('[id^="idofgrid"]').each(function() {
lableval =lableval + parseFloat($(this).val());
});
i will explain...
here you are taking your gridview [ $('yourGridView1.ClientID')] and
with in grid, you are finding your all elements with the weightage using id [ .find('[id^="idofgrid"]' ]
then you will loop throught each of those elements selected, and take the value and sum up.
did u got this ?
if still you have issue, run your website, take view-source... copy all the gridview HTML portion, i will try to write you full query. :)
- Rigin