2012-07-10 104 views
1

我有以下页面加载注册的脚本JavaScript函数:注册使用ScriptManager.RegisterClientScriptBlock

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "watermark", "function WaterMark(txtWaterMark, event,text) { if (txtWaterMark.value.length > 0 && event.type == 'mouseover') {txtWaterMark.style.color = '#c6c1c1'; if(txtWaterMark.value == text) {txtWaterMark.value = text;} } if (txtWaterMark.value.length > 0 && event.type == 'mouseout') {txtWaterMark.style.color = 'gray';if (txtWaterMark.value.length == 0){txtWaterMark.value = text;} } if (event.type == 'focus') {alert(txtWaterMark.value); if(txtWaterMark.value == text){txtWaterMark.value = '';} } }", true); 

我可以在ASPX创建此功能在CS注册?

+0

为什么要在CS注册? – Adil 2012-07-10 08:50:52

+0

@Adil因为控件位于updatepanel内部。如果我不注册他们,我得到函数未定义的错误。 – DotnetSparrow 2012-07-10 09:08:14

+0

你可以在更新面板中添加必要的html,这样我可以为你提供一些解决方案,我想我们可以在不注册你正在做的方式的情况下做到这一点。 – Adil 2012-07-10 09:13:48

回答

0

是的,你可以做这样的

ScriptManager.RegisterClientScriptBlock(this.GetType(), "watermark", "<script>WaterMark('watermark',event,'text')</script>"); 



<script> 
function WaterMark(txtWaterMark, event,text) { if (txtWaterMark.value.length > 0 && event.type == 'mouseover') {txtWaterMark.style.color = '#c6c1c1'; if(txtWaterMark.value == text) {txtWaterMark.value = text;} } if (txtWaterMark.value.length > 0 && event.type == 'mouseout') {txtWaterMark.style.color = 'gray';if (txtWaterMark.value.length == 0){txtWaterMark.value = text;} } if (event.type == 'focus') {alert(txtWaterMark.value); if(txtWaterMark.value == text){txtWaterMark.value = '';} } } 
<script> 
+0

我使用了上面的代码,但没有定义WaterMark。 – DotnetSparrow 2012-07-10 09:22:21

0

要回答你的问题,是的,你可以在一个外部js文件编写脚本代码和ScriptManager控制的范围内进行注册:

<asp:ScriptManager runat="server" ID="ss" > 
     <Scripts> 
      <asp:ScriptReference Path="~/Scripts/yourscript.js" /> 
     </Scripts> 
    </asp:ScriptManager> 

但在这种情况下,你不需要,我只是复制你的代码,它的工作原理,尝试像这样注册它:

<script type="text/javascript"> 
    function WaterMark(txtWaterMark, event, text) { 
     if (txtWaterMark.value.length > 0 && event.type == 'mouseover') { 
      txtWaterMark.style.color = '#c6c1c1'; 
      if (txtWaterMark.value == text) { 
       txtWaterMark.value = text; 
      } 
     } 
     if (txtWaterMark.value.length > 0 && event.type == 'mouseout') { 
     txtWaterMark.style.color = 'gray'; if (txtWaterMark.value.length == 0) { txtWaterMark.value = text; } }if (event.type == 'focus') { alert(txtWaterMark.value); if (txtWaterMark.value == text) {txtWaterMark.value = ''; } } 
    } 
</script> 


    <asp:ScriptManager runat="server" ID="ss" /> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
     <asp:TextBox 
      ID="txtFirstName" 
      runat="server" 
      Width="165px" 
      Text="Enter First Name." 
      ForeColor="Gray" 
      onmouseover="WaterMark(this, event,'Enter First Name.');" 
      onmouseout="WaterMark(this, event,'Enter First Name.');" 
      onfocus="WaterMark(this, event,'Enter First Name.');" 
      ToolTip="Type your First Name." 
      ValidationGroup="CheckoutConfirm"> 
     </asp:TextBox> 
    </ContentTemplate> 
</asp:UpdatePanel> 

这是我得到的输出:

enter image description here