[英]Issues with calling a codeBehind function from the aspx page
因此,大約四年前其他人在Visual Studios 2008中構建的ASP.net項目現在需要更改其一些硬編碼值,這是重建它的任務。 我正在使用Visual Studios 2012。
我對ASP沒有很好的了解,因為這不是我通常要做的。 我遇到了以下(已編輯)代碼問題:
<%@ 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表示“名稱'writeCourseList'在當前上下文中不存在”
我很困惑,因為writeCourseList是CourseList類中的公共方法。 另外,由於此代碼可在實時服務器上運行,因此必須在某一時刻進行編譯。 CourseList類:
namespace ah{
public partial class CourseList: System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
}
public void writeCourseList()
{ //do stuff}
}
}
任何幫助,將不勝感激。 另外,請隨時就如何更好地提出這個問題向我提出建議。 謝謝!
嘗試
public string writeCourseList()
{
return "this is a course list";
}
當您要使用嵌入式asp.net標記時,需要從該方法返回一個字符串。
內聯asp.net標記...將它們全部排序(<%$,<%=,<%,<%#等)
但是相反,我不會使用此類classasp樣式。 您應該改用Web數據綁定控件,例如GridView, Repeater, ListView,DataList
或簡單的ListBox
。
例如:
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();
}
}
listBox1.Items.Add(new ListItem("Course-name","ID"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.