如何从aspx页面调用代码隐藏方法?

时间:2022-08-25 22:09:54

I've an object that contains a field called DevList which is defined like this

我有一个对象,其中包含一个名为DevList的字段,其定义如下

public List<string> DevList { get; set; }

I also defined a method called DisplayListOfDevelopers that is supposed to concatenate the list of developers and display it as a one string.

我还定义了一个名为DisplayListOfDevelopers的方法,该方法应该连接开发人员列表并将其显示为一个字符串。

This is how I'm calling the method from aspx.

这就是我从aspx调用方法的方法。

<asp:TemplateField HeaderText = "Developer(s)">
 <ItemTemplate>
   <asp:Label 
        ID="_lblDevList" 
        runat="server" 
        Text= '<%# DisplayListOfDevelopers(DevList) %>'>
   </asp:Label>
 </ItemTemplate>
</asp:TemplateField>

But, I'm getting this error: The name 'DevList' does not exist in the current context

但是,我收到了这个错误:当前上下文中不存在名称'DevList'

Am I missing something?

我错过了什么吗?

EDIT

_gvStatus = ds;
_gvStatus.DataBind();

Where ds is just a list of objects that contains the DevList for now.

其中ds只是一个包含DevList的对象列表。

Thanks for helping

谢谢你的帮助

1 个解决方案

#1


13  

Assuming this is how your class looks:

假设这是你的班级的样子:

public class MyItem
{
    public List<string> DevList { get; set; }
}

And that

然后

ds = List<MyItem>();

Do this:

做这个:

In your code-behind:

在您的代码隐藏中:

protected string DisplayListOfDevelopers(object _devList)
{
    //Cast your dev list into the correct object
}

In your markup:

在你的标记中:

<asp:TemplateField HeaderText = "Developer(s)">
 <ItemTemplate>
   <asp:Label 
        ID="_lblDevList" 
        runat="server" 
        Text= '<%# DisplayListOfDevelopers(Eval("DevList")) %>'>
   </asp:Label>
 </ItemTemplate>
</asp:TemplateField>

Just be sure to make the function in your code-behind is protected or public.

确保您的代码隐藏中的功能受到保护或公开。

#1


13  

Assuming this is how your class looks:

假设这是你的班级的样子:

public class MyItem
{
    public List<string> DevList { get; set; }
}

And that

然后

ds = List<MyItem>();

Do this:

做这个:

In your code-behind:

在您的代码隐藏中:

protected string DisplayListOfDevelopers(object _devList)
{
    //Cast your dev list into the correct object
}

In your markup:

在你的标记中:

<asp:TemplateField HeaderText = "Developer(s)">
 <ItemTemplate>
   <asp:Label 
        ID="_lblDevList" 
        runat="server" 
        Text= '<%# DisplayListOfDevelopers(Eval("DevList")) %>'>
   </asp:Label>
 </ItemTemplate>
</asp:TemplateField>

Just be sure to make the function in your code-behind is protected or public.

确保您的代码隐藏中的功能受到保护或公开。