我在页面加载时注册了以下脚本:
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 :(得分: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>
答案 1 :(得分: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>
这是我得到的输出: