【问题标题】:User web control handle OnClientClick event用户 web 控件处理 OnClientClick 事件
【发布时间】:2015-04-17 10:43:54
【问题描述】:

我有下一个用户网络控制:

<script type="text/javascript">
    function Myfunction() {
        alert('Hello');
    }
</script>
    <asp:Panel Id="problemSolving_panel"    runat="server">
    <asp:Button ID="Button1" runat="server" Text="Hello world" OnClientClick="javascript:Myfunction();return false;"/>
    <fieldset class="fieldsetsElement">
        <legend class="legendsElement">
            <input Id="callToAbonent"   runat="server" type="checkbox" onchange ="ChangeEnable(this.checked, 'callToAbonentDiv');"/>
            <label for="callToAbonent">Call to abonent:</label>
        </legend>
        <div Id="callToAbonentDiv">
            <asp:CheckBox   Id="isCallToAbonentFailed"          runat="server" Text="Call to abonent failed"   Enabled="false"/>
            <br />
            <asp:Table runat="server" Enabled="false">
                <asp:TableRow>
                    <asp:TableCell>
                        <asp:TextBox    Id="datesOfCallTrying"              runat="server"                             />
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:Button ID="Button2" runat="server" Text="Hello world" OnClientClick="javascript:Myfunction();return true;"/>
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
            <br />
            <asp:Button     Id="addInfoAboutCall_button"  runat="server" Text="Add info about call" Enabled="false" OnClick="OnAddInfoAboutCall"/>
        </div>
    </fieldset>
</asp:Panel>

当我点击 Button1 时,OnClientClick 处理此事件,向我显示“你好”,没有任何回发到服务器,但是当我点击 Button2(Button1 的复制粘贴)时,OnClientClick 不处理点击事件并且服务器服务器得到回发。我不明白为什么会这样,因为我不需要回发。谁能告诉我我的代码有什么问题?

【问题讨论】:

  • 在javascript端使用return false。这将取消回发

标签: javascript c# asp.net


【解决方案1】:

您没有从 button1 获得回发,因为您是从 OnClientClickbutton1 返回的 false,而您从 button2 获得回发是因为您从 button2 返回 true

  • 如果您从 javascript 处理程序返回 true,您将收到回发。

  • 如果您从 javascript 处理程序返回 false,您将 Not 得到 回发。

button1 返回false

 <asp:Button ID="Button1" runat="server" Text="Hello world" 
 OnClientClick="javascript:Myfunction();return false;"/>

button2返回true

<asp:Button ID="Button2" runat="server" Text="Hello world" 
OnClientClick="javascript:Myfunction();return true;"/> 

您可以从事件处理函数返回真或假

function Myfunction()                   
{
    //Your code
    if(SomeCondition)
       return true;
    else
       return false;
}

【讨论】:

  • 感谢您的回答,但我有问题,因为表中按钮的 onclick 事件为空:
【解决方案2】:

也许修改你的 onclientlcick 和函数

OnClientClick="javascript:return Myfunction();"

function Myfunction() {
        alert('Hello');
        return false;
    }

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 2010-09-10
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多