为什么javascript部署后不呈现任何功能

时间:2018-11-13 13:31:27

标签: javascript

我正在使用onkeychange javascript 电子邮件字段

 <asp:TextBox ID="Email" runat="server" onkeyup="change();"></asp:TextBox>

function change(){
        var Email= document.getElementById('#ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_Email');
        var UserName= document.getElementById('#ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_UserName');
        UserName.value=Email.value;
     }

然后我在C#中渲染

 protected override void RenderContents(HtmlTextWriter writer)
        {

System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
sb2.AppendLine(@"<script language='javascript'>");
sb2.AppendLine(@"function change(){");
sb2.AppendLine(@"var Email= document.getElementById('#ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_Email');");
sb2.AppendLine(@"var UserName= document.getElementById('#ctl00_ctl40_g_e7fed4bf_b25a_4a8a_943d_e31932556a9e_FBACreateUserWizard_CreateUserStepContainer_UserName');");
sb2.AppendLine(@"UserName.value=Email.value;");
sb2.AppendLine(@" }");
sb2.Append(@"</script>");

if (!Page.ClientScript.IsStartupScriptRegistered("JSScript"))
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb2.ToString());

}

当我部署该功能时,我看不到呈现给页面的代码。

1 个答案:

答案 0 :(得分:0)

我认为您需要更改此行

if (!Page.ClientScript.IsStartupScriptRegistered("JSScript"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb2.ToString());

            }

对此。

//it will Render the function definition. 
if (!ClientScript.IsClientScriptBlockRegistered("JSScript")) 
{
   ClientScript.RegisterClientScriptBlock(this.GetType(), "JSScript", sb2.ToString()); 
}

有关更多信息,请参见this