由 ASP.NET 页面框架调用,以通知使用基于合成的实现的服务器控件创建它们包含的任何子控件,以便为回发或呈现做准备。
如果创建一个usercontrol,想要在这个UserControl里加一些js功能的话,可在重写control类下面的CreatChildControls方法
例如:
protected override void CreateChildControls()
{
base.CreateChildControls();
string script = @"function UpdateMemberCost()
{
var $lbMemberCost = $(""#" + Lb_MemberCost.ClientID + @""");
var $txtBlockPrice = $(""#" + txtBlockPrice.ClientID + @""");
if($lbMemberCost.length && $txtBlockPrice.length)
{
$lbMemberCost.text((Number($txtBlockPrice.val()) * (1 + 0.15)).toFixed(2));
$txtBlockPrice.val(Number($txtBlockPrice.val()).toFixed(2));
}
}";
txtBlockPrice.Attributes.Add("onchange", "UpdateMemberCost();");
Page.ClientScript.RegisterStartupScript(this.GetType(), "script-UpdateMemberCost", script, true);
script = @"function ddlMaxBlockNumber_onchange()
{
var $ddlMaxBlockNumber = $(""#" + ddlMaxBlockNumber.ClientID + @""");
var $txtBlockPrice = $(""#" + txtBlockPrice.ClientID + @""");
var $rfvBlockPrice = $(""#" + rfvBlockPrice.ClientID + @""");
var $rvBlockPrice1 = $(""#" + rvBlockPrice1.ClientID + @""");
var $rvBlockPrice2 = $(""#" + rvBlockPrice2.ClientID + @""");
var $rxvBlockPrice = $(""#" + RxvBlockPrice.ClientID + @""");
var valEnabled = false;
if($ddlMaxBlockNumber.length)
{
if(Number($ddlMaxBlockNumber.val()) > 0)
valEnabled = true;
if(typeof(ValidatorEnable) != ""undefined"" && $rfvBlockPrice.length && $rvBlockPrice1.length && $rvBlockPrice2.length && $rxvBlockPrice.length)
{
ValidatorEnable($rfvBlockPrice[0], valEnabled);
ValidatorEnable($rvBlockPrice1[0], valEnabled);
ValidatorEnable($rvBlockPrice2[0], valEnabled);
ValidatorEnable($rxvBlockPrice[0], valEnabled);
}
}
if($txtBlockPrice.length)
{
if(valEnabled)
$txtBlockPrice.removeAttr(""disabled"");
else
{
$txtBlockPrice.val("""");
$txtBlockPrice.attr(""disabled"", ""disabled"");
UpdateMemberCost();
}
}
}";
ddlMaxBlockNumber.Attributes.Add("onchange", "ddlMaxBlockNumber_onchange();");
Page.ClientScript.RegisterStartupScript(this.GetType(), "script-ddlMaxBlockNumber_onchange", script, true);
}