Solved

TextBox Data Binding source c# aspx

Posted on 2006-11-10
13
1,606 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
ID: 17915780
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
ID: 17915800
or bind direct to the datatable :

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

Author Comment

by:SylvainBissonnette
ID: 17916326
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:VovinE
ID: 17916621
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
ID: 17916802
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
ID: 17916841
Oh, sorry.
I did not realise you were using a asp.net textbox, I thought it was a winforms textbox.
0
 

Author Comment

by:SylvainBissonnette
ID: 17927586
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
ID: 17928286
<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
ID: 17928299
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
ID: 17931033
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
ID: 17931256
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
ID: 17931332
Me again,

   Where I have to put the  AutoWireEvents?

Sylvain
0
 
LVL 6

Expert Comment

by:VovinE
ID: 17933075
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

685 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