如何从aspx页面调用代码隐藏方法? [英] How to call a code-behind method from aspx page?

查看:23
本文介绍了如何从aspx页面调用代码隐藏方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

这就是我从 aspx 调用方法的方式.

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

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

但是,我收到此错误:当前上下文中不存在名称DevList"

我错过了什么吗?

_gvStatus = ds;
_gvStatus.DataBind();

其中 ds 只是目前包含 DevList 的对象列表.

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

感谢您的帮助

推荐答案

假设您的类是这样的:

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

还有那个

ds = List<MyItem>();

这样做:

在您的代码隐藏中:

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

在您的标记中:

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

只需确保将代码隐藏中的函数设为 protected 或 public.

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

这篇关于如何从aspx页面调用代码隐藏方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆