Question

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();
Was it helpful?

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.

OTHER TIPS

If you want to set a Value to the text box what you need to do is :

textBox1.Text= "MyText";

then the user can change that value and on the postback you use that value for whatever you need and the user

You can't bind a DataSource to a TextBox in WebForms. You can set the text to a field in a DataRow using the indexer.

`txtBox1.Text = datarow["SomeField"]`
DataView dv = DataSource.DefaultView;

dv.RowFilter = "id=1"; // or whatever as per your need to make sure only 1 row is there
textBox1.DataBindings.Add("Text",dv,"id");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top