Reputation: 5366
Why do people put javascript in the code behind ? I think it is ugly to have 100 (see below) of these.... Is there some basic reasons that javascript must be in the code behind in some instances? And when it should in the aspx. ??
// now we gotta recalc fields
szCalcBenefitsTotal += " CalcCostFromPct('" + tbSocialSecurityPercent3.ClientID + "', '" + tbSocialSecurity3.ClientID + "', '" + tbSalaryAdjusted3.ClientID + "');";
szCalcBenefitsTotal += " CalcCostFromPct('" + tbMedicarePercent3.ClientID + "', '" + tbMedicare3.ClientID + "', '" + tbSalaryAdjusted3.ClientID + "');";
szCalcBenefitsTotal += " CalcCostFromPct('" + tbHealthInsurancePercent3.ClientID + "', '" + tbHealthInsurance3.ClientID + "', '" + tbSalaryAdjusted3.ClientID + "');";
szCalcBenefitsTotal += " CalcCostFromPct('" + tbLifeInsurancePercent3.ClientID + "', '" + tbLifeInsurance3.ClientID + "', '" + tbSalaryAdjusted3.ClientID + "');";
szCalcBenefitsTotal += " CalcCostFromPct('" + tbVacationPercent3.ClientID + "', '" + tbVacation3.ClientID + "', '" + tbSalaryAdjusted3.ClientID + "');";
szCalcBenefitsTotal += " CalcCostFromPct('" + tbSickLeavePercent3.ClientID + "', '" + tbSickLeave3.ClientID + "', '" + tbSalaryAdjusted3.ClientID + "');";
szCalcBenefitsTotal += " CalcCostFromPct('" + tbRetirementPercent3.ClientID + "', '" + tbRetirement3.ClientID + "', '" + tbSalaryAdjusted3.ClientID + "');";
Upvotes: 2
Views: 568
Reputation: 55976
It's likely in the code-behind because the javascript is dynamically generated by ASP.NET Code. It's not the best way to do it, it's better to inject variables in places and use static jscript to act upon the dynamically generated variables.
I would suggest putting your javascript in it's own .js file not cluttering up your html markup if possible, otherwise put it in the .aspx
Edit: (To answer your comment question)
Yes, you can do the same thing using <%= %>
and <%# %>
and in ASP.NET 4.0 <%: %>
. Also constructs like <asp:Repeater></asp:Repeater>
can be used to loop over lists/enumerables, so in theory you can do pretty-much everything in the .aspx
Upvotes: 3
Reputation: 888203
You're right; it is ugly.
They're probably generating the Javascript based on some information that is only available in the code-behind.
In general, you should put your Javascript in the ASPX, or (preferably) in a separate .JS file.
Upvotes: 1