0

我使用 Bootstrap 在 ASP.Net 中创建了一个选项卡。

<div>
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active">
            <a href="#divrecentQ" id="linkdivrecentQ" aria-controls="divrecentQ" role="tab" data-toggle="tab">
                Recent Questions
            </a>
        </li>
        <li role="presentation">
            <a href="#divpriorityQ" aria-controls="divpriorityQ" role="tab" data-toggle="tab">
                Priority Questions
            </a>
        </li>
    </ul>

    <div class="tab-content">
        <div role="tabpanel" class="tab-pane fade in active" id="divrecentQ">
            ...Recent
            <div role="tabpanel" class="tab-pane fade" id="divpriorityQ">
                ..Priority.
            </div>
        </div>

我想在单击选项卡时进行服务器调用,但由于它处于data-toggle模式,它不能在服务器端触发任何东西。我尝试使用 jQuery 拨打电话。

<script>


    $("#linkdivrecentQ").click(function () {

        alert($(this).attr('id'));
        loadimages($(this));
        $.ajax({
            type: 'POST',
            url: '<%= ResolveUrl("~/amain.aspx/LoadImages") %>',
            data: '{ }',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (msg) {
                alert(msg.d)
            }
                    });
    });

</script>

对于服务器端的代码:

public static void LoadImages()
{
    log.Debug("LoadImages is called");
}

但不调用服务器代码。谢谢你的帮助。

4

2 回答 2

1

用这个:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<div> 
                <!-- Nav tabs -->
                <ul class="nav nav-tabs" role="tablist">
                    <li role="presentation" class="active"><a href="#divrecentQ" id="linkdivrecentQ" onclick="GetCurrentString();" aria-controls="divrecentQ" role="tab" data-toggle="tab">Recent Questions</a></li>
                    <li role="presentation"><a href="#divpriorityQ" aria-controls="divpriorityQ" role="tab" data-toggle="tab">Priority Questions</a></li>
                </ul>

                <!-- Tab panes -->
                <div class="tab-content">
                    <div role="tabpanel" class="tab-pane fade in active" id="divrecentQ">
                        ...Recent
                    </div>
                    <div role="tabpanel" class="tab-pane fade" id="divpriorityQ">..Priority.</div>

                </div>
            </div>

#linkdivrecentQ 锚点中 GetCurrentString 函数的 jquery 函数:

   function GetCurrentString() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetCurrentString",
            data: '{name : "pandey" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }

function OnSuccess(response) {
    alert(response.d);
}

这里 GetCurrentString 是服务器代码中的方法

[System.Web.Services.WebMethod]
    public static string GetCurrentString(string name)
    {
        return "vishal "+name;
    }
于 2016-11-07T13:38:38.827 回答
0

我采取的一种方法是使其成为 html 服务器控件 ( runat="server") 并在使用data-toggle="tab".

     <a data-target="#divrecentQ" id="linkdivrecentQ" aria-controls="divrecentQ" 
        role="tab" runat="server" onserverclick="GetCurrentString" 
        onclick="showTab('#linkdivrecentQ');" clientidmode="static">
        Recent Questions
     </a>
  • href 更改为 data-target,因为 onserverclick() 使用 href。渲染后,您可以在检查器中看到它。
  • 我放置了clientidmode="static",所以 id 与声明的完全相同,并且没有聚合到它的父命名容器。您可以删除它并onclick="showTab('#<%=linkdivrecentQ.ClientID%>');"改用它。

showTab() 激活 <a> 中指定的数据目标。

    function showTab(id) {
        $(id).tab('show');
    }

我还将添加我正在使用选项卡面板 <div> 内的更新面板,以防止活动选项卡由于回发而更改为默认值。如果您不想使用更新面板,您可以在后面的代码中调用 showTab() 而不是控件上的 onclick()。

于 2021-09-24T17:34:18.413 回答