Solved

TextBox Data Binding source c# aspx

Posted on 2006-11-10
13
1,573 Views
Last Modified: 2012-06-21
Hi

    I need to bind a text box from a database table field,  When I drag the TextBox I was supprise that there is no binding proprety.  What can I do? and how?

Thanks
0
Comment
Question by:SylvainBissonnette
  • 6
  • 4
  • 3
13 Comments
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
There is a binding property.
You can either specify the databinding in the databindings perperties in the properties window of the text box or you can add it programmatically:

e.g :

this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet1, "Table1.Column1"));
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
or bind direct to the datatable :

this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataTable1, "Column1"));
0
 

Author Comment

by:SylvainBissonnette
Comment Utility
Hi thanks for your reply,

     After re-verifying there is no binding proprety for the textbox in C# aspx Visual Studio 2005,
Here is the proprety

(Expressions)
(ID)
AccessKey
AutoCompleteType
AutoPostBack
BackColor
BorderColor
BorderStyle
BorderWidth
CausesValidation
Colomns
CssClass
Enavled
EnableTheming
EnableViewState
Font
ForColor
Height
MaxLength
ReadOnly
Rows
SkinID
TabIndex
Text
ToolTip
ValidationGroup
Visible
Width
Wrap

and there is no textBox1.DataBindings  Only TextBox1.DataBind()

Thanks again
Sylvain Bissonnette
0
 
LVL 6

Expert Comment

by:VovinE
Comment Utility
There is no DataBinding for TextBox control in ASP.NET, but you may define your own databinding for the whole page.
The glitch is that you would be required to call Page.DataBind() method to bind your data.

In the aspx code you can use <%# %> tags, to indicate databinding. It can occur anywhere on the aspx in the raw page content, in an attribute or somewhere else.
But just mind that such databinding is one direction only.

hope it helps.
0
 

Author Comment

by:SylvainBissonnette
Comment Utility
I had on my web form 5 dropdown list that I had bind via the proprety window, how could I use my SQLDataSource and by my source code get the result of the select query I had configure via the wizzard? It should have a simple way to bind a simple textbox via a datasource?

Sylvain
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
Oh, sorry.
I did not realise you were using a asp.net textbox, I thought it was a winforms textbox.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:SylvainBissonnette
Comment Utility
Hi,

    Ater 20 hours of work I found that this code work but is there a way to make it directly in the aspx file and nothing in the cs file?

Thanks

    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            DataView dw = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            Value = dw[0]["DeviceName"].ToString();
            DataBind();
        }
    }

<body>
    <form id="form1" runat="server">
        &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
            ProviderName="<%$ ConnectionStrings:ConnectionString2.ProviderName %>" SelectCommand="SELECT * FROM Device WHERE (DeviceID = 350)">
        </asp:SqlDataSource>

        <asp:Label ID="Label1" runat="server" Text='<%# Value %>'></asp:Label>

    </form>
</body>
0
 
LVL 6

Accepted Solution

by:
VovinE earned 125 total points
Comment Utility
<body>
    <form id="form1" runat="server">
        &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
            ProviderName="<%$ ConnectionStrings:ConnectionString2.ProviderName %>" SelectCommand="SELECT * FROM Device WHERE (DeviceID = 350)">
        </asp:SqlDataSource>

        <asp:Label ID="Label1" runat="server" Text='<%# ((DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty))[0]["DeviceName"].ToString() %>'></asp:Label>

    </form>
</body>

plus only DataBind call is needed on the codebehind class (in cs file).
0
 
LVL 6

Expert Comment

by:VovinE
Comment Utility
If don't want anything in the .cs file you may put add <script runat="server"> tag to your aspx file.

<script runat="server">
            private void Page_Load(object sender, System.EventArgs e)
            {
            if (!Page.IsPostBack)
                DataBind();
            }
</script>

to use it make sure AutoWireEvents is set to true.


0
 

Author Comment

by:SylvainBissonnette
Comment Utility
Hi VovinE,

   You are right at 99% it give me a error:
The type or namespace name 'DataView' could not be found (are you missing a using directive or an assembly reference?)      C:\Zip\test\Default.aspx      14

Sylvain      
0
 

Author Comment

by:SylvainBissonnette
Comment Utility
Hi, VovinE,

   I just found the 1% missing I had to put in the top of my aspx file
<%@ Import Namespace="System.Data" %>

Thanks a lot for your knowleage
Sylvain


0
 

Author Comment

by:SylvainBissonnette
Comment Utility
Me again,

   Where I have to put the  AutoWireEvents?

Sylvain
0
 
LVL 6

Expert Comment

by:VovinE
Comment Utility
Sorry, I meant AutoEventWireup at the page directive:

<%@ Page Language="c#" AutoEventWireup="true" Codebehind="default.aspx.cs" Inherits="default"%>

Such directive should be on every aspx page :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now