How to make an ASP.NET Web User Control Width editable.

Add a property to your WebUserControl:
Private widthValue As Integer
Public Property Width() As Integer
    Get 
        Return widthValue 
    End Get 
    Set(ByVal value As Integer)
        widthValue = value 
    End Set 
End Property 
Then add a prerender event handler to the WebUserControl, where in this example the MainTable happens to be the outermost container element in the Web User Control with a width attribute:
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    If Me.Width > 0 Then
        Me.MainTable.Width = Me.Width
    End If 
End Sub

Then wherever you use the control you can specify the width if you want to:
<uc2:MyWebUserControl ID="MyWebUserControl1" runat="server" width="650" />

No comments:

Post a Comment