Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to alter div styles using button clicks in C# is to add an event handler to each button click event. Within the event handler, you can access the div element using its ID and use the Style property to modify its CSS styles. For example:

protected void Button1_Click(object sender, EventArgs e)
{
    // get the div element by its ID
    HtmlGenericControl myDiv = (HtmlGenericControl)FindControl("myDivID");

    // modify the div's styles
    myDiv.Style.Add("color", "red");
    myDiv.Style.Add("font-size", "24px");
}

protected void Button2_Click(object sender, EventArgs e)
{
    // get the div element by its ID
    HtmlGenericControl myDiv = (HtmlGenericControl)FindControl("myDivID");

    // modify the div's styles
    myDiv.Style.Add("color", "blue");
    myDiv.Style.Add("font-size", "16px");
}

In this example, two buttons with IDs "Button1" and "Button2" are defined in an ASP.NET page. The corresponding event handlers are defined in the code-behind file (C#). Each event handler gets the div element by its ID ("myDivID") using the FindControl method and modifies its styles using the Style property. When the buttons are clicked, the styles of the div element are changed accordingly.