Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 31st, 2006, 01:10 PM
Registered User
 
Join Date: Oct 2006
Location: san francisco, CA, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

Reply With Quote
  #2 (permalink)  
Old November 1st, 2006, 02:07 AM
Imar's Avatar
Wrox Author
Points: 66,557, Level: 100
Points: 66,557, Level: 100 Points: 66,557, Level: 100 Points: 66,557, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,119
Thanks: 65
Thanked 1,381 Times in 1,361 Posts
Default

In this thread: http://p2p.wrox.com/topic.asp?TOPIC_ID=39560 I suggested it's impossible to answer your question without more information.

So, could you describe the problem in more detail? What are you trying to accomplish? What have you already tried? How does your code look like etc etc.

http://www.catb.org/~esr/faqs/smart-questions.html

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.
Reply With Quote
  #3 (permalink)  
Old November 1st, 2006, 03:52 PM
Registered User
 
Join Date: Oct 2006
Location: san francisco, CA, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>




Reply With Quote
  #4 (permalink)  
Old November 2nd, 2006, 05:03 PM
Imar's Avatar
Wrox Author
Points: 66,557, Level: 100
Points: 66,557, Level: 100 Points: 66,557, Level: 100 Points: 66,557, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,119
Thanks: 65
Thanked 1,381 Times in 1,361 Posts
Default

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.
Reply With Quote
  #5 (permalink)  
Old November 3rd, 2006, 04:21 AM
Registered User
 
Join Date: Oct 2006
Location: san francisco, CA, USA.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #6 (permalink)  
Old November 3rd, 2006, 09:43 AM
Authorized User
 
Join Date: Nov 2006
Location: Bristol, TN, USA.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to davidrodrigues40
Default

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..

Reply With Quote
  #7 (permalink)  
Old November 3rd, 2006, 09:51 AM
Authorized User
 
Join Date: Nov 2006
Location: Bristol, TN, USA.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to davidrodrigues40
Default

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

Reply With Quote
  #8 (permalink)  
Old November 3rd, 2006, 03:24 PM
Imar's Avatar
Wrox Author
Points: 66,557, Level: 100
Points: 66,557, Level: 100 Points: 66,557, Level: 100 Points: 66,557, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,119
Thanks: 65
Thanked 1,381 Times in 1,361 Posts
Default

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.
Reply With Quote
  #9 (permalink)  
Old November 19th, 2006, 12:12 PM
Registered User
 
Join Date: Nov 2006
Location: , , USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have found this topic very helpful. Thanks guys


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Get GridView Cell Value Based on GridView Column stublair C# 2008 aka C# 3.0 0 September 4th, 2008 08:30 AM
GridView - cell wrapping - need help snufse ASP.NET 2.0 Basics 0 July 21st, 2008 04:53 PM
Change value in GridView Cell Sheraz Khan ASP.NET 2.0 Professional 2 August 30th, 2007 11:12 AM
Problem in Gridview Cell Lalit Pradhan ASP.NET 2.0 Professional 2 August 1st, 2007 04:08 AM



All times are GMT -4. The time now is 02:27 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.