使用JQuery与ASP.. NET用于slideToggle
本文关键字:用于 slideToggle NET ASP JQuery 使用 | 更新日期: 2023-09-27 18:03:19
我想在ASP中滑动切换项目列表。. NET使用jQuery,当我点击一个按钮。后面的c#代码将通过调用web服务并显示数据来更新项目符号列表。
在c#代码完成后,我必须从c#代码调用jQuery函数吗?有人能举个例子吗?
下面是我的代码,我将保持简单:页面代码:
<asp:BulletedList runat="server" id="resultsList">
</asp:BulletedList>
<asp:Button runat="server" Text="Search" id="searchBtn"
onclick="searchBtn_Click" />
button的c#代码:
resultsList.Item.Add( "New Item");
//do other stuff to list
jQuery代码:
$("#searchBtn").click(function () {
$("#resultsList").slideToggle('slow', function () {
//do something
});
});
虽然它不起作用。是因为我没有从c#代码后面调用jQuery代码吗?
试试这个方案
$("#searchBtn").click(function () {
if(!$("#resultsList").data("listBound")){
$.ajax({
url: "urlWhichWillReturnListMarkUpOrJsonResult",
succcess: function(response){
//If the response is just the requried markup
$("#resultsList").html(response).slideToggle('slow');
//If the response is a json result then you have to write a logic to read the response and create the required markup for the list and append that markup to resultList container and then call slideToggle method.
//Once the list is populated set at flag using data attributes so that next time when u click u dont have to make ajax call but just slideToggle it
$("#resultsList").data("listBound", true);
}
});
}
else
{
$("#resultsList").slideToggle('slow');
}
});
添加下面的代码作为searchBtn_Click方法的最后一个字符串:
ClientScript.RegisterStartupScript(this.GetType(), "ToogleResultsList", string.Format("$('#{0}').slideToggle('slow', function () { alert('Foo!'); });", resultsList.ClientID), true);