KennyH
KennyH

Reputation: 773

Find Control Text (ASP.NET/C#)

Trying to pull the text value of the label that is dynamically populated by a SQL database. Any help would be greatly appreciated!

ASP.NET

<asp:Label ID="PlatformName" Text='<%# DataBinder.Eval(Container.DataItem, "PlatformName") %>' runat="server" />

C# Code Behind (Which gives me the object, not the string value in the label)

string strPlatform = GameGrid.Rows[counter].FindControl("PlatformName").ToString() 

Upvotes: 2

Views: 6141

Answers (1)

Xavier Poinas
Xavier Poinas

Reputation: 19733

FindControl will return a control (of type Control), so you will need to cast it to a Label to access the Text property.

Try:

Label lbl = GameGrid.Rows[counter].FindControl("PlatformName") as Label;
if (lbl != null)
    strPlatform = lbl.Text;

Upvotes: 7

Related Questions