So I am trying to implement a column of buttons on my data table with a button field. However, I did not map them to any event so far, but for some reason whenever I click one of the buttons, I believe it triggers the Page_Load event. This will of course reload the page, but the page itself adds a duplicate of the column because of the way it is coded. I find this strange however, because whenever I refresh the page itself, the columns will not duplicate. Perhaps this is because when refreshing a page it will start from a clean state. Regardless, the buttons mysteriously have a function and I believe it may be hindering the functionality that I actually want it to have.
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();
}
function for 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();
}
And the event handler that I want it to go into (but will currently not even enter when a button is pressed) when a button is pressed looks like:
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.
}
}