Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1670
  • Last Modified:

TextBox Data Binding source c# aspx

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
SylvainBissonnette
Asked:
SylvainBissonnette
  • 6
  • 4
  • 3
1 Solution
 
ozymandiasCommented:
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
 
ozymandiasCommented:
or bind direct to the datatable :

this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataTable1, "Column1"));
0
 
SylvainBissonnetteAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
VovinECommented:
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
 
SylvainBissonnetteAuthor Commented:
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
 
ozymandiasCommented:
Oh, sorry.
I did not realise you were using a asp.net textbox, I thought it was a winforms textbox.
0
 
SylvainBissonnetteAuthor Commented:
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
 
VovinECommented:
<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
 
VovinECommented:
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
 
SylvainBissonnetteAuthor Commented:
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
 
SylvainBissonnetteAuthor Commented:
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
 
SylvainBissonnetteAuthor Commented:
Me again,

   Where I have to put the  AutoWireEvents?

Sylvain
0
 
VovinECommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now