Saba Aslam
Saba Aslam

Reputation: 41

How to change styling of an HTML tag

I am working on a project where i want to change the styling of a <div> using c# in asp.net my html code is

 <div id="xyz" style="display: none" runat="server"> Please Register YourSelf First</div>

and my c# code is

 if (q == 0)
        {

            HtmlGenericControl ul = (HtmlGenericControl)(this.FindControl("xyz"));
            ul.Style["display"] = "block";
        } else { ...}

in which ul is always showing null.. please help

Upvotes: 0

Views: 168

Answers (2)

Zollistic
Zollistic

Reputation: 98

<asp:Panel ID="foo" runat="server"></asp:Panel>    

Then in your code behind....

foo.Attributes.Add("style", "border: 1px solid");

Upvotes: 0

Win
Win

Reputation: 62301

You can use either xyz.Attributes.Add("style", "display: block") or xyz.Attributes["style"] = "display: block".

FYI: You do not need to use FindControl unless xyz is located inside Data controls like Repeater.

<div id="xyz" style="display: none" runat="server">
    Please Register YourSelf First
</div>
<asp:Button runat="server" ID="SubmitButton"
    OnClick="SubmitButton_Click" Text="Submit" />

protected void SubmitButton_Click(object sender, EventArgs e)
{
    xyz.Attributes.Add("style", "display: block");
}

Upvotes: 1

Related Questions