Search code examples
c#asp.netsqldata-bindingweb-controls

C# Binding Data to Text Box


I am trying to bind a database row to a text box in C# so it is the default value (but the text box can still be edited).

The current issue i am having is that the text box will not let me attach a data source with the error (System.Web.UI.WebControls.TextBox does not contain a definition for 'DataSource'....)

I am able to succesfully bind the datasource to a drop down list, but using the same code for a text box does not work.

        txtBox1.DataSource = "DataSource";
        txtBox1.DataBind();

Solution

  • The reason for that is that a DataSource contains multiple rows. A drop down can support that, as it will show one entry per row, but a TextBox doesn't support it. There is only one text field, which row from the data source should it use?

    To set the text of a TextBox use the Text property.