Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding class attribute from code behind

I have the following code:

div1.Attributes.Add("class", "displayNone");

it works on page load but doesn't on an OnClick event.

This is because my html <div id="div1"></div> seems to change to:

<div id="div1_ucSomeControl_SoemthingElse"></div>

after the page has been rendered.

How can I get around this?

like image 512
Anicho Avatar asked Feb 14 '26 19:02

Anicho


2 Answers

I don't think that the ID of the control matters in this case. You may be running into this issue because the class attribute already exists. Try this instead:

div1.Attributes["class"] = "displayNone";
like image 147
James Johnson Avatar answered Feb 16 '26 09:02

James Johnson


<div id="div1" runat ="server">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>

code behind:

protected void Button1_Click(object sender, EventArgs e)
{
    div1.Attributes.Add("class", "displayNone");
}

this will be work.

like image 25
Göktürk Solmaz Avatar answered Feb 16 '26 07:02

Göktürk Solmaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!