用户Web控件处理OnClientClick事件

时间:2015-04-17 10:43:54

标签: javascript c# asp.net

我有下一个用户网页控件:

<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处理此事件时,显示'Hello'而没有任何回发到服务器,但当我点击Button2(Button1的复制粘贴)时,OnClientClick无法处理点击事件和服务器服务器获得回发。我无法理解为什么会这样,因为我不需要回发。谁能告诉我我的代码有什么问题?

2 个答案:

答案 0 :(得分:2)

您没有收到来自button1的回发,因为您从false OnClientClick返回button1,并且您从button2收到回复,因为您从true返回button2 false

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

  • 如果您从javascript处理程序返回false,则获取 回发。

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

true

button2返回<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;
}

您可以从事件处理函数

返回true或false
{{1}}

答案 1 :(得分:0)

也许修改你的onclientlcick和功能

OnClientClick =&#34; javascript:返回Myfunction();&#34;

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