View Full Version : Databinding to labels and textboxes

05-21-2007, 05:21 PM
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

<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">
<asp:Label ID="lbl_assignedto" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>

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.

05-21-2007, 10:33 PM
This should be moved to ASP.NET forum for you to get helpful answers. Ask the mods to do it.

05-23-2007, 04:30 PM
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:

// 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 (http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist_members%28VS.80%29.aspx)

Good luck.

05-27-2007, 07:47 PM
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.