How do I update a label with the summary total of six textboxes?

K

Karen Grube

Hi!

I have a web page containing 6 text boxes placed one below the other in a
column. Each textbox accepts only numeric input and is formatted and
validated as numeric.

As I'm entering each amount, I need to display the summary total of the
amounts that have been entered in a label below the column. I can't seem to
be able to do that! All I want to do is update the text value of the label.
I want for the value of the total to change as the individual amounts are
entered. Below, I'm showing three columns, but really, this is just one
column showing what happens as each of the three amounts is entered.

ENTRY1 ENTRY2 ENTRY3
120.00 120.00 120.00 <---- This is textbox1
240.00 240.00 <---- This is textbox2
230.50 <--- This is textbox3

120.00 360.00 590.50 <--- This is the label I need
to update.


For each of the text boxes, I'm calling the TextChanged event, recalculating
the total and then setting the label text to the string formatted value of
the total. However, the new amount is never displayed on the screen.
Initially, the value of the total is set to zero, and that displays, but is
never updated as the amounts are entered.

These are completely unbound controls, by the way. Do I need to be binding
the label to something or do something in the html code to get the total to
display?

Any exmples or discussion of how to do this would be greatly appreciated.

Thanks!
Karen Grube
(e-mail address removed)
 
K

Ken Cox [Microsoft MVP]

Hi Karen,

I'm not sure that I understand completely, but you should be able to update
the labels every time there's a textchanged event in a textbox - if
autopostback=true. I've pasted in a little sample below. Let us know if it
helps?

Ken
MVP [ASP.NET]

<%@ Page Language="VB" %>
<script runat="server">

' Insert page code here
'

Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
call UpdateLabels
End Sub

Sub UpdateLabels
label1.text=(cint(Textbox1.text) + cint(textbox2.text)).tostring()
label2.text=(cint(Textbox1.text) + cint(textbox2.text) +
cint(textbox3.text)).tostring()
end sub

Sub TextBox2_TextChanged(sender As Object, e As EventArgs)
call UpdateLabels
End Sub

Sub TextBox3_TextChanged(sender As Object, e As EventArgs)
call UpdateLabels
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server" AutoPostBack="True"
OnTextChanged="TextBox1_TextChanged">0</asp:TextBox>
</p>
<p>
<asp:TextBox id="TextBox2" runat="server" AutoPostBack="True"
OnTextChanged="TextBox2_TextChanged">0</asp:TextBox>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
<p>
<asp:TextBox id="TextBox3" runat="server" AutoPostBack="True"
OnTextChanged="TextBox3_TextChanged">0</asp:TextBox>
<asp:Label id="Label2" runat="server">Label</asp:Label>
</p>
<!-- Insert content here -->
</form>
</body>
</html>
 
K

Karen Grube

Hi Ken!

Thanks! Yes, you were right. My problem was with not having
auto-postback set correctly. When I did set it, however, I found that the
screen completely redisplayed and didn't return the cursor to where it had
been. I did find that I can reset focus to the control and return the cursor
properly. However, isn't there a way to refresh (redisplay) the contents of
a control without completely redisplaying the entire screen? For example, on
the amount entry field, I'd like to redisplay what the user enters formatted
correctly with a dollar sign. I know how to format it correctly, but I can't
get it to redisplay without having autopostback set and redisplaying the
whole screen.

Isn't there some easier way of redisplaying the contents of an individual
control without auto-postback or at least without redisplaying the entire
screen and having to reset focus? Perhaps in the HTML Code? In a script?

You're right. I really am asking two separate questions. The first is
how to redisplay the contents of a single control formatted correctly. If
there's some code I can add to the html or to the code behind (like adding an
attribute or something) that will get it to redisplay formatted as a dollar
value without autopostback being triggered that would be great. The second
is how to redisplay the contents of a different control on the screen based
on the value of another control without postback being triggered and losing
focus.

Any suggestions? My screen is working now, but it's redisplaying every
time the user enters a dollar value, and I'd like to resolve that if I can.

Thanks!
Karen
 
K

Karen Grube

Hi Ken!

Yes, I did find some code on that site that I believe will be helpful -
one especially about formatting a control as currency. However, I was
wondering if you could please clarify something you said. You said that I
don't need to use ASP Server controls at all, the implication being that I
can use html controls on this page. I know you're correct about the amount
fields I mentioned, but not for some of the other fields on the form.

What I have is a database application, a "Customer Request' input form.
At the top of the form are two dropdowns, StoreID and CustomerID. On
PageInit, the StoreID dropdown is populated with a list of store ids and
names from a Store table. When the user select a store, using the 'on
selecteditemchanged event, the customer dropdown is populated from the
Customer table with Customers for that store. The Customer dropdown is
databound to the Customer table, and a parameter (@StoreId) is used in the
select command for the "daCustomer' data adapter on this web form.

The select command for the daCustomer data adapter reads something like
'Select CustomerID, CustomerName from Customers where StoreID = @StoreID'.
In the selecteditemchanged event handlier, the value for the parameter
@StoreID is set to the selectedvalue from the Store dropdown, and the
daCustomer data adapter (now filtered by StoreID) is re-bound for the
Customer dropdown. These are the only two controls on the form that are
actually bound to the database. They're really just there to identify the
customer for whom this form is being filled out. All the other controls are
not databound. Once all the data is gathered on the form and the user clicks
'Submit,' the data is validated and, if valid, inserted into a new row in a
'Customer Request' table in the database.

I'm having the same problem here as I did with the label containing the
total. I don't know how to get the second dropdown populated without setting
autopostback on the first dropdown and using the selecteditemchanged event
handler. So, there's this ugly screen refresh that happens after the StateID
is selected while the Customer dropdown list is being populated. It seems
like such a simple function! All I want to do is go get the correctly
filtered data from the database and populate the contents of the drop-down
list. I can do this - and it does work - but isn't there some way to do this
without the screen redisplaying?

On the site you mentioned, there were great examples of doing something like
this within the HTML code using javascript, but the data was all hard coded
from one drop-down to the next. My problem is that I don't know how to
populate a client-side control from a database like that, or even if that's
what it would take to populate the second dropdown without the screen
redisplaying.

Isn't there some way I can use autopostback and the server-side control
eventhandler without making the screen redisplay like that? Or is there a
way I can populate a client side control from a database, and in particular
populate the second control with data from a database filtered based on the
selected item from the first dropdown?

Perhaps you can recommend a book that deals specifically with developing
database applications like this. It just seems to me that there's a kind of
disconnect between the need to do work on the server side and the need to
display information interactively on the client side without having to use
submit buttons or redisplay the entire screen when data is changed in a
single control.

Any advice you can give would be greatly appreciated!

Thanks!
Karen Grube
(e-mail address removed)
 
K

Karen Grube

Hi!

I think I have part of my answer! I looked into SmartNavigation, and
that setting seems to be helping with returning the user to the correct place
when autopost is turned on. Still, it would be good to know how to update a
dropdown from a database without having to do autopost.

Any suggestions or referrals?

Thanks!
Karen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top