Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > How to bind a textbox to an a data source

Reply
Thread Tools

How to bind a textbox to an a data source

 
 
Morris Neuman
Guest
Posts: n/a
 
      04-28-2007
Hi,

I want to bind a textbox (not in a gridview or detailsview or formview),
just a regular textbox to a field in an AccessDataSource or SQLDataSource.

How can I do that?
--
Thanks
Morris
 
Reply With Quote
 
 
 
 
Tim Van Wassenhove
Guest
Posts: n/a
 
      04-28-2007
Morris Neuman schreef:
> Hi,
>
> I want to bind a textbox (not in a gridview or detailsview or formview),
> just a regular textbox to a field in an AccessDataSource or SQLDataSource.


Exactly what is an AccessDataSource?

Under the assumption that you have class (eg: class A and an instance a)
with a property (eg: B) you can bind to the Text property of a Textbox
(eg: c) as following:

c.DataBindings.Add("Text", a, "B");

If you create a datasource in the designer, you can also set this via
the properties window -> DataBindings -> Text

--
Tim Van Wassenhove <url:http://www.timvw.be/>
 
Reply With Quote
 
 
 
 
Riki
Guest
Posts: n/a
 
      04-29-2007
Morris Neuman wrote:
> Hi,
>
> I want to bind a textbox (not in a gridview or detailsview or
> formview), just a regular textbox to a field in an AccessDataSource
> or SQLDataSource.
>
> How can I do that?


The easiest way is still to put that textbox in the ItemTemplate of a
FormView,
and binding the FormView to the datasource.

--

Riki


 
Reply With Quote
 
Morris Neuman
Guest
Posts: n/a
 
      04-29-2007
Thanks. Can the Textbox in the itemtemplate be bound to a datasource
differnt from the one bound to the FornView? I have done that with
Dropdownlist control in a itemtemplate of a formview or gridview but can't
seem to do that with a textbox control.

Any suggestions?
--
Thanks
Morris


"Riki" wrote:

> Morris Neuman wrote:
> > Hi,
> >
> > I want to bind a textbox (not in a gridview or detailsview or
> > formview), just a regular textbox to a field in an AccessDataSource
> > or SQLDataSource.
> >
> > How can I do that?

>
> The easiest way is still to put that textbox in the ItemTemplate of a
> FormView,
> and binding the FormView to the datasource.
>
> --
>
> Riki
>
>
>

 
Reply With Quote
 
Riki
Guest
Posts: n/a
 
      04-29-2007
Morris Neuman wrote:
> Thanks. Can the Textbox in the itemtemplate be bound to a datasource
> differnt from the one bound to the FornView? I have done that with
> Dropdownlist control in a itemtemplate of a formview or gridview but
> can't seem to do that with a textbox control.


That's because a dropdownlist is a list control.

Why do you want the textbox to have its own datasourcecontrol?

Just bind it inside the itemtemplate to the data of the formview, and you're
done.

--

Riki


 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-30-2007
Hi Morris,

For TextBox control, it is a simple databound webcontrol, you can simply
bind its property with an object/value or function e.g.

<asp:TextBox .... Text=<%# function or object %> />

For DataSource control, it is designed to support template based databound
control or list databound control rather, so those simple controls like
Label , Textbox doesn't directly support binding with it.

Would you provide some further information about the scenario in your page?
So far according to your description, I think it may be better that we use
code to programmatically retrieve value and assign it to textbox(if it is
already in a template control which bound to another datasource).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
Morris Neuman
Guest
Posts: n/a
 
      04-30-2007
OK. I have 2 different scenario.

Scenario 1.
I have a formview that I use to insert records. It adds the records to
mailbox table (Access or SQL). So I have the formview bound to either
AccessDataSource or SQLDataSource controls.

I also have a table (mailboxdefault) that has default values for some of the
mailbox fields. These can be changed by the system administrator.

What I am trying to do is the following:
When the formview control is displayed in insert mode, I want the fields to
display the values from the mailboxdefault, allowing the user to change them
if desired or leave the defaults as is, and then add the record to the
mailbox table.

Can I bind the textbox control in the inserttemplate to a different data
source than the one use to bind the formview?


Scenario 2.
Can I bind a regular textbox control (not in a formview or gridview, etc) to
a datasource so I can display a value from a table?

Thanks to everyone for your help.
--
Thanks
Morris


"Steven Cheng[MSFT]" wrote:

> Hi Morris,
>
> For TextBox control, it is a simple databound webcontrol, you can simply
> bind its property with an object/value or function e.g.
>
> <asp:TextBox .... Text=<%# function or object %> />
>
> For DataSource control, it is designed to support template based databound
> control or list databound control rather, so those simple controls like
> Label , Textbox doesn't directly support binding with it.
>
> Would you provide some further information about the scenario in your page?
> So far according to your description, I think it may be better that we use
> code to programmatically retrieve value and assign it to textbox(if it is
> already in a template control which bound to another datasource).
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

 
Reply With Quote
 
Riki
Guest
Posts: n/a
 
      04-30-2007
Morris Neuman wrote:
> OK. I have 2 different scenario.
>
> Scenario 1.
> I have a formview that I use to insert records. It adds the records
> to mailbox table (Access or SQL). So I have the formview bound to
> either AccessDataSource or SQLDataSource controls.
>
> I also have a table (mailboxdefault) that has default values for some
> of the mailbox fields. These can be changed by the system
> administrator.
>
> What I am trying to do is the following:
> When the formview control is displayed in insert mode, I want the
> fields to display the values from the mailboxdefault, allowing the
> user to change them if desired or leave the defaults as is, and then
> add the record to the mailbox table.
>
> Can I bind the textbox control in the inserttemplate to a different
> data source than the one use to bind the formview?


Since you're using the formview only for inserting, you can configure
the datasourcecontrol as follows:
1) create the datasource with the wizard in Visual Studio. Use the mailbox
table. This will create the right InsertCommand value for what you want, but
the SelectCommand will still point to the mailbox table, which is not what
you want.
2) modify the SelectCommand in order to select the default values from the
mailboxdefault table.

This will produce what you want.

> Scenario 2.
> Can I bind a regular textbox control (not in a formview or gridview,
> etc) to a datasource so I can display a value from a table?


Yes, but not to a datasourcecontrol (as far as I know).
You can write some code to databind your textbox when the page loads.

--

Riki


 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      05-01-2007
Thanks for your reply Morris and thanks for Riki's further suggestion.

For scenario1, I agree with Riki that if the FormView is used for inserting
item only, you can consider manually change the "SelectStatement" of the
datasource control so as to let it return values from the "DefaultMailBox"
table. However, if your FormView will be used to both insert and
update/display items, I'm afraid you have to manually use code to
programmatically retrieve the default values from database and bind it to
the textboxes in Inserttemplate(in PreRender event or any other place that
can access the loaded controls in InsertTemplate).

For scenario2, since TextBox can not directly bind to datasource control, I
suggest you consider define some codebehind helper functions that retrieve
the proper value(from database or ...) and use those helper function in
your databinding expression. e.g.

<asp:TextBox ..... Text="<%# GetValueFromHelper() %>" />....

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
Morris Neuman
Guest
Posts: n/a
 
      05-01-2007
Thanks to everyone for all the help. I tried as suggested by Riki and
retrieved the default fields in the select statement used to bind the
formview.

Then I changed the field in the formview that I want to display the default
value, to a template field. I bound the item template to the default value
but the value does not show on the screen. I also bound the insert template
to the default value but that did not work either.

Question: If I want to display on the screen the mailboxdefault value but
insert to the mailbox record, which template should I use for the display and
which template should I use for the insert? I am only displaying the formview
in insert mode when the user clicks on the add mailbox link button.
--
Thanks
Morris


"Steven Cheng[MSFT]" wrote:

> Thanks for your reply Morris and thanks for Riki's further suggestion.
>
> For scenario1, I agree with Riki that if the FormView is used for inserting
> item only, you can consider manually change the "SelectStatement" of the
> datasource control so as to let it return values from the "DefaultMailBox"
> table. However, if your FormView will be used to both insert and
> update/display items, I'm afraid you have to manually use code to
> programmatically retrieve the default values from database and bind it to
> the textboxes in Inserttemplate(in PreRender event or any other place that
> can access the loaded controls in InsertTemplate).
>
> For scenario2, since TextBox can not directly bind to datasource control, I
> suggest you consider define some codebehind helper functions that retrieve
> the proper value(from database or ...) and use those helper function in
> your databinding expression. e.g.
>
> <asp:TextBox ..... Text="<%# GetValueFromHelper() %>" />....
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to bind a textbox to a data source Fred ASP .Net Web Controls 1 05-05-2007 01:14 PM
Using a data-bind dropdownlist to populate another data-bind dropdownlist mr2_93 ASP .Net 1 10-02-2005 05:07 PM
Data Bind for textbox =?Utf-8?B?ZGF2aWQ=?= ASP .Net 3 04-03-2005 11:55 PM
Can we bind data to textbox in a headertemplate of a datagrid Vijaya ASP .Net 2 11-13-2003 06:42 AM



Advertisments