Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I can't make the Attributes.Add work. Here I'm trying to change the default text from the code but I can't get it to change. I've tried with background-color also. Still no restult.

Anyone have the answere?

Jesper

What I have tried:



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)


BoxFirstname.Attributes.Add("Text", "Test text")


End Sub





<asp:textbox id="BoxFirstname" placeholder="Firstname" runat="server" class="medinput" onkeypress="javascript:login(event);">
Posted
Updated 19-Feb-21 3:57am
Comments
20212a 18-Feb-21 9:17am    
Html textboxes do not have a text property. You use the placeholder property.
F-ES Sitecore 18-Feb-21 9:44am    
Or "value" if they want the text in the box to be changed.
20212a 18-Feb-21 9:52am    
True. But I can't really tell what they are asking.

1 solution

If you want to change the Text property, then set the Text property. But make sure you only do it when the page is first loaded, otherwise you'll overwrite whatever value the user has entered.
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Page.IsPostBack Then
        BoxFirstname.Text = "Test text"
    End If
End Sub
If you want to change the placeholder, then you need to set the placeholder attribute:
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Page.IsPostBack Then
        BoxFirstname.Attributes.Add("placeholder", "Test text")
    End If
End Sub
If you want to set the background colour, you need to modify the style:
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Page.IsPostBack Then
        BoxFirstname.Style.Add("background-color", "#424242")
    End If
End Sub
 
Share this answer
 
Comments
Member 15076984 19-Feb-21 4:18am    
Thanks for answer. I've now added all three to my Page_Load and same result, still no change of text or color. But the weird thing is that I've a button that picks up the value that is entered using .text and it works. Any ideas?
Richard Deeming 19-Feb-21 4:27am    
That doesn't make any sense. Either the page is cached, or you're not looking at the control you're modifying.
Member 15076984 19-Feb-21 4:30am    
True it dosen't. I've just cleared all cookies and the control is picked from the VB suggestions.
Richard Deeming 19-Feb-21 4:34am    
But are you sure it's the same control you're looking at in the browser?

What happens if you set the text directly in the markup?

Could there be something else running on the server or the client that's changing the text after this code runs?
Member 15076984 19-Feb-21 4:44am    
Hi again. I think I'm getting closer it was my page_load that was the problem it's on a ascx page and is not called. I've to change the sub. Thanks for the effort.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900