简体   繁体   中英

Issues with calling a codeBehind function from the aspx page

So an ASP.net project that someone else built about four years ago in Visual Studios 2008 now needs some of its hard coded values changed, and it's my task to rebuild it. I am using visual studios 2012.

I do not have a good grasp of ASP, as this is not what I normally do. I am having issues with the following bit of (redacted) code:

<%@ Page Language="C#" AutoEventWireup="True" 
CodeBehind="CourseList.aspx.cs" Inherits="ah.CourseList" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
<form id="form1" runat="server">
<% writeCourseList(); %> <!--Compiler complains -->
</form>
</body>
</html>

Visual studios says that "The name 'writeCourseList' does not exist in the current context"

I am confused, as writeCourseList is a public method in class CourseList. Also, this code must have compiled at one point since it works on the live server. CourseList class:

namespace ah{
    public partial class CourseList: System.Web.UI.Page{
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void writeCourseList()
        { //do stuff}
    }
}

Any help would be appreciated. Also, please feel free to advise me on how to better ask this question. Thank you!

Try

public string writeCourseList()
{
    return "this is a course list";
}

You need to return a string from the method when you want to use inline asp.net tags.

inline asp.net tags... sorting them all out (<%$, <%=, <%, <%#, etc.)

But instead i would not use this classis asp style. You should use web databound controls like GridView, Repeater, ListView,DataList or a simple ListBox instead.

For example:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
     {
          listBox1.DataSource = getAllListCourses(); // f.e. a DataTable
          listBox1.DataTextField  = "TextColumn";
          listBox1.DataValueField = "IdColumn";
          listBox1.DataBind();
     }
}

You can also add ListItems manually:

listBox1.Items.Add(new ListItem("Course-name","ID"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM