Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 435
Richard77 Avatar asked Oct 05 '12 21:10

Richard77


People also ask

How do you call a method from ASPX?

If you need to call a server-side function without postback, the only ways are using AJAX or ICallBackEventHandler. There are only 2 ways to call a server-side function from client-side: AJAX or a PostBack (__doPostBack(...)).

How call JavaScript code behind method in asp net?

There are two ways to access code behind method in asp.net using JavaScript: Using ScriptManager and WebMethod. Using Jquery and button click from JavaScript.


1 Answers

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.

like image 59
Lawrence Johnson Avatar answered Oct 24 '22 03:10

Lawrence Johnson