Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

asp.net - How to call a code-behind method from aspx page?

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

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.

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>

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

Am I missing something?

EDIT

_gvStatus = ds;
_gvStatus.DataBind();

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

Thanks for helping

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...