简体   繁体   English

单击按钮后停止页面刷新

[英]Stop page refresh after button click

It is not a repeat post, I have already checked all the answers present on net and nothing is working for me so i'm posting this here. 这不是重复发的帖子,我已经检查了网上存在的所有答案,没有任何内容适合我,因此我将其发布在这里。

Please find the code. 请找到代码。

 <form runat="server">
        <div class="card">
            <ul class="nav nav-tabs Main-Menu" role="tablist">
                <li role="presentation" class="active"><a href="#Enabled" class="element" style="font-size: 20px;" aria-controls="home" role="tab" data-toggle="tab">Check Box - Enabled</a></li>
                <li role="presentation"><a href="#Disabled" class="element" style="font-size: 20px;" aria-controls="profile" role="tab" data-toggle="tab">Check Box - Disabled</a></li>
            </ul>

            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="Enabled">
                    <asp:CheckBoxList runat="server" ID="CheckBoxEnabled" RepeatDirection="Horizontal">
                        <asp:ListItem Text="Selenium IDE"></asp:ListItem>
                        <asp:ListItem Text="Selenium RC"></asp:ListItem>
                    </asp:CheckBoxList>
                    <asp:Button runat="server" Text="Submit" CssClass="btn btn-success Elements-Text" OnClick="btnEnabled_Click" />
                    <asp:Label runat="server" ID="lblEnabled" CssClass="label success"></asp:Label>
                </div>
                <div role="tabpanel" class="tab-pane" id="Disabled">
                    <asp:CheckBoxList runat="server" ID="CheckBoxDisbled" RepeatDirection="Horizontal" CellPadding="5" CellSpacing="5" CssClass="element form-control">
                        <asp:ListItem Text="Selenium IDE"></asp:ListItem>
                        <asp:ListItem Text="Selenium RC" Enabled="false"></asp:ListItem>
                        <asp:ListItem Text="Selenium WebDriver"></asp:ListItem>
                        <asp:ListItem Text="Selenium Grid" Enabled="false"></asp:ListItem>
                    </asp:CheckBoxList>
                    <asp:Button runat="server" ID="btnDisabled" Text="Submit" CssClass="element btn btn-success" OnClick="btnDisabled_Click" />
                    <asp:Label runat="server" ID="lblDisabled" CssClass="label success"></asp:Label>
                </div>
            </div>
        </div>
    </form>

I'm having 2 tabs in my page, when i'm on the second tab, select one checkbox and click on the button with #btnDisabled the page gets postback and navigates to the first tab. 我的页面上有2个标签,当我在第二个标签上时,选中一个复选框,然后单击带有#btn的按钮。已禁用页面将回发并导航到第一个标签。 I want this to stop. 我要停止。

I have already tried, 1. AutoPostBack = false 2. onClick = "return false;" 我已经尝试过了:1. AutoPostBack =假2. onClick =“返回假;” but not able to solve the issues. 但无法解决问题。

Any help will be appreciated. 任何帮助将不胜感激。 :) :)

You want to do btnDisabled_Click, but not refresh the whole page. 您想执行btnDisabled_Click,但不刷新整个页面。 You need Ajax controls around the button or anything needs to be updated. 您需要按钮周围的Ajax控件或任何需要更新的内容。 Check the tool list to find it. 检查工具列表以找到它。

( Firstly download and install AjaxControlToolkit if you haven't.) (如果尚未下载,请首先下载并安装AjaxControlToolkit。)

You can save the index of active panel in a hidden control and use it after post back. 您可以将活动面板的索引保存在隐藏的控件中,并在回发后使用。

<input type="hidden" runat="server" id="tabIndex" value="0" />

I do not know how you switch between your tabs, but on each tab change you must save it's index in hidden field(by javascript). 我不知道如何在选项卡之间切换,但是在每次更改选项卡时,都必须将其索引保存在隐藏字段中(通过javascript)。 For example if you select the second tab, value of hidden field must change to 1 . 例如,如果选择第二个选项卡,则隐藏字段的值必须更改为1

Finally add this script after all of your tabs. 最后,在所有选项卡之后添加此脚本。

var tabIndex = $('#tabIndex').val();
$('.tab-pane').removeClass('active');
$('.tab-pane').eq(tabIndex).addClass('active');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM