查找控件文本(ASP.NET/C#)

Ken*_*nyH 2 c# asp.net

尝试提取由SQL数据库动态填充的标签的文本值.任何帮助将不胜感激!

ASP.NET

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

C#Code Behind(它给出了对象,而不是标签中的字符串值)

string strPlatform = GameGrid.Rows[counter].FindControl("PlatformName").ToString() 
Run Code Online (Sandbox Code Playgroud)

Xav*_*nas 7

FindControl将返回一个控件(类型为Control),因此您需要将其强制转换为Label以访问Text属性.

尝试:

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