c# - 在服务器端运行时查找div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14017711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find div at run time at server side in c#
提问by Ankur Monga
Hye all.
大家好。
i have div in index.aspx page like this
我在 index.aspx 页面中有这样的 div
<div id="MainDiv" runat="server">
and i am creating div from server side(At run time) like
我正在从服务器端(在运行时)创建 div,例如
for (Int32 i = 0; i < 4; i++)
{
//Create here divFinal
HtmlGenericControl divFinal = new HtmlGenericControl("div");
divFinal.ID = i.ToString();
divFinal.Attributes.Add("class", "column");
mainDiv.Controls.Add(divFinal);
//add to maindiv
HtmlGenericControl div = new HtmlGenericControl("div");
div.ID = "t_e_" + i.ToString() + "_a";
div.Style["background-color"] = "#CFD8E6";
div.Attributes.Add("class", "grid");
div.Attributes.Add("onclick", "OnMouseDownDiv(this)");
div.Attributes.Add("onmouseover", "OnMouseDown(this)");
div.Attributes.Add("onmouseout", "OnMouseUp(this)");
divFinal.Controls.Add(div);
// add to dvfinal
}
After gen rate its look like this in HTML form
在生成率之后,它在 HTML 表单中看起来像这样
<div id="mainDiv"><div id="0" class="column"><div id="t_e_0_a"></div></div><div id="1" class="column"><div id="t_e_1_a"></div></div></div>
Now I need to find div id t_e_0_a inside main Divdiv.
现在我需要在主 Divdiv 中找到 div id t_e_0_a。
HtmlGenericControl div =
((HtmlGenericControl)showdiv.FindControl("0"));
But its give me error....
但它给了我错误....
采纳答案by Viktor S.
You can't do it like you want as it is not a control. You should place runat="server"
attribute on it, or you can get it somehow from showdiv.InnerHtml
- there it should be presented as a string which you can parse with some HTML parser for .net (for instance, HTMLAgilityPacksuggested here)
你不能随心所欲,因为它不是一个控件。你应该把runat="server"
它的属性,也可以某种方式得到它showdiv.InnerHtml
-那里它应作为您可以与一些HTML解析器解析为.NET字符串(例如,HTMLAgilityPack建议在这里)
To create server side controls at run time, you can use something like below:
要在运行时创建服务器端控件,您可以使用以下内容:
for (Int32 i = 0; i < 2; i++)
{
HtmlGenericControl div = new HtmlGenericControl("div");
div.ID = i.ToString();
div.InnerHtml = i.ToString();
div.ClientIDMode = ClientIDMode.Static; //this is for .NET 4.5 only. Required to have ClientID the same as ID.
showdiv.Controls.Add(div);
}
and than, after controls are added, you should be able to use something like this:
然后,在添加控件之后,您应该能够使用如下内容:
HtmlGenericControl div=((HtmlGenericControl)showdiv.FindControl("1"))
to get those controls. But please remember that controls added like that, must be added for each request.
获得这些控件。但是请记住,像这样添加的控件必须为每个请求添加。
回答by Hiren Desai
you should have tried with recursive function that how Page.FindControl
works.
你应该尝试过如何Page.FindControl
工作的递归函数。
private Control getFollowingControl(Control c, string key,out Control returnControl)
{
if(c.hasChild)
{
foreach(Control item in c.controls)
{
getFollowingControl(item,key,out returnControl);
}
}
else
{
if(c.Id==key)
{
returnControl=c;
break;
}
}
}
Now you can use above recusive function to find any control at any depth....
现在您可以使用上述递归函数来查找任何深度的任何控件....
Control getThisControl=null;
getFllowingControl(this,"myButton1",out getThisControl);
At the end it would give you the control that has Id="myButton1" in GetThisControl control object.
最后,它会为您提供在 GetThisControl 控件对象中具有 Id="myButton1" 的控件。