Reputation: 135
I am Working in asp.net and c#. I have a div
tag in my application with class="something"
.I need to access this something class in codebehind.How can i do that..
Code:
<div class="something">
//somecode
<div>
Note:I want access Something class in codebehind.
Upvotes: 7
Views: 51058
Reputation: 41
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.
For example:
Control myDiv = (Control)FindControl("myDiv");
myDiv.Visible = false;
I found this to be quite effective, hopefully it can help someone else out.
Upvotes: 2
Reputation: 1
protected void Datalist_ItemDataBound(object sender, DataListItemEventArgs e) { System.Web.UI.HtmlControls.HtmlGenericControl div22 = (System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("something"); }
Upvotes: -3
Reputation: 1
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
Upvotes: 0
Reputation: 13248
Give ID
and attribute runat='server'
as :
<div class="something" ID="mydiv" runat="server">
//somecode
<div>
Codebehind:
You can modify your something
styles here.
mydiv.Style.Add("display", "none");
Upvotes: 10
Reputation: 34915
You need the runat='server' attribute and 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
}
Upvotes: 3
Reputation: 17724
Make it a server tag if you want to access it in CS code.
<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.
CS:
something.InnerHtml = "some code";
Upvotes: 4
Reputation: 821
All you can do is to loop through all controls and then check if its CssClass
is something
i.e. if that div is inside a the div with ID Div1
then you will use:
foreach (Control c in Div1.Controls)
{
if (c.CssClass == "something")
{
// do the moves here
}
}
Upvotes: 0