I have some 20 labels in .aspx page, when i run my stored procedure i get datatable filled with two columns and 20 rows (same as the number of labels). The two column names are: 1) ID, 2) Data
Now what I need is, first row "Data" (column in DataTable) value should be displayed as first labels text, second row value should be the second labels text and so on till 20 rows.
List<MyData> data = new List<MyData>
{
new MyData{ID=1, Data="One"},
new MyData{ID=2, Data="Two"},
new MyData{ID=3, Data="Three"}
};
Label[] labels = MyPlaceholder.Controls.OfType<Label>().ToArray();
for (int i = 0; i < data.Count; i++)
{
labels[i].Text = data[i].Data;
}
In the above I'm using a List of this class
public class MyData
{
public int ID { get; set; }
public string Data { get; set; }
}
but you just substitute for your own data. The important thing is the OfType command to return all of the labels in a certain container. Once you have that, just iterate your own data and update the corresponding label.
I get error: Object reference not set to an instance of an object when I am assigning values to existing labels..
Dim i As Integer = 0
For Each dr As DataRow In DataTableName.Rows
TryCast(Me.Page.FindControl("Label" & (i + 1)), Label).Text = dr("PTSensoryMotor").ToString()
//The error is thrown here
i += 1
Next
public void bindlabel()
{
SqlDataAdapter da = new SqlDataAdapter("select * from test1", con);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
Label lbl = new Label();
lbl.ID = "lbl" + i;
lbl.Text = dt.Rows[i][0].ToString();
Anand Reddy
Member
8 Points
66 Posts
How to bind data to labels programmatically
Apr 29, 2012 05:54 PM|LINK
Hi,
I have some 20 labels in .aspx page, when i run my stored procedure i get datatable filled with two columns and 20 rows (same as the number of labels). The two column names are: 1) ID, 2) Data
Now what I need is, first row "Data" (column in DataTable) value should be displayed as first labels text, second row value should be the second labels text and so on till 20 rows.
Can any one explain me how to do this..
Thanks & regards,
Anand
AidyF
Star
9184 Points
1570 Posts
Re: How to bind data to labels programmatically
Apr 29, 2012 06:25 PM|LINK
ASPX
Code
List<MyData> data = new List<MyData> { new MyData{ID=1, Data="One"}, new MyData{ID=2, Data="Two"}, new MyData{ID=3, Data="Three"} }; Label[] labels = MyPlaceholder.Controls.OfType<Label>().ToArray(); for (int i = 0; i < data.Count; i++) { labels[i].Text = data[i].Data; }In the above I'm using a List of this class
public class MyData { public int ID { get; set; } public string Data { get; set; } }but you just substitute for your own data. The important thing is the OfType command to return all of the labels in a certain container. Once you have that, just iterate your own data and update the corresponding label.
Anand Reddy
Member
8 Points
66 Posts
Re: How to bind data to labels programmatically
Apr 29, 2012 07:17 PM|LINK
Thanks for reply,
sample code which you gave works fine, but What I need is, as I told that I have 20 labels with id Label1, Label2, and so on till Label20..
I get DataTable with 20 rows with columns "ID" and "Data", now based on the column "ID", I should bind the "Data" column to label.
for example: if ID=1, then I need to bind "Data" of id = 1 to Label1, If ID=2, label2.Text = <"Data" of id=2> and so on..
AidyF
Star
9184 Points
1570 Posts
Re: How to bind data to labels programmatically
Apr 29, 2012 07:30 PM|LINK
Just use FindControl to dynamically find the corresponding label
foreach (MyData d in data) { Label l = (Label) FindControl("Label" + d.ID.ToString()); if (l != null) l.Text = d.Data; }Anand Reddy
Member
8 Points
66 Posts
Re: How to bind data to labels programmatically
May 01, 2012 05:21 AM|LINK
I get error: Object reference not set to an instance of an object when I am assigning values to existing labels..
Dim i As Integer = 0
For Each dr As DataRow In DataTableName.Rows
TryCast(Me.Page.FindControl("Label" & (i + 1)), Label).Text = dr("PTSensoryMotor").ToString() //The error is thrown here
i += 1
Next
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: How to bind data to labels programmatically
May 01, 2012 06:18 AM|LINK
try this
string lbl = ((Label)(this.Page.FindControl(("Label" + (i + 1))))).Text;
then
lbl.text = dr("PTSensoryMotor").ToString()
Akhand Prata...
Member
6 Points
11 Posts
Re: How to bind data to labels programmatically
May 01, 2012 07:01 AM|LINK
Hi
Try This code
public void bindlabel()
{
SqlDataAdapter da = new SqlDataAdapter("select * from test1", con);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
Label lbl = new Label();
lbl.ID = "lbl" + i;
lbl.Text = dt.Rows[i][0].ToString();
this.form1.Controls.Add(lbl);
}
}
Plz Mark as Answer
Anand Reddy
Member
8 Points
66 Posts
Re: How to bind data to labels programmatically
May 01, 2012 10:32 AM|LINK
hi,
Your code works fine but, you are creating new labels and adding it form. I don't want to create new labels, I already have labels with id's,
Label1, Label2, Label3, and so on...
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: How to bind data to labels programmatically
May 01, 2012 10:35 AM|LINK
srry anand have you tried my code or not?
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: How to bind data to labels programmatically
May 01, 2012 10:38 AM|LINK
Refer
http://www.aspsnippets.com/Articles/DataBinding-DropDownList-Label-and-Textbox-Controls-in-ASP.Net.aspx
Contact me