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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x