Each label has id Label1, Label2, and so on.. Now I need to display text of each label as:
For Label1 - first row (second column) value as Label1.Text,
Label2 - second row (second column value) as Label2.Text and so on from DataTable which has two columns (ID, textData) programmatically.
protected void Page_Load(object sender, EventArgs e)
{
DataTable table="Your DataTable";
DataRow[] dr = table.Select("ID>=1"); //Presuming the minimal ID=1,This will select all rows in your DataTable.
int i = 1;
foreach (GridViewRow row in GridView1.Rows)
{
((Label)row.FindControl("Label"+i)).Text = dr[i-1]["textData"].ToString();
i++;
}
}
Please mark the replies as answers if they help or unmark if not.
Feedback to us
Anand Reddy
Member
8 Points
67 Posts
Re: How to bind data to labels programmatically
May 01, 2012 10:41 AM|LINK
Yes. But it throwing the same error which i mentioned earlier. Object reference not set to an instance of the object.
string lbl = ((Label)(this.Page.FindControl(("Label" + (i + 1))))).Text; //It throwing error
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: How to bind data to labels programmatically
May 01, 2012 10:43 AM|LINK
can you show me your aspx page please?
Anand Reddy
Member
8 Points
67 Posts
Re: How to bind data to labels programmatically
May 01, 2012 11:02 AM|LINK
how to post an image of my aspx page??
amsneuton
Member
528 Points
121 Posts
Re: How to bind data to labels programmatically
May 01, 2012 11:12 AM|LINK
HI
Why don't you use any databinding control like Grid or repeater.
You can Use Formview control also for custom layout.
Senior Software Engineer
Find latest stuff on
DotnetAdda
Anand Reddy
Member
8 Points
67 Posts
Re: How to bind data to labels programmatically
May 01, 2012 11:14 AM|LINK
This is my aspx source...
I don't know whether you can understand..
</div>
<div style="width:100%">
<div style="float:left;width:30%;">
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlWalk300ft" runat="server">
<asp:ListItem Text="Verbal/Visual/Model"></asp:ListItem>
</asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:30%;">
<asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlWalk600ft" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:30%;">
<asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlWalk5minNS" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:30%;">
<asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlWalk10minNS" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:30%;">
<asp:Label ID="Label9" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlWalk15minNS" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:30%;">
<asp:Label ID="Label10" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlComp5minER" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:30%;">
<asp:Label ID="Label11" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlComp10minER" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:30%;">
<asp:Label ID="Label12" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlComp15minER" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:30%;">
<asp:Label ID="Label13" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlWalksOnTM" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:30%;">
<asp:Label ID="Label14" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlPBorSB" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:30%;">Comments</div>
<div style="float:left;">
<asp:TextBox ID="TextBox6" runat="server" TextMode="MultiLine" Rows="3" Columns="25"></asp:TextBox>
</div>
<div class="clear"></div>
</div>
</div>
<div style="float:left; width:47%;">
<b style="text-decoration:underline;">Transfers</b>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:35%;">
<asp:Label ID="Label15" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlChairtoChair" runat="server">
<asp:ListItem Text="Verbal/Visual/Model"></asp:ListItem>
</asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:35%;">
<asp:Label ID="Label16" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlSittoStand" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:35%;">
<asp:Label ID="Label17" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlStandtoSit" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:35%;">
<asp:Label ID="Label18" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlToilettoChair" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:35%;">
<asp:Label ID="Label19" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlChairtoToilet" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:35%;">
<asp:Label ID="Label20" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlStandtoToilet" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:35%;">
<asp:Label ID="Label21" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlToilettoStand" runat="server"></asp:DropDownList>
</div>
<div class="clear"></div>
</div>
<div class="height20"></div>
<div style="width:100%">
<div style="float:left;width:35%;">
<asp:Label ID="Label22" runat="server" Text="Label"></asp:Label>
</div>
<div style="float:left;">
<asp:DropDownList id="ddlChairtoFloor" runat="server"></asp:DropDownList>
</div>
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: How to bind data to labels programmatically
May 01, 2012 11:25 AM|LINK
as i can see your labels start from 5 to 22 why are you starting from label1?
Anand Reddy
Member
8 Points
67 Posts
Re: How to bind data to labels programmatically
May 01, 2012 11:30 AM|LINK
Actually I didn't send full source... If you want I can send all of my source..
Anand Reddy
Member
8 Points
67 Posts
Re: How to bind data to labels programmatically
May 01, 2012 12:18 PM|LINK
My .aspx page look like below..
Each label has id Label1, Label2, and so on.. Now I need to display text of each label as:
For Label1 - first row (second column) value as Label1.Text,
Label2 - second row (second column value) as Label2.Text and so on from DataTable which has two columns (ID, textData) programmatically.
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: How to bind data to labels programmatically
May 01, 2012 12:24 PM|LINK
if these controls on your form then why are you doing the findcontrol thing
just label1.text = you data
whats your issue?
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: How to bind data to labels programmatically
May 04, 2012 07:26 AM|LINK
protected void Page_Load(object sender, EventArgs e) { DataTable table="Your DataTable"; DataRow[] dr = table.Select("ID>=1"); //Presuming the minimal ID=1,This will select all rows in your DataTable. int i = 1; foreach (GridViewRow row in GridView1.Rows) { ((Label)row.FindControl("Label"+i)).Text = dr[i-1]["textData"].ToString(); i++; } }Feedback to us
Develop and promote your apps in Windows Store