Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default values on custom web controls?

Status
Not open for further replies.

adam0101

Programmer
Jun 25, 2002
1,952
US
I'd like to create my own control that inherits from a textbox and has a default value for the Text property. However, I'd also like the developer using my control to be able to override my default value in HTML. The problem is that the text inserted by the developer into the Text property is being overwritten by my default value. I'm setting the default value in the init event. Is this the correct place?

Adam
 
I think you are correct in where you are setting it, but I guess you'll have to check to see if the developer has set a value for the Text property before applying your default value.

Can you post an example of how you are applying this default value?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I'm just doing

Code:
Private Sub MyTextbox_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Init
  Me.Text = "some value"
End Sub
So I guess I could do this...
Code:
Private Sub MyTextbox_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Init
  If Me.Text = "" Then
    Me.Text = "some value"
  End If
End Sub

But then the developer wouldn't be able to set the value to "".

Adam
 
I finally got it. I set the default value in the constructor instead of the init event.
Code:
Public Class MyTextbox
	Inherits System.Web.UI.WebControls.TextBox

	Public Sub New()
		Me.Text = "my default value"
	End Sub

End Class
That way it has a default value, but the developer can still overwrite it in HTML.
Code:
<cc2:mytextbox id="MyTextbox1" runat="server" text="developers default value"></cc2:mytextbox>
I think if this were a composite control, the init event would have been the correct place.

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top