【问题标题】:How to get a button inside the asp form without reloading the page?如何在不重新加载页面的情况下在 asp 表单中获取按钮?
【发布时间】:2012-08-03 14:57:22
【问题描述】:

所以我有这个更新面板,它运行良好。

    <div>
        <button id="foo" onclick="changeVal(this.id);">Send the ID "foo"...</button>
    </div>

<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" >
</asp:ScriptManager> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Always"> 
<ContentTemplate> 

<div id="thethetext">
<asp:Literal ID="thetext" runat="server"></asp:Literal>
</div>

</ContentTemplate> 
<Triggers>
<asp:AsyncPostBackTrigger ControlID="myButton" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>



   **<!-- MOVE BUTTON (ID="foo") OVER HERE TO DISPLAY IT UNDER THE LITERAL -->**

<asp:HiddenField ID="blah" runat="server" value="Initial Value"></asp:HiddenField>
<asp:Button ID="myButton" Text="Get Concatenated..." OnClick="Test" runat="server"></asp:Button>    
</form>

但是,我想在 asp 表单中移动按钮(id 为“foo”)。所以它显示在文字用户控件下......但如果我这样做,每次我点击它时,它都会重新加载页面。有没有办法避免这种情况?谢谢!

【问题讨论】:

    标签: asp.net ajax vb.net


    【解决方案1】:

    它会重新加载页面,因为您的按钮提交了您刚刚放入的表单。 为您的按钮赋予属性type 和值button,这样它就不会提交:

    <div>
     <button type="button" id="foo" onclick="changeVal(this.id);">Send the ID "foo"...</button>
    </div>
    

    参考:W3C HTML 4.01 Specification

    【讨论】:

    • 谢谢,工作就像一个魅力。我还发现在 onclick 函数中添加“return false”也可以使其工作。
    • @eastboundr,也许这也可以,但是没有 javascript 的浏览器仍然可以通过按下按钮意外地提交表单。 type="button" 也防止了这种情况。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2012-02-05
    • 2015-07-31
    • 2016-12-05
    • 2016-01-20
    • 1970-01-01
    相关资源
    最近更新 更多