Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculations on GridView data on the fly

Status
Not open for further replies.

Peppi

Programmer
Apr 9, 2001
205
CA
Hi,

I have a GridView that is always available for editing (i.e. user is always in edit mode and does not need to click a button for each row in order to edit). Each row contains two date fields that are modifiable, and a Days field that is not modifiable, but is instead a calculation of the number of business days between those two dates.

If the user changes either of those two dates, I need the value of the Days column to re-calculate accordingly. I already have a method called GetBusinessDayCount which will calculate the business days between two dates that are passed as parameters. It involves calls to the database so I can't use JavaScript code.

I guess what I really need here is some event to be triggered when either of the dates change in any of the rows, so that I can call my GetBusinessDayCount method. I'm not sure how to implement this. Would it involve creating a custom event (something that I'm not terribly familiar with), or is there something else that coule be done?

Thx.
 
Couldn't you set the date controls to autopostback and then use the their relevant selection changed event?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
There are a couple of problems with doing it on the text changed event.

First, I have a JavaScript function that I want to run to check if the user has entered a valid date. If not, they should get an alert message. If I add any JavaScript code to the 'onChange' event, even if it's just 'return true;' then my ASP.NET DateChanged event never gets fired.

Secondly, if I remove the JavaScript code, the DateChanged event gets fired, but it is called for every single row in the grid. I only want it called for the row that was changed.

<asp:TextBox ID="txtLeaveDate" runat="server" SkinID="Date" Text='<%# Bind("LeaveRequestLeaveDate") %>' AutoPostBack="True" OnTextChanged="DateChanged">
</asp:TextBox><br />


Protected Sub DateChanged(ByVal sender As Object, ByVal e As System.EventArgs)
...code to get business days
End Sub
 
If I add any JavaScript code to the 'onChange' event, even if it's just 'return true;' then my ASP.NET DateChanged event never gets fired.
Strange, adding javascript code to a control shouldn't stop it posting back so there must be something wrong with the method you've used to add it.

I remove the JavaScript code, the DateChanged event gets fired, but it is called for every single row in the grid
Why does it get fired for each row? I'm sure that shouldn't be the case.



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Update on the Javascript - if I don't have a 'return' statement, then my DateChanged event will be called. If I use a 'return' statement, it doesn't get called, even if I'm returning true.

Here's what I currently have for attaching the Javascript:

Code:
'Add some Javascript to the date fields that will verify if a valid date has been entered, and if so, the row will be flagged as having been changed.
leaveDateTextBox.Attributes.Add("onChange", "if (verifyDate(" + leaveDateTextBox.ClientID + ")) {flagAsChanged(" + rowChangedHiddenField.ClientID + ");}")
returnDateTextBox.Attributes.Add("onChange", "if (verifyDate(" + returnDateTextBox.ClientID + ")) {flagAsChanged(" + rowChangedHiddenField.ClientID + ");}")

I still haven't been able to figure out why the event executes for each row in the grid, but it definitely does do that. In fact, it executes twice for each row.
 
I've given up on re-calculating the Days column automatically. It now does the calculation on the save.

However, I'm still having the issue of the DateChanged event being called for every textbox in each of the grids if I change the value in one of the textboxes.

Does anyone know why this is happening?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top