본문 바로가기

.NET/ASP.NET

[asp.net] 코드 비하인드에서 동적으로 CSS 바꾸기 / System.Web.UI.AttributeCollection

반응형

일단 aspx 코드창에

<link id="MyStyleSheet" rel="stylesheet" type="text/css"  /></head>
라고 css가 정의 되어있다고 보면




여기서 HTML 코드는 

똑똑한 인텔리센스가 코드 비하인드에서 나오질 않는다

하지만 runat="server" 를 붙이면 서버 컨트롤로 인식되어 인텔리센스에도 나타난다



 아래와같이 runat="server"를 추가한다
 <link id="MyStyleSheet" rel="stylesheet" type="text/css" runat="server" />


runat="server"를 붙이면 이 HTML 태그는

서버컨트롤로 작동된다


그러고 난다음 코드비하인드에는


MyStyleSheet.Attributes.Add("href","/css/flostyle.css")

이렇게 하면 동적으로 바꿀수 있다







Attributes를 이용한 예를 보면 다음과 같다



btnEdit.Attributes.Add("onclick", "alert('수정되었습니다');");

DropDownList3.Items[i].Attributes.Add("style", "background-color:" +        DropDownList3.Items[i].Value + ";");

DropDownList1.Attributes.Add("OnSelectedIndexChanged", "location.reload()")


Label1.Attributes.Add("style", "color:" + HiddenField6.Value + ";");


TextBox1.Attributes.Add("onkeypress", "if(event.keyCode == 13) { " +
            Page.ClientScript.GetPostBackEventReference(ImageButton99, "") + "; return false;}");


((TextBox)LoginView1.FindControl("txtPassword")).Attributes["onkeypress"] = "if (event.keyCode == 13)..


e.Row.Cells[8].Attributes["onmouseover"] = "this.style.cursor='pointer'";


tx.Attributes["onkeyup"] = "javascript:answerLengthCheck();"






AttributeCollection 클래스

ASP.NET 서버 컨트롤 요소의 여는 태그에 선언된 모든 특성에 대한 개체 모델 액세스를 제공합니다.

 이 클래스는 상속될 수 없습니다.

네임스페이스: System.Web.UI
어셈블리: System.Web(system.web.dll)