using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
namespace blogsTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox[] txt = new TextBox[] { txtShiSuFei_Yuan, txtShiSuFei_Ren, txtShiSuFei_Tian };
Func("{0}*{1}*{2}", txtShiSuFei_Zong,txt);
}
public void Func(string fun,TextBox txtZhongHe,TextBox[] txt)
{
StringBuilder sb = new StringBuilder();
sb.Append("<Script>function calc"+txtZhongHe.ClientID+"(){");
sb.Append("var txtArray = new Array("+txt.Length+");");
for (int i = 0; i < txt.Length; i++)
{
txt[i].Attributes.Add("onblur", "calc" + txtZhongHe.ClientID + "()");
sb.Append("txtArray[" + i + "] = document.getElementById('" + txt[i].ClientID + "');");
}
sb.Append("var txtZhongHe = document.getElementById('" + txtZhongHe.ClientID + "');");
sb.Append("for(var i=0;i<txtArray.length;i++){");
sb.Append("if(txtArray[i].value==null ||txtArray[i].value==''||isNaN(txtArray[i].value))");
sb.Append("{txtZhongHe.value=''; return; }");
sb.Append("}");
string[] txtName = new string[txt.Length];
for (int i = 0; i < txt.Length; i++)
{
txtName[i] = "parseFloat(txtArray["+i+"].value)";
}
sb.Append("txtZhongHe.value = " + String.Format(fun, txtName) + ";}</Script>");
Page.RegisterClientScriptBlock(txtZhongHe.ClientID, sb.ToString());
}
}
}
按照要求实现运算~!
每一个红框调用一遍 Func();蓝框一样!
注意: {0}{1}{2},的序号要和数组序号对应起来!