smith269
smith269

Reputation: 135

Accessing a div tag from codebehind

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

Answers (7)

Tuk419200
Tuk419200

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

Priya.M
Priya.M

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

jero
jero

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

coder
coder

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

Konstantin Dinev
Konstantin Dinev

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

nunespascal
nunespascal

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

Jon P
Jon P

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

Related Questions