 |
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

October 31st, 2006, 02:10 PM
|
Registered User
|
|
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to change the value of one cell in a GridView?
Hi,
I want to use JavaScript TextChange(onChange) event to change the value of a cell in GridView. For example, if the user change the value of Row2Column1 to 3, then Row2Column3 = Row2Column2 * Row2Column1 after TextChange event.
Thanks,
James
|

November 1st, 2006, 04:52 PM
|
Registered User
|
|
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
Thanks for your reply, what I have is a GridView with ItemTemplate is the default view. In the ItemTemplate, I replaced the 2 default labels with 2 TextBoxes Quantity, and UnitPrice, and added one TextBox SubTotal in VisualStudio 2005. The reason for adding the TextBoxes is to allow user can change the Quantity, so the SubTotal will change to Quantiy * UnitPrice using JavaScript OnChange event. We know there are four columns, but we don't know how many rows the GridWiew will retrieve from the database. Followings are the ASPX and HTML source files:
===========default2.apsx source file =====
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ORDERID" HeaderText="ORDER ID" SortExpression="ORDERID" />
<asp:TemplateField HeaderText="QUANTITY" SortExpression="QUANTITY">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("QUANTITY") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("QUANTITY") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price" SortExpression="UnitPrice">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub Total" SortExpression="SubTotal">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>"
ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>" SelectCommand='SELECT "ORDERID", "QUANTITY", "UnitPrice" FROM "ORDERDETAIL"'>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
======================== HTML View Source file with 4 columns and 2 rows ==========================
<!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><title>
Untitled Page
</title></head>
<body>
<form name="form1" method="post" action="Default2.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE0NTU4NjI2NDkPZBYCAgQPZBYCAgEPPCsADQEADxY GHgtfIURhdGFCb3VuZGceCVBhZ2VDb3VudAIBHgtfIUl0ZW1Db 3VudAICZBYCZg9kFgYCAQ9kFghmDw8WAh4EVGV4dAUFMTAwMDJ kZAIBDw8WAh8DBQExZGQCAg8PFgIfAwUDRFZEZGQCAw8PFgIfA wUBUmRkAgIPZBYIZg8PFgIfAwUFMTAwMjBkZAIBDw8WAh8DBQE xZGQCAg8PFgIfAwUDRFZEZGQCAw8PFgIfAwUBVWRkAgMPDxYCH gdWaXNpYmxlaGRkGAEFCUdyaWRWaWV3MQ9nZA55ctqtE4EUX0/4Z0c4ARkiZFUF" />
</div>
<div>
<div>
<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
<tr>
<th scope="col">ORDER ID</th><th scope="col">QUANTITY</th><th scope="col">Unit Price</th><th scope="col">Sub Total</th>
</tr><tr>
<td>10002</td><td>0</td><td>5</td><td>0</td>
</tr><tr>
<td>10020</td><td>0</td><td>6</td><td>0</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
|

November 2nd, 2006, 06:03 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Inside the RowCreated event (which fires for each row added to the GridView) you can use FindControl to get a reference to your TextBox controls. Then you can ask for its ClientID which is the HTML id attribute as it ends up in the browser.
Then on one textbox you can add a client side onblur event to calculate whatever it is you need to calculate. This onblur handler should get the references to the TextBoxes whose ClientID you retrieved in the RowCreated event.
Finally, you can add the onblur event to the TextBox using its Attributes collection.
Does this put you on the right track?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|

November 3rd, 2006, 05:21 AM
|
Registered User
|
|
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
Thanks for your quit response. I still can not make the JavaScript to change the SubTotal value after the quantity value changes from 0 to 2. Can you provide me a sample ASPX file with JavaScript modules.
The goal is GridView1 retrieves 2 values OrderID, UnitPrice from a look-up table tblLookUp. The Users can change the value in the Quantity field of the added Template TextBox, SubTotal will change to Quantity * UnitPrce.
Then, user can save the set of GridView1 records to another table tblFinal.
table tblLookup:
OrdeerID, UnitPrice
table tblFinal
OrderID, UnitPrice, Quantity, SubTotal;
Thanks,
James
|

November 3rd, 2006, 10:43 AM
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Here is one way you could do it
On the rowcreated add the following
Dim tb1 As TextBox = e.Row.FindControl("TextBox5") 'Unit Price
Dim tb2 As TextBox = e.Row.FindControl("TextBox4") 'Quantity
Dim tb3 As TextBox = e.Row.FindControl("TextBox6") 'Sub Total
If Not tb1 Is Nothing Then
tb1.Attributes.Add("onBlur", "changeMe('" & tb1.ClientID & "','" & tb2.ClientID & "','" & tb3.ClientID & "')")
tb2.Attributes.Add("onBlur", "changeMe('" & tb1.ClientID & "','" & tb2.ClientID & "','" & tb3.ClientID & "')")
End If
Add this script to the aspx page
<script type="text/javascript">
function changeMe(oPrice, oQty, oSubTotal){
var Price = document.getElementById(oPrice).value;
var Qty = document.getElementById(oQty).value;
SubTotal = Price * Qty;
document.getElementById(oSubTotal).value = SubTotal;
}
</script>
Let me know if this works..
|

November 3rd, 2006, 10:51 AM
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I would also add the following to the itemtemplate for the sub total
Text='<%# eval("QUANTITY") * eval("UnitPrice")%>'
This will initially populate the sub total textboxes
|

November 3rd, 2006, 04:24 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yeah, that's about the code I had in mind....
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|

November 19th, 2006, 01:12 PM
|
Registered User
|
|
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have found this topic very helpful. Thanks guys
|
|
 |