[英]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. 因此,大约四年前其他人在Visual Studios 2008中构建的ASP.net项目现在需要更改其一些硬编码值,这是重建它的任务。 I am using visual studios 2012.
我正在使用Visual Studios 2012。
I do not have a good grasp of ASP, as this is not what I normally do. 我对ASP没有很好的了解,因为这不是我通常要做的。 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" Visual Studios表示“名称'writeCourseList'在当前上下文中不存在”
I am confused, as writeCourseList is a public method in class CourseList. 我很困惑,因为writeCourseList是CourseList类中的公共方法。 Also, this code must have compiled at one point since it works on the live server.
另外,由于此代码可在实时服务器上运行,因此必须在某一时刻进行编译。 CourseList class:
CourseList类:
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. 当您要使用嵌入式asp.net标记时,需要从该方法返回一个字符串。
inline asp.net tags... sorting them all out (<%$, <%=, <%, <%#, etc.) 内联asp.net标记...将它们全部排序(<%$,<%=,<%,<%#等)
But instead i would not use this classis asp style. 但是相反,我不会使用此类classasp样式。 You should use web databound controls like
GridView, Repeater, ListView,DataList
or a simple ListBox
instead. 您应该改用Web数据绑定控件,例如
GridView, Repeater, ListView,DataList
或简单的ListBox
。
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: 您也
add
手动add
ListItems
:
listBox1.Items.Add(new ListItem("Course-name","ID"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.