[英]Calling a method from within a class - not working
所以我在這個類中有一個類,評論者和兩個方法,SaveBtn_Click - 主要不是由我創建的,然后是PeerReview,主要是由我創建的。
無論如何,代碼就像這樣開始(在各種using語句之后):
public partial class commenter : System.Web.UI.Page
{
string employee_reviewed;
PeerReview pr = new PeerReview();
public void SaveBtn_Click(object sender, EventArgs e)
{
//all the information for the SaveBtn_Click method.
}
在那之后,我有PeerReview:
public void PeerReview(System.Web.UI.WebControls.ListBox listbox)
{
MySqlConnection con = new MySqlConnection("server=localhost;database=hourtracking;uid=username;password=password");
MySqlCommand cmd = new MySqlCommand("select first_name from employee where active_status=1", con);
con.Open();
MySqlDataReader r = cmd.ExecuteReader();
Console.WriteLine("Another test!");
Console.WriteLine(r);
Console.WriteLine("Hi, this is a test!");
while (r.Read())
{
listbox.Items.Add(new ListItem(Convert.ToString(r["first_name"]), Convert.ToString(r["first_name"])));
}
con.Close();
}
我將它與ASP.NET連接,我可以顯示列表框,但不能顯示列表框中的各個項目。 我正在使用console.writeline命令對其進行測試,以查看是否輸出任何內容 - 但ASP頁面上沒有任何內容。
我不確定我應該如何引用這些特定的部分(C#的新內容,就此問了3個問題)。
ASP代碼如下所示:
<asp:ListBox ID="listBox1" runat="server">
你有一些混淆的聲明。
您聲明了一個名為PeerReview的方法 ,但您也嘗試創建一個PeerReview實例,就像它是一個類型一樣 。 我想你真的只想從你的按鈕點擊事件中調用PeerReview方法,例如
public void SaveBtn_Click(object sender, EventArgs e)
{
PeerReview();
}
然后消除“PeerReview pr = new PeerReview();” 線。 此外,由於它位於頁面上,因此您可以通過其ID在部分類中對列表框進行隱式引用,因此您無需將其作為參數傳遞。 並且Console.WriteLines在Web應用程序中沒用 - 如果您想將其添加到輸出以進行調試,可以嘗試使用Response.Write。
基於OP響應進行編輯
您應該在Page_Load事件處理程序中調用PeerReview:
public void Page_Load(object sender, EventArgs e)
{
// You need to determine if you should call PeerReview every time the page
// loads, or only on the initial call of the page, thus determining whether
// you need the IsPostBack() test. My instinct is that you *do* want to constrain
// it to the first pass, but only you can make that determination for
// certain based on your requirements.
if (!Page.IsPostBack) //Do you need this check?
{
PeerReview();
}
}
快速查看此處是您listBox1
項目添加到listbox
而不是listBox1
更改:
listbox.Items.Add(new ListItem(Convert.ToString(r["first_name"]), Convert.ToString(r["first_name"])));
至:
listBox1.Items.Add(new ListItem(Convert.ToString(r["first_name"]), Convert.ToString(r["first_name"])));
您嘗試將項目添加到列表框,盡管您的listBox的id為listBox1
而不是循環遍歷數據並添加項目,為什么不將數據源綁定到列表框,然后在列表框上設置DataTextField和DataValueField。
所以例如(錯別字可能存在......從我寫C#以來已經有一段時間了)
MySqlConnection con = new MySqlConnection("server=localhost;database=hourtracking;uid=username;password=password");
MySqlCommand cmd = new MySqlCommand("select first_name from employee where active_status=1", con);
con.Open();
MySqlDataReader r = cmd.ExecuteReader();
listBox1.DataSource = r;
listBox1.DataBind();
con.Close();
如果你無法綁定到閱讀器(不記得..)然后先將結果轉儲到數據表中,然后綁定到listBox1
DataTable dTable = New DataTable();
dTable.Load(reader);
listBox1.DataSource = dTable;
listBox1.DataBind();
在你的asp中,設置listBox字段,如:
<asp:ListBox ID="listBox1" runat="server" DataTextField="first_name" DataValueField="first_name">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.