C# 从代码隐藏访问 div 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14930785/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:28:10  来源:igfitidea点击:

Accessing a div tag from codebehind

c#asp.net

提问by smith269

I am Working in asp.net and c#. I have a divtag in my application with class="something".I need to access this somethingclass in codebehind.How can i do that..

我在 asp.net 和 c# 中工作。我有一个div标签在我的应用程序class="something"。我需要访问这个东西在codebehind.How类我能做到这一点..

Code:

代码

 <div class="something">
//somecode
 <div>

Note:I want access Somethingclass in codebehind.

注意:我想在代码隐藏中访问某些类。

采纳答案by coder

Give IDand attribute runat='server'as :

给出ID和属性runat='server'为:

<div class="something" ID="mydiv" runat="server">
//somecode
<div>

Codebehind:

代码隐藏:

You can modify your somethingstyles here.

您可以something在此处修改样式。

mydiv.Style.Add("display", "none");

回答by Jon P

All you can do is to loop through all controls and then check if its CssClassis something

您所能做的就是遍历所有控件,然后检查它CssClass是否是something

i.e. if that div is inside a the div with ID Div1then you will use:

即,如果该 div 位于带有 ID 的 div 内,Div1那么您将使用:

foreach (Control c in Div1.Controls)
{
    if (c.CssClass == "something")
    {
        // do the moves here
    }
}

回答by nunespascal

Make it a server tag if you want to access it in CS code.

如果您想在 CS 代码中访问它,请将其设为服务器标记。

<div class="something" runat="server" id="something">
//somecode
 <div>

asp.net does not provide a method to search by classes. so you will have to rely on the ID.

asp.net 不提供按类搜索的方法。所以你将不得不依赖ID。

CS:

CS:

something.InnerHtml = "some code";

回答by jero

div tag is for making your page stylish.you can use id & class attributes within div and this id & class name will call on css & js

div 标签是为了让你的页面时尚。你可以在 div 中使用 id 和 class 属性,这个 id 和 class 名称将调用 css 和 js

回答by Konstantin Dinev

You need the runat='server' attribute and ID.

您需要 runat='server' 属性和 ID。

<div class="something" runat="server" id="TestDiv">
//somecode
<div>

Then in code behind:

然后在后面的代码中:

protected void Page_Load(object sender, EventArgs e)
{
    TestDiv.InnerHtml += "test"; // The div is accessible
}

回答by Priya.M

protected void Datalist_ItemDataBound(object sender, DataListItemEventArgs e) { System.Web.UI.HtmlControls.HtmlGenericControl div22 = (System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("something"); }

protected void Datalist_ItemDataBound(object sender, DataListItemEventArgs e) { System.Web.UI.HtmlControls.HtmlGenericControl div22 = (System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("something"); }

回答by Tuk419200

Another solution that may help solve some of the issues encountered by others in the comments of the selected answer would be to use the Control class with the FindControl() method to access the desired element.

另一种可能有助于解决其他人在所选答案的评论中遇到的一些问题的解决方案是使用具有 FindControl() 方法的 Control 类来访问所需的元素。

For example:

例如:

Control myDiv = (Control)FindControl("myDiv");

myDiv.Visible = false;

I found this to be quite effective, hopefully it can help someone else out.

我发现这非常有效,希望它可以帮助其他人。