私は asp.net で多くの作業を行ったことがなく、Table クラスは私をやや混乱させます。ここでの目的は、レコードを表示および編集するためのチェックボックスまたはボタンを含むいくつかの列を含む、データベース内のボランティアのリストを含むテーブルを表示することです。これは私がこれまでに得たものです...
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using System.Data.OleDb;
public partial class VolunteerList : System.Web.UI.Page
{
#region Database
public OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|database.accdb");
public OleDbCommand cmd = new OleDbCommand();
public OleDbDataReader rdr;
#endregion;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
// Check if the user is logged in
if ((Convert.ToBoolean(Session["Check"]) == false) || Session["Check"] == null)
Response.Redirect("/Login.aspx");
// Display volunteer table
//volunteers = listVolunteers();
//volunteers.ID = "volunteerlist";
}
protected Table listVolunteers()
{
// Query to get volunteer information
string query = "SELECT gvo_id, gvo_name, gvo_email, gvo_mobile_ph, gvo_dob, gvo_status FROM volunteer";
// Table will hold the following information on volunteers:
// ID, Name, Email, Mobile, DOB, Status
// As well as a check box to do mass delete, a view button and an edit button on each
Table result = new Table();
result.ID = "volunteers";
// Creater table header
TableHeaderRow header = new TableHeaderRow();
TableHeaderCell id = new TableHeaderCell();
id.Text = "ID #";
header.Cells.Add(id);
TableHeaderCell name = new TableHeaderCell();
name.Text = "NAME";
header.Cells.Add(name);
TableHeaderCell email = new TableHeaderCell();
email.Text = "EMAIL";
header.Cells.Add(email);
TableHeaderCell mobile = new TableHeaderCell();
mobile.Text = "MOBILE #";
header.Cells.Add(mobile);
TableHeaderCell dob = new TableHeaderCell();
dob.Text = "D.O.B";
header.Cells.Add(dob);
TableHeaderCell status = new TableHeaderCell();
status.Text = "STATUS";
header.Cells.Add(status);
TableHeaderCell check = new TableHeaderCell();
check.Text = " ";
header.Cells.Add(check);
TableHeaderCell view = new TableHeaderCell();
view.Text = " ";
header.Cells.Add(view);
TableHeaderCell edit = new TableHeaderCell();
edit.Text = " ";
header.Cells.Add(edit);
// Add header to table
result.Rows.Add(header);
//Declaration for following code
TableRow row = new TableRow();
TableCell rid = new TableCell();
rid.CssClass = "id";
TableCell rname = new TableCell();
rname.CssClass = "name";
TableCell remail = new TableCell();
remail.CssClass = "email";
TableCell rmobile = new TableCell();
rmobile.CssClass = "mobile";
TableCell rdob = new TableCell();
rdob.CssClass = "dob";
TableCell rstatus = new TableCell();
rstatus.CssClass = "status";
TableCell rcheck = new TableCell();
rcheck.CssClass = "check";
TableCell rview = new TableCell();
rview.CssClass = "view";
TableCell redit = new TableCell();
redit.CssClass = "edit";
CheckBox chk = new CheckBox();
chk.Checked = false;
Button btnView = new Button();
btnView.Text = "View";
Button btnEdit = new Button();
btnEdit.Text = "Edit";
// Add volunteers to table
try
{
using (conn)
{
conn.Open();
cmd = new OleDbCommand(query, conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
rid.Text = rdr.GetValue(0).ToString();
rname.Text = rdr.GetValue(1).ToString();
remail.Text = rdr.GetValue(2).ToString();
rmobile.Text = rdr.GetValue(3).ToString();
rdob.Text = rdr.GetValue(4).ToString();
rstatus.Text = rdr.GetValue(5).ToString();
chk.ID = rid.Text;
btnView.PostBackUrl = string.Format("/VolunteerView.aspx?ID={0}", rid.Text);
btnEdit.PostBackUrl = string.Format("/VolunteerEdit.aspx?ID={0}", rid.Text);
rcheck.Controls.Add(chk);
rview.Controls.Add(btnView);
redit.Controls.Add(btnEdit);
// Add cells to row
row.Cells.Add(rid);
row.Cells.Add(rname);
row.Cells.Add(remail);
row.Cells.Add(rmobile);
row.Cells.Add(rdob);
row.Cells.Add(rstatus);
row.Cells.Add(rcheck);
row.Cells.Add(rview);
row.Cells.Add(redit);
// Add row to table
result.Rows.Add(row);
}
}
}
catch (Exception e)
{
TableCell error = new TableCell();
error.ColumnSpan = 9;
error.CssClass = "error";
error.Text = "An error occurred while loading the volunteers";
row.Cells.Add(error);
result.Rows.Add(row);
}
finally
{
if (rdr != null)
rdr.Close();
}
if (conn != null)
conn.Close();
return result;
}
}
そして、これは私が試して.aspxページに投稿する必要があるものです
<form id="list_volunteers" runat="server">
<% listVolunteers(); %>
</form>
いろいろ試してみたのですが、うまく動かない...