|
 |
aspx thread: Binding Data to a textbox
Message #1 by "Sion Edwards" <sion@h...> on Sat, 11 May 2002 12:16:02
|
|
I am having trouble binding data read from a database table to a textbox.
I have no problems doing this with a pull down list. How can this be done?
Thanks
Sion
Message #2 by Feduke Cntr Charles R <FedukeCR@m...> on Mon, 13 May 2002 09:34:06 -0400
|
|
Sion,
What have you tied so far? Are you loading a dataset in the page or
from viewstate? If so, you should be able to do something like...
<asp:TextBox RunAt="server" Text='<%# DataBinder.Eval(Container.DataItem,
"Field_Name") %>' />
I have no idea how the whole DataBinder or Container works, or how
it even knows to reference the dataset you have created, but I do know that
DataBinder.Eval results in some overhead, so use it sparingly.
- Chuck
-----Original Message-----
From: Sion Edwards [mailto:sion@h...]
Sent: Saturday, May 11, 2002 8:16 AM
To: ASP+
Subject: [aspx] Binding Data to a textbox
I am having trouble binding data read from a database table to a textbox.
I have no problems doing this with a pull down list. How can this be done?
Thanks
Sion
Message #3 by "Sion Edwards" <sion@h...> on Mon, 13 May 2002 14:51:36
|
|
Thanks for that but I have another problem
In my datagrid when I edit an item I have created a template so that
Instead of a text box appearing, a drop down list appears. How do I
populate this from a database table?
Thanks
> Sion,
What have you tied so far? Are you loading a dataset in the page
or
from viewstate? If so, you should be able to do something like...
<asp:TextBox RunAt="server" Text='<%# DataBinder.Eval(Container.DataItem,
"Field_Name") %>' />
I have no idea how the whole DataBinder or Container works, or how
it even knows to reference the dataset you have created, but I do know that
DataBinder.Eval results in some overhead, so use it sparingly.
- Chuck
-----Original Message-----
From: Sion Edwards [mailto:sion@h...]
Sent: Saturday, May 11, 2002 8:16 AM
To: ASP+
Subject: [aspx] Binding Data to a textbox
I am having trouble binding data read from a database table to a textbox.
I have no problems doing this with a pull down list. How can this be done?
Thanks
Sion
Message #4 by Feduke Cntr Charles R <FedukeCR@m...> on Mon, 13 May 2002 10:03:21 -0400
|
|
Sion,
One of the nice things about ADO.NET is that it is data source
independent. This means that you can have an array as a data source, or a
full blow dataset as a data source. It just so happens that a DropDownList
can have a DataSource of type object. So...
// the below assumes you're not using ViewState or Session to cache the data
set
// if you do, use the "if (!Page.IsPostBack)" to populate initially,
// and if (Page.IsPostBack) then read from ViewState or Session instead.
protected System.Web.UI.WebControls.DropDownList someDropDownList = null;
private void Page_Load(object sender, System.EventArgs e)
{
string connectionString = "your connection string";
string sqlSelect = "SELECT some_field FROM some_table ORDER BY
some_field";
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter(sqlSelect, con);
DataSet ds = new DataSet();
da.Fill(ds, "data");
// desired results? Probably not.
someDropDownList.DataSource = da.Tables["data"].Select();
}
That's very rudimentary. You might want to consider writing your
own custom control that inherits from DropDownList and has a
ConnectionString and SqlSelect properties, and populate the control in the
DataBind method/event (at least that's how I've done it here). There are
many different ways to go about doing it. If you'd like to see how I did
it, I'll post the source code.
HTH,
- Chuck
-----Original Message-----
From: Sion Edwards [mailto:sion@h...]
Sent: Monday, May 13, 2002 10:52 AM
To: ASP+
Subject: [aspx] RE: Binding Data to a textbox
Thanks for that but I have another problem
In my datagrid when I edit an item I have created a template so that
Instead of a text box appearing, a drop down list appears. How do I
populate this from a database table?
Thanks
> Sion,
What have you tied so far? Are you loading a dataset in the page
or
from viewstate? If so, you should be able to do something like...
<asp:TextBox RunAt="server" Text='<%# DataBinder.Eval(Container.DataItem,
"Field_Name") %>' />
I have no idea how the whole DataBinder or Container works, or how
it even knows to reference the dataset you have created, but I do know that
DataBinder.Eval results in some overhead, so use it sparingly.
- Chuck
-----Original Message-----
From: Sion Edwards [mailto:sion@h...]
Sent: Saturday, May 11, 2002 8:16 AM
To: ASP+
Subject: [aspx] Binding Data to a textbox
I am having trouble binding data read from a database table to a textbox.
I have no problems doing this with a pull down list. How can this be done?
Thanks
Sion
Message #5 by "Lewis" <lewis@t...> on Mon, 13 May 2002 14:35:04 -0600
|
|
use
textbox.text = dataview(0)("fieldname")
you should check for null values or do type conversion as required
-----Original Message-----
From: Sion Edwards [mailto:sion@h...]
Sent: Saturday, May 11, 2002 12:16 PM
To: ASP+
Subject: [aspx] Binding Data to a textbox
I am having trouble binding data read from a database table to a textbox.
I have no problems doing this with a pull down list. How can this be done?
Thanks
Sion
|
|
 |