RSS

Daily Archives: September 1, 2012

Javascript function for multiplication in table dynamically

Consider the table will be generated dynamically like below,

For ex,

var table = document.getElementById(“DynamicTable”);//In this div, you have to put table tag.
for (var i = 0; i <= rowLength; i++) {
var row = table.insertRow(i);
for (var j = 0; j < columnLength + extraColumns; j++) {
var col = row.insertCell(j);
col.innerHTML = “<input type=’text’ onkeyup=’GetSelectedRow(this)’/>’;
}
}

Then function will be follows,

function GetSelectedRow(txt) {
var row = txt.parentNode.parentNode;
var price = Number(row.cells[0].getElementsByTagName(“input”)[0].value);
var qty = Number(row.cells[1].getElementsByTagName(“input”)[0].value);
var Total = price * qty;
row.cells[2].innerHTML = Total;
}

 
Leave a comment

Posted by on September 1, 2012 in Javascript

 

Multiplication of two columns in gridview and display the result in third column with footer dynamically

We can display the multiplication result of two column value in third column and net total in footer of gridview.

In .aspx page,

<asp:GridView ID=”GridView1″ runat=”server”>
<Columns>
<asp:TemplateField HeaderText=”Price”>
<ItemTemplate>
<asp:TextBox ID=”txtPrice” runat=”server” onkeyup=”GetSelectedRow(this)” ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Qty”>
<ItemTemplate>
<asp:TextBox ID=”txtQty” runat=”server” onkeyup=”GetSelectedRow(this)”></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Total”>
<ItemTemplate>
<asp:Label ID=”lblTotal” runat=”server”></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID=”Label1″ runat=”server” Text=”Total”></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

In Javascript,

function GetSelectedRow(txt) {
var row = txt.parentNode.parentNode;
var price = Number(row.cells[0].getElementsByTagName(“input”)[0].value);
var qty = Number(row.cells[1].getElementsByTagName(“input”)[0].value);
var Total = price * qty;
row.cells[2].getElementsByTagName(“span”)[0].innerHTML = Total;
var NetTotal = 0;
var grid = document.getElementById(“<%=GridView1.ClientID%>”);
var gridLength = grid.rows.length;
for (i = 1; i < gridLength; i++) {
if (grid.rows[i].cells[0].colSpan == 1) {
if (typeof (grid.rows[i].cells[2].text) != ‘undefined’) // This checking is for to support in all browser
number = grid.rows[i].cells[2].text;
else if (typeof (grid.rows[i].cells[2].textContent) != ‘undefined’)
number = grid.rows[i].cells[2].textContent;
else if (typeof (grid.rows[i].cells[2].innerText) != ‘undefined’)
number = grid.rows[i].cells[2].innerText;
//if (!isNaN(number))
NetTotal = Number(NetTotal) + Number(number);
}
}
GrandTotal = Number(NetTotal) – Number(number);
var footerText = (grid.rows[gridLength – 1].cells[2]);
if (typeof (footerText.text) != ‘undefined’)
footerText.text = GrandTotal;
else if (typeof (footerText.textContent) != ‘undefined’)
footerText.textContent = GrandTotal;
else if (typeof (footerText.innerText) != ‘undefined’)
footerText.innerText = GrandTotal;
}
}

 
Leave a comment

Posted by on September 1, 2012 in C#.NET, VB.NET

 

Nested GridView in C#.Net

Inside the gridview’s templatefield of itemtemplate you can add one more gridview.

In .aspx page,

<asp:GridView ID=”GridView1″ runat=”server”>
<Columns>
<asp:BoundField DataField=”SNo” HeaderText=”S.No”/>
<asp:BoundField DataField=”CategoryName” HeaderText=”Product Name”/>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID=”GridView2″ runat=”server” AutoGenerateColumns=”false”>
<Columns>
<asp:BoundField DataField=”SNo” HeaderText=”S.No”/>
<asp:BoundField DataField=”CategoryName” HeaderText=”Product Name”/>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

In code behind,

protected void Page_Load(object sender, EventArgs e)
{
da.Fill(dt);
GridView1.DataSource = dt; // Data source for outer grid
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)e.Row.FindControl(“GridView2”); //Finding inner grid
DataTable dt = new DataTable();
da.Fill(dt);
gv.DataSource = dt; // Data source for inner grid.
gv.DataBind();
}
}

 
Leave a comment

Posted by on September 1, 2012 in C#.NET

 

Dataset to XML document in VB.NET

Dim doc As XmlDataDocument = New XmlDataDocument(ds)
Response.Clear()
Response.ContentType = “text/xml”
Response.ContentEncoding = System.Text.Encoding.UTF8
doc.Save(Response.Output)
Response.End()
(OR)
Dim doc As XmlDataDocument = New XmlDataDocument(ds)
doc.LoadXml(ds.GetXml)
doc.Save(Response.Output)

 
Leave a comment

Posted by on September 1, 2012 in VB.NET