你将如何在代码后面(服务器端)动态创建一个新的html li
元素?你将如何操纵服务器端的html列表?
如何在服务器端的现有ul
元素中访问li
? 我需要FindControl获取所有li
项目,然后添加新的li
项目。
更新
我使用jQuery AJAX来访问服务器端,所以我必须使用静态的WebMethod。 FindControl是非静态方法。
<script type="text/javascript">
$(function(){
$("#sortable").sortable();
$("#sortable").disableSelection();
});
这是jQuery的AJAX调用
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "DraggableTest.aspx/SomeMethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
这是代码隐藏
[WebMethod]
public static string SomeMethod()
{
// using System.Web.UI.HtmlControls;
HtmlGenericControl ul = FindControl("sortable") as HtmlGenericControl;
if (ul != null)
{
foreach (HtmlControl c in ul.Controls)
{
if (c.TagName.ToLower() == "li")
{
// Processing here
}
}
}
}
如果你告诉我们你的整个形势开始,而不是位添加额外的点点滴滴位你可能会得到更好的答案 - 它_sounds_喜欢你这个接近的错误的方式,但是你没有真正解释问题是什么,所以很难知道。 – Dexter 2009-12-15 16:13:19
感谢您的额外更新 - 但是如果您可以告诉我们您想通过ajax查询来实现什么,可能会有所帮助?你试图实现什么功能? – Dexter 2009-12-15 16:23:44
我有一些可排序的ul。我想要保存每个拖动元素的位置。 – nemke 2009-12-15 16:33:05