Asp.net 获取循环中div的id
在我的asp.net页面中,当单击按钮(getDivsOrder)时,我希望使用循环或类似的方法按顺序获取mainDiv中div的ID。我该怎么做Asp.net 获取循环中div的id,asp.net,asp.net-ajax,Asp.net,Asp.net Ajax,在我的asp.net页面中,当单击按钮(getDivsOrder)时,我希望使用循环或类似的方法按顺序获取mainDiv中div的ID。我该怎么做 <ContentTemplate> <div id="mainDiv" runat="server"> <div id="Red" runat="server"></div>
<ContentTemplate>
<div id="mainDiv" runat="server">
<div id="Red" runat="server"></div>
<div id="Yellow" runat="server"></div>
<div id="Green" runat="server"></div>
...
</div>
<asp:Button ID="getDivsOrder" runat="server" Text="Update label "
onclick="btnHelloWorld_Click" />
</ContentTemplate>
...
我编写了以下实用方法,您可以将其放入“实用工具带”类或其他任何类中。您可以像这样使用它:mainDiv.FindControlsOfType()
来返回控件列表
/// <summary>
/// Given a root Control, do a recursive search for Controls implementing or deriving from class/interface T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ctlRoot"></param>
/// <returns></returns>
public static List<T> FindControlsOfType<T>(this Control ctlRoot)
{
List<T> controlsFound = new List<T>();
if (typeof(T).IsInstanceOfType(ctlRoot))
controlsFound.Add((T)(object)ctlRoot);
foreach (Control ctlTemp in ctlRoot.Controls)
{
controlsFound.AddRange(FindControlsOfType<T>(ctlTemp));
}
return controlsFound;
}
//
///给定根控件,递归搜索实现或派生自类/接口T的控件
///
///
///
///
公共静态列表FindControlSoftType(此控件根)
{
列表控件查找=新列表();
if(typeof(T).IsInstanceOfType(ctlRoot))
controlsFound.Add((T)(object)ctlRoot);
foreach(ctlRoot.Controls中的控件ctlTemp)
{
controlsFound.AddRange(FindControlSoftType(CTLTEM));
}
返回控制基金;
}
您遇到的一个问题是div
与任何标准ASP.NET web控件都不对应,因此必须在搜索中使用HtmlGenericControl
。因此,搜索将拾取任何HtmlGenericControl
,而不仅仅是div
元素。因此,您必须查看每个控件的标记名
属性,以确保它确实是div
另外,请注意,在服务器端执行此操作将意味着您仅检查服务器上
mainDiv
控件的内容和顺序。如果您在客户端更改div的顺序,例如使用jQuery draggable,服务器将不会知道这一点,除非您使用额外的javascript发回新的div
顺序。我编写了以下实用方法,您可以将其放入“实用工具带”类或其他类中。您可以像这样使用它:mainDiv.FindControlsOfType()
来返回控件列表
/// <summary>
/// Given a root Control, do a recursive search for Controls implementing or deriving from class/interface T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ctlRoot"></param>
/// <returns></returns>
public static List<T> FindControlsOfType<T>(this Control ctlRoot)
{
List<T> controlsFound = new List<T>();
if (typeof(T).IsInstanceOfType(ctlRoot))
controlsFound.Add((T)(object)ctlRoot);
foreach (Control ctlTemp in ctlRoot.Controls)
{
controlsFound.AddRange(FindControlsOfType<T>(ctlTemp));
}
return controlsFound;
}
//
///给定根控件,递归搜索实现或派生自类/接口T的控件
///
///
///
///
公共静态列表FindControlSoftType(此控件根)
{
列表控件查找=新列表();
if(typeof(T).IsInstanceOfType(ctlRoot))
controlsFound.Add((T)(object)ctlRoot);
foreach(ctlRoot.Controls中的控件ctlTemp)
{
controlsFound.AddRange(FindControlSoftType(CTLTEM));
}
返回控制基金;
}
您遇到的一个问题是div
与任何标准ASP.NET web控件都不对应,因此必须在搜索中使用HtmlGenericControl
。因此,搜索将拾取任何HtmlGenericControl
,而不仅仅是div
元素。因此,您必须查看每个控件的标记名
属性,以确保它确实是div
另外,请注意,在服务器端执行此操作将意味着您仅检查服务器上
mainDiv
控件的内容和顺序。如果您在客户端更改div的顺序,例如使用jQuery draggable,服务器将不会知道,除非您使用额外的javascript发回新的div
顺序。如果您只需要div id:
foreach (Control c in mainDiv.Controls)
{
if (c is HtmlGenericControl)
{
String id = c.ID;
}
}
如果您只需要div id:
foreach (Control c in mainDiv.Controls)
{
if (c is HtmlGenericControl)
{
String id = c.ID;
}
}
您可以添加
btnHelloWorld\u Click
功能吗?您可以添加btnHelloWorld\u Click
功能吗?是否有方法更改c作为div的属性?您的意思是什么?如果你修改c,你将修改相应的div。有没有办法将c的属性更改为div?这是什么意思?如果您修改c,您将修改相应的div。如果现在已关闭,请标记已回答的问题。谢谢。如果现在已关闭,请标记已回答的问题。谢谢