Solved

TextBox Data Binding source c# aspx

Posted on 2006-11-10
13
1,593 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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