How to bind a textbox to an a data source

Discussion in 'ASP .Net Web Controls' started by Morris Neuman, Apr 28, 2007.

  1. 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
    Morris Neuman, Apr 28, 2007
    #1
    1. Advertising

  2. 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/>
    Tim Van Wassenhove, Apr 28, 2007
    #2
    1. Advertising

  3. Morris Neuman

    Riki Guest

    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
    Riki, Apr 29, 2007
    #3
  4. 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
    >
    >
    >
    Morris Neuman, Apr 29, 2007
    #4
  5. Morris Neuman

    Riki Guest

    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
    Riki, Apr 29, 2007
    #5
  6. 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.
    Steven Cheng[MSFT], Apr 30, 2007
    #6
  7. 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.
    >
    >
    Morris Neuman, Apr 30, 2007
    #7
  8. Morris Neuman

    Riki Guest

    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
    Riki, Apr 30, 2007
    #8
  9. 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.
    Steven Cheng[MSFT], May 1, 2007
    #9
  10. 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.
    >
    >
    Morris Neuman, May 1, 2007
    #10
  11. Morris Neuman

    Fred Guest

    The problem is that, in VS2003 using web forms, the text control had a
    property - "Databinding" that simplified connecting the control to a
    datasource. It could be done visually during design and was very simple
    to modify.

    In VS2005, I want to do the same thing: I want to add a textbox to a
    form and then bind it to a datasource, and I want to do it visually,
    without having to go to the HTML source and add something like,
    Text='<%# DataBinder.Eval(HelpDeskDV, "[0].ContactName", "{0}") %>'.

    So the question is: how do I assign a datasource to an existing textbox
    on a web form?

    Regards,
    Fred


    *** Sent via Developersdex http://www.developersdex.com ***
    Fred, May 1, 2007
    #11
  12. Hi,

    Any ideas or thoughts on my 5/1 posting?

    Any help is most appreciated.
    --
    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.
    >
    >
    Morris Neuman, May 2, 2007
    #12
  13. Hi Morris,

    I've investigated the FormView databinding model further and find that
    using the built-in "SelectCommand" does not help for this scenario. Since
    FormView will need to use two way databinding, the InsertCommand need to be
    set to the actual database table(you want to insert record). For the
    default table's values, I'm afraid you need to maually assign them to the
    TextBoxes in the "Inserttemplate". e.g.

    =================
    protected void FormView1_PreRender(object sender, EventArgs e)
    {
    if (FormView1.CurrentMode == FormViewMode.Insert)
    {
    TextBox txt1 = FormView1.FindControl("nameTextBox") as TextBox;
    TextBox txt2 = FormView1.FindControl("descriptionTextBox") as
    TextBox;

    //replace default value with the one you retrieve from database
    txt1.Text = "default name..";
    txt2.Text = "default description..";

    }
    }
    ====================

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], May 3, 2007
    #13
  14. Hi Steven,

    Thanks for your response and your help.

    The default value from is a field from a datasource control defined as a
    variable in my web.config AppSettings as MyDataSource. This control can
    point to either an AccessDataSource or SqlDataSource based on web.config.

    I also have the default value as a boundfield in my gridview control,
    however I have iit set to invisible.

    So the question is - in your comment you have
    txt1.Text = "default name..";
    but in that case "default name is a literal. How do I set txt1.Text set to
    the value in either the database source or the boundfield in the gridview.

    Thanks again for your help.


    --
    Thanks
    Morris


    "Steven Cheng[MSFT]" wrote:

    > Hi Morris,
    >
    > I've investigated the FormView databinding model further and find that
    > using the built-in "SelectCommand" does not help for this scenario. Since
    > FormView will need to use two way databinding, the InsertCommand need to be
    > set to the actual database table(you want to insert record). For the
    > default table's values, I'm afraid you need to maually assign them to the
    > TextBoxes in the "Inserttemplate". e.g.
    >
    > =================
    > protected void FormView1_PreRender(object sender, EventArgs e)
    > {
    > if (FormView1.CurrentMode == FormViewMode.Insert)
    > {
    > TextBox txt1 = FormView1.FindControl("nameTextBox") as TextBox;
    > TextBox txt2 = FormView1.FindControl("descriptionTextBox") as
    > TextBox;
    >
    > //replace default value with the one you retrieve from database
    > txt1.Text = "default name..";
    > txt2.Text = "default description..";
    >
    > }
    > }
    > ====================
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    Morris Neuman, May 4, 2007
    #14
  15. Thanks for your reply Morris,

    If you need to get the data field value from DataSource control, you can
    also do it programmatically. ASP.NET 2.0 DataSource control provide method
    to let you explicitly perform the query and get the resultset. e.g.


    =========================
    protected void FormView1_PreRender(object sender, EventArgs e)
    {
    if (FormView1.CurrentMode == FormViewMode.Insert)
    {
    TextBox txt1 = FormView1.FindControl("nameTextBox") as TextBox;
    TextBox txt2 = FormView1.FindControl("descriptionTextBox") as
    TextBox;

    IEnumerable result =
    SqlDataSource2.Select(DataSourceSelectArguments.Empty);
    DataView dv = result as DataView;

    string defaultname = (string)dv[0]["name"];
    string defaultdesc = (string)dv[0]["description"];

    txt1.Text = defaultname;
    txt2.Text = defaultdesc;


    //............

    }
    }
    ===============================

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], May 4, 2007
    #15
  16. Hi Steven,

    Tried the code per your suggestion, however I get the following error:
    The type or namespace name 'DataView' could not be found (are you missing a
    using directive or an assembly reference?
    What do I need to change?

    Also in your post you specify SqlDataSource1. I use either SqlDataSource1
    or AccessDataSource1 based on a variable MyDataSource from the AppSetting
    section of the web.config.
    How would I modify your statement to base selection on the MyDataSource app
    setting?

    Thanks as always for your help.
    --
    Thanks
    Morris


    "Steven Cheng[MSFT]" wrote:

    > Thanks for your reply Morris,
    >
    > If you need to get the data field value from DataSource control, you can
    > also do it programmatically. ASP.NET 2.0 DataSource control provide method
    > to let you explicitly perform the query and get the resultset. e.g.
    >
    >
    > =========================
    > protected void FormView1_PreRender(object sender, EventArgs e)
    > {
    > if (FormView1.CurrentMode == FormViewMode.Insert)
    > {
    > TextBox txt1 = FormView1.FindControl("nameTextBox") as TextBox;
    > TextBox txt2 = FormView1.FindControl("descriptionTextBox") as
    > TextBox;
    >
    > IEnumerable result =
    > SqlDataSource2.Select(DataSourceSelectArguments.Empty);
    > DataView dv = result as DataView;
    >
    > string defaultname = (string)dv[0]["name"];
    > string defaultdesc = (string)dv[0]["description"];
    >
    > txt1.Text = defaultname;
    > txt2.Text = defaultdesc;
    >
    >
    > //............
    >
    > }
    > }
    > ===============================
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    >
    Morris Neuman, May 7, 2007
    #16
  17. Hi Morris,

    As for the DataView class, it is under the System.Data namespace, have you
    imported the namespace in your code behind file?

    For SqlDataSouce and AccessDataSource, they've both implemented the
    "Select" method that can directly perform query in manual code and return
    the queried resultset. Make sure you set "DataSourceMode" of them to
    "DataSet"(the default value) so that the returned object from Selecct
    method would be a DataView object. For detecting whether it is
    SqlDataSource or AccessDataSource, I suggest you typeof() directive (in C#)
    to detect the type (use if...else block) to separate your code logic of the
    datasource control's converting. How do you think?

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], May 8, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vijaya
    Replies:
    2
    Views:
    2,635
    Vijaya
    Nov 13, 2003
  2. =?Utf-8?B?ZGF2aWQ=?=

    Data Bind for textbox

    =?Utf-8?B?ZGF2aWQ=?=, Apr 3, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,782
    Karl Seguin
    Apr 4, 2005
  3. mr2_93
    Replies:
    1
    Views:
    3,587
    mr2_93
    Oct 2, 2005
  4. Henry Lee
    Replies:
    2
    Views:
    488
    Darren Kopp
    Mar 31, 2006
  5. Fred

    How to bind a textbox to a data source

    Fred, May 4, 2007, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    272
Loading...

Share This Page