通过服务器端Thru Button Command调用客户端

时间:2009-03-16 10:54:54

标签: asp.net javascript

我有这样简单的代码......如下......

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="AlinmaWebApp._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        #formHt
        {
            height: 284px;
        }
        .DivStyle
        {
            height :250px;
            background-color :Green ;
            display : block;
        }

    </style>

<script language="javascript" type ="text/javascript" >

    function TryClose() {
        document.getElementById("DivBah").style.height = '1px';
        document.getElementById("DivBah").style.display = 'none';
        return;
    }
</script>


</head>
<body onload="TakeIT() return;" >
    <form id="formHt" runat="server">
    <div id="DivBah" class="DivStyle" >
        Just Try if this part will Collapse or not</div>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="TryClose()"/>
    </form>
</body>
</html>

这给了我像=&gt;的错误消息“TryClose”不是“默认_._ ASPX”的一部分 这种编码有什么可能的错误? 因为我之前总是在Simple ASP Program中使用这种类型。

2 个答案:

答案 0 :(得分:1)

如果您使用的是vs 2008(.NET 3.5),请改用OnClientClick。

你得到的错误很合乎逻辑。 “OnClick”属性用于将处理程序“服务器端处理程序”连接到click事件。因为这些处理程序是服务器端处理程序,所以VS查看了映射到aspx页面的.vb文件,但没有找到名为TryClose()的方法。

TryClose()这是一个客户端功能;为了将其用作“客户端”上的点击事件的处理程序,您需要使用OnClientClick属性将其连接起来。 希望这有帮助!

答案 1 :(得分:1)

试试这个:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="TryClose()"/>

OnClick用于服务器端点击事件。