Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Databinding to labels and textboxes

    Hello, I am new to VS, ASP.NET and sql server. I am a php programmer that worked out of Dreamweaver on mysql DBs.


    What I am trying to do is make a webcontrol that hooks to my DB and fills in labels' text property with the query results. I found a tutorial online that uses a SqlDataSource (which has a connection string and a SelectCommand) then you drop a DataList on the form and inside that you have a ItemTemplate.

    Then inside the labels you would have something like this

    Code:
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString2 %>" SelectCommand="SELECT UserName from Users Where TaskID = 2"></asp:SqlDataSource>
    <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Style="position: relative">
       <ItemTemplate>
          <asp:Label ID="lbl_assignedto" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>	
       </ItemTemplate>
    </asp:DataList>
    This works and when I run it, it shows my UserName. But if I want to change the Text property in my code behind file, there is no 'lbl_assignedto' it does not seem to exist. I am not sure this is the best method for showing info on the page from a DB but the info is not something that repeats. So I did not want to use a datagrid. It holds a bunch of misc labels that give the user info based on the specific TaskID they are looking at.

    If there is a better way, please let me know. As of now, I figured out that if I take my label outside of the ItemTemplate tag, I can access its properties in my code behind file. So it has something to do with how I am trying to access it in the ItemTemplate.

    Thank you for your time and any help you can give me on this matter.
    Last edited by tripwater; 05-21-2007 at 05:23 PM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    This should be moved to ASP.NET forum for you to get helpful answers. Ask the mods to do it.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    I always have some type of data handling going on, I'm not a fan of databinding, although for your situation the issue you are running into is that the label is only visible through the data list. In short, what you need to do is loop through all of the items in the list until you find the one you are looking for....quick and dirty like:
    Code:
    // label
    Label lbl = null;
    
    // loop through each row in the datalist
    foreach (DataListItem dli in DataList1.Items)
    {
        // try to get a reference to the control
       lbl = (Label)dli.FindControl("lbl_assignedto");
    }
    
    // found it now assign something else to it
    if (lbl != null)
    {
        lbl.Text = "yaks";
    }
    Ref to the DataList members page:
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist_members(VS.80).aspx

    Good luck.

  • #4
    Regular Coder
    Join Date
    May 2007
    Location
    UK
    Posts
    180
    Thanks
    0
    Thanked 18 Times in 18 Posts
    If you are using ASP.NET 2.0 and you only want to read or write a single row then it is easier and more appropriate to use a FormView control rather than a DataList. Accessing your Label control within the ItemTemplate is then easy.

    If you are using ASP.NET 1.0 or 1.1 then databinding is not necessarily the best option, as Brandoe85 implied.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •