所以我试图在我的数据表上用一个按钮字段实现一列按钮。但是,到目前为止,我没有将它们映射到任何事件,但由于某种原因,每当我单击其中一个按钮时,我相信它会触发 Page_Load 事件。这当然会重新加载页面,但由于其编码方式,页面本身会添加列的副本。然而,我觉得这很奇怪,因为每当我刷新页面本身时,列都不会重复。也许这是因为刷新页面时它会从一个干净的状态开始。无论如何,这些按钮神秘地有一个功能,我相信它可能会阻碍我真正想要它拥有的功能。
public partial class ViewData : System.Web.UI.Page
{
//will have use for this later
private Member curMember;
//database connection
private SLHSDataContext SLHS_DB = new SLHSDataContext();
private int curStudentIndex;
//useful constant
private const string NUMBER = "Number";
private const string FIRSTNAME = "First Name";
private const string LASTNAME = "Last Name";
private const string AGE = "Age";
private const string EMAIL = "Email";
private const string EDIT = "Edit";
protected void Page_Load(object sender, EventArgs e)
{
CheckMember();
LoadDataTable();
}
LoadDataTable() 的函数:
void LoadDataTable()
{
//create table
DataTable table = new DataTable();
AddColumnToTable(table);
//query all students
IQueryable<Member> query = from mem in SLHS_DB.Members
where mem.RoleId == (int)Credentials.MemberRole.STUDENT
select mem;
Member[] students = query.ToArray();
//add them to table
curStudentIndex = 0;
foreach (Member student in students)
{
AddRowToTable(table, student);
}
//Add the button column of "Remove"s
ButtonField buttonField = new ButtonField
{
ButtonType = ButtonType.Button,
Text = "Remove",
HeaderText = "Edit",
CommandName = "Remove"
};
gridViewStudent.Columns.Add(buttonField);
//bind data to grid view
gridViewStudent.DataSource = table;
gridViewStudent.DataBind();
}
当按下按钮时,我希望它进入的事件处理程序(但当前甚至不会在按下按钮时进入)如下所示:
protected void gridViewStudent_RowCommand(object sender, GridViewCommandEventArgs e)
{
System.Diagnostics.Debug.WriteLine("testing button event");
if (e.CommandName == "Remove")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
System.Diagnostics.Debug.WriteLine(index);
// Retrieve the row that contains the button
// from the Rows collection.
//GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
}
}