我没有做过很多与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>
我尝试了一些东西,似乎无法让它运行......
答案 0 :(得分:2)
在您的设计师中,您可以选择占位符。 (PlaceHolder控件用于为代码添加的控件保留空间。)
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
在代码隐藏文件中,您需要在占位符中添加动态表。
protected void Page_Load(object sender, EventArgs e)
{
PlaceHolder1.Controls.Add(listVolunteers());
}
我希望它会对你有所帮助。