text field with prepopulated text

I have a web form, there is an address text field, for my purpose, I only need it into this one field for street address city, state, zip.

I would like to have something like street address city, state, zip in the text box when page load, but when the user click the text field, it goes blank, so the user can enter their own address.

How can I do this?

Thanks

0
anncao
2/12/2009 9:33:31 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

5 Replies
739 Views

Similar Articles

[PageSpeed] 39

Hi,

this can be done by prefilling it in the page_load for example and then use javascript onfocus event to clear out the value of the textbox. Another way to go is to use the Textboxwatermark extender from the ASP.NET AJAX Control toolkit.


Read my blog. Handy Firefox plugins for web developers.


Workaround for non working Mark as answer buttons.
0
XIII
2/12/2009 9:44:23 PM

anncao:
I would like to have something like street address city, state, zip in the text box when page load,
 

 

Here's an example using the ADO.NET way with DataTable:

C#

private void GetData(string user)
{
    DataTable dt = new DataTable();
    SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
    connection.Open();
    SqlCommand sqlCmd = new SqlCommand("SELECT * from TABLE1 WHERE UserID = @username", connection);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

    sqlCmd.Parameters.AddWithValue("@username",user);
    sqlDa.Fill(dt);
    if (dt.Rows.Count > 0)
    {
           TextBox1.Text = dt.Rows[0]["ColumnName1"].ToString(); //Where ColumnName is the Field from the DB that you want to display
           TextBox2.Text = dt.Rows[0]["ColumnName2"].ToString();
           Label1.Text = dt.Rows[0]["ColumnName3"].ToString();
           Label2.Text = dt.Rows[0]["ColumnName4"].ToString();
    }
        connection.Close();
}

protected void Page_Load(object sender, EventArgs e)
{

   if (!Page.IsPostBack){
    GetData(this.User.Identity.Name);
   }

}

VB.NET 
 
Private Sub GetData(ByVal user As String)
    Dim dt As New DataTable()
    Dim connection As New SqlConnection("YOUR CONNECTION STRING HERE")
    connection.Open()
    Dim sqlCmd As New SqlCommand("SELECT * from TABLE1 WHERE UserID = @username", connection)
    Dim sqlDa As New SqlDataAdapter(sqlCmd)
   
    sqlCmd.Parameters.AddWithValue("@username", user)
    sqlDa.Fill(dt)
    If dt.Rows.Count > 0 Then
        TextBox1.Text = dt.Rows(0)("ColumnName1").ToString()  'Where ColumnName is the Field from the DB that you want to display
        TextBox2.Text = dt.Rows(0)("ColumnName2").ToString()
        Label1.Text = dt.Rows(0)("ColumnName3").ToString()
        Label2.Text = dt.Rows(0)("ColumnName4").ToString()
    End If
    connection.Close()
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Page.IsPostBack Then
        GetData(Me.User.Identity.Name)
    End If
End Sub 
anncao:
but when the user click the text field, it goes blank, so the user can enter their own address.
You can create a JavaScript function that would clears the Text field when clicked on it.. but I would not recommend you on doing so,
because what if a users goes back to that field and edit the info? basically it will clears the text field again and which is not good. What you can do is
create a separate button that would clear the text field instead when clicked on that button.

Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
2/12/2009 9:51:46 PM

Thanks both of you, I will look into this tomorrow.

But I don't understand the :

"Here's an example using the ADO.NET way with DataTable:" part in vinz's note. Is this to my question?

I tried to mark your two post as mark as answer, it is not working.

I tired also copy the url and then mark it, it isnot working either.

Thanks

0
anncao
2/12/2009 10:12:10 PM

anncao:
"Here's an example using the ADO.NET way with DataTable:" part in vinz's note. Is this to my question?

Hi,

Sorry..My bad, I thought you want to populate your text fields with data from DB on Page_Load ...  I misunderstood your question..


Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
2/12/2009 10:50:33 PM
Hi,

anncao:
I tried to mark your two post as mark as answer, it is not working.
 

Currently there's a bug in the forums software that prevents this in certain occasions. I found a workaround for it which you can read here: Workaround for non working Mark as answer buttons.

Grz, Kris.


Read my blog. Handy Firefox plugins for web developers.


Workaround for non working Mark as answer buttons.
0
XIII
2/13/2009 6:20:54 AM
Reply: