1

我在页面加载时注册了以下脚本:

    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 中注册吗?

4

2 回答 2

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>

这是我得到的输出:

在此处输入图像描述

于 2012-07-10T09:43:52.083 回答
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>
于 2012-07-10T08:48:02.650 回答