• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

Work around for autopostback in ASP.NET

I am having performance issues on an ASP.NET page. It has a lot of data - and is using autopostbacks for some fields. I need to create a faster way to do this. Any suggestions?

This is what needs to happen:

The page needs to be able to display and correctly save numeric values that may need to be converted to metric values. The data is ALWAYS STORED IN US values (with 4 decimal place accuracy). For example, if the value is 12 feet, the value stored in the database is 12. If the user that is viewing the page has a login type of Canadian, then this page will need to convert the 12 Feet to the appropriate number of Meters and display that. When the user saves the page, the value in this field will need to be converted to FEET and saved.

The way that I am currently doing this is to have a hidden label field on the form and load the database value into this form on page load. I have an onchange event for this textbox that causes any entered value to be stored in the hidden field (and converted to US value (feet)  if this is a Canadian user) I have an autopostback on this field.

My solution works great - except that there is a huge performance problem.

Is there a way to accomplish this in Javascript?
0
pmacmann
Asked:
pmacmann
  • 4
  • 4
1 Solution
 
jpontaniCommented:
<script language="javascript">
function toMeters(vFeet) {
      return ((vFeet * 12 * 2.54) / 100);
}

function toFeet(vMeter) {
      return (vMeter * 100 / 2.54 / 12);
}
</script>

- Joe
0
 
pmacmannAuthor Commented:
Since I need to do this on change of the initial text field, what do I need to do to make this happen.

I have in my aspx page:

<asp:textbox runat=server id="txtAmount"></asp:textbox>
<asp:Label runat=server id="lblHiddenAmount"></asp:label>
0
 
jpontaniCommented:
<HTML>
<HEAD>
<script language="javascript">
var curType = 1; // 0 will be English, 1 will be Metric

function load() {
      if(curType == 1) { document.getElementById("nunit").innerText = "feet"; document.getElementById("ounit").innerText = "meters"; }
      else if(curType == 0) { document.getElementById("nunit").innerText = "meters"; document.getElementById("ounit").innerText = "feet"; }
}

function changeValue(myItem, newItem) {
      var myNewItem = document.getElementById(newItem);
      if(curType == 0) {
            myNewItem.innerText = toMeters(myItem.value);
      } else if (curType == 1) {
            myNewItem.innerText = toFeet(myItem.value);
      }
}

function toMeters(vFeet) {
     return Math.ceil(vFeet * 12 * 2.54 / 100);
}

function toFeet(vMeter) {
     return Math.ceil(vMeter * 100 / 2.54 / 12);
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout" onload="load()">
<form id="Form1" method="post" runat="server">
      <asp:textbox runat=server id="txtAmount" onchange="changeValue(this, 'txtHiddenAmount')"></asp:textbox>&nbsp;<span id="ounit"></span>
      <br>
      <asp:Label runat=server id="txtHiddenAmount"></asp:Label>&nbsp;<span id="nunit"></span>
</form>
</body>
</HTML>

That's a demo bit.

HTH,
- Joe
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
pmacmannAuthor Commented:
The determination whether this user is US or Canadian is stored in a Session variable - does Javascript have access to this value? If not, I could store this value ( Session("CulturePref") ) in another hidden field - and then can Javascript access this field to determine if a conversion is necessary?

Thanks
0
 
jpontaniCommented:
In your javascript code, just do

var curtype = <%=Session("CulturePref")%>

or modify the script in they aren't 0/1 to determine which is which.

- Joe
0
 
pmacmannAuthor Commented:
Excellent! Works like a charm. One more question... let's say that the hidden value stored is 12.0001 and the value displayed is 12.00

If the user (who would be US at this time) wants the 12.00 value (not the 12.0001 value) - if the user enters 12.00 in the txt field - the onchange event doesn't fire because this is the same value. Is there a method besides onchange that will fire if the user enters anything into the field?
0
 
jpontaniCommented:
Yes, put it to onkeyup.

Note that putting onkeyup will fire after every keypress inside that field, so if the person types in 12345, it will fire after 1, then after 2 (reading 12), then after 3 (reading 123), and so on, but it will fire no matter if they type in the same value as before.

- Joe
0
 
pmacmannAuthor Commented:
OK - thanks for your assistance.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now