Archive

Archive for June, 2013

Get Sum of checked rows in Gridview using JQuery

June 22, 2013 2 comments

Hi all,

try this example to Get Sum of checked rows in Gridview using JQuery


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
 <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
 <title></title>
 <script type="text/javascript">
 $(document).ready(function () {
 var total;
 //Column index value of price field (Column index start from 1)
 var columnIndexValue = 4;
 var checked = $('input:checkbox').click(function (e) {
 var total = 0.0;
 $("tr:has(:checkbox:checked) td:nth-child(" + columnIndexValue + ")").each(function () {
 total += parseFloat($(this).text());
 });
 $('#Sum').text("your total is:" + total.toFixed(2));
 });
 });
 </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <div>
 <asp:GridView ID="grdItems" runat="server" AutoGenerateColumns="false">
 <Columns>
 <asp:TemplateField>
 <ItemTemplate>
 <asp:CheckBox ID="chkItem" runat="server" />
 </ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Name">
 <ItemTemplate>
 <asp:Label ID="label2" Text='<%#Eval("Name") %>' runat="server" />
 </ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Quantity">
 <ItemTemplate>
 <asp:Label ID="Label1" Text='<%#Eval("Quantity") %>' runat="server" />
 </ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Price">
 <ItemTemplate>
 <asp:Label ID="Label2" Text='<%#Eval("Price") %>' runat="server" />
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 </asp:GridView>
 </div>
 <div id="Sum">
 </div>
 </form>
 </body>
 </html>

In code behind :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 grdItems.DataSource = new Item().Items;
 grdItems.DataBind();

}

}
public class Item
{
 public string Name { get; set; }
 public int Quantity { get; set; }
 public decimal Price { get; set; }

public List<Item> Items
 {
 get
 {
 return new List<Item>()
 {
 new Item(){Name = "Item01",Quantity = 10,Price = 180M},
 new Item(){Name = "Item01",Quantity = 11,Price = 184M},
 new Item(){Name = "Item01",Quantity = 12,Price = 190M},
 new Item(){Name = "Item01",Quantity = 13,Price = 110M},
 };
 }
 }
}

Hope this helps

Good Luck.

Categories: ASP.Net, Javascript, Jquery

Get Selected Value of HTML Radio inside a GridView control

June 20, 2013 1 comment

Hi all,

try this example to Get Selected Value of HTML Radio inside a GridView control

1) Open VS 2008 and create a new web site and add new web page

2) From ToolBox drag and drop Gridview , Lable and Button Control

3) Bind your Gridview using SQLDataSource to Categories table in Northwind database


<asp:GridView ID="gv_Categories" runat="server" AutoGenerateColumns="False" BackColor="White"
 BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
 Font-Names="Verdana" DataSourceID="sds_Categories">
 <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
 <Columns>
 <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
 <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
 <asp:BoundField DataField="Description" HeaderText="Description" />
 <asp:TemplateField HeaderText="Select One">
 <ItemTemplate>
 <input name="MyRadioButton" type="radio" value='<%# Eval("CategoryID") %>' />
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 <RowStyle BackColor="White" ForeColor="#330099" />
 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
 <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
 </asp:GridView>
 <asp:SqlDataSource ID="sds_Categories" runat="server"
 ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
 SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"></asp:SqlDataSource>
 <br />
 <asp:Button ID="btn_Get" runat="server" Text="Get Selected Value"
 onclick="btn_Get_Click" />
 <br />
 <asp:Label ID="lbl_Value" runat="server" Text=""></asp:Label>

In code behind add the following code :


protected void btn_Get_Click(object sender, EventArgs e)
 {
 string selectedValue = Request.Form["MyRadioButton"];
 lbl_Value.Text ="You select Category with ID " + selectedValue;
 }

Hope this helps

Good Luck.

Categories: ASP.Net