I made a user control, and I wanted to add a ctl_Width property and apply the value to all the contained sub-controls.
But setting the “Width” property of each did not work – I had to change the style of each. So I made some functions to help me in setting the style attribute of each of the sub controls contained in my user control. Here are the functions:
' ------------------------------------------------------------------------------
' purpose - set some style at the server side
' how to call: iRet = SetStyleValue(ddlCombo, "width", sWidth, strError)
' ref: http://codebetter.com/blogs/brendan.tompkins/archive/2003/10/23/2827.aspx
Public Shared Function SetStyleValue(ByVal o As System.Web.UI.WebControls.WebControl, ByVal sKey As String, ByVal sVal As String, Optional ByRef strError As String = "") As Integer
Try
o.Style.Add(sKey, sVal)
Return 1
Catch ex As Exception
strError = ex.Message
Return -666
End Try
End Function
' *********************************************************************
' purpose - get the current style string for the control
' ref:http://codebetter.com/blogs/brendan.tompkins/archive/2003/10/23/2827.aspx
' how to call:
Public Shared Function GetStyleString(ByVal o As System.Web.UI.WebControls.WebControl) As String
Dim strStyle As String = ""
Try
Dim S As System.Web.UI.CssStyleCollection = o.Style
For Each key As String In S.Keys
strStyle += key + ":" + S(key) + ";"
Next
Catch ex As Exception
strStyle = "ERROR"
End Try
Return strStyle
End Function