[英]How to make a server call on tab click in bootstrap in asp.net
我已經使用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");
}
但是不調用服務器代碼。 謝謝你的幫助。
用這個:
<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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.