0

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.

        }

    }
Sohee
  • 23
  • 5
  • An ASP.NET button sends a *post-back* to the page *no matter what*, I recommend you read about `IsPostBack` (a property on `System.Web.UI.Page`), and in your `Page_Load` test for that property. (If it's true, then the user clicked a button that reloaded the page.) You can use this to determine if you should reload the data or not. (Usually, a good chunk of the `Page_Load` code is in a `if (!IsPostBack)` block, as a pointer.) – Der Kommissar Sep 06 '17 at 16:13
  • So I should essentially put **CheckMember();** and **LoadDataTable();** in a conditional for IsPostBack? Also how does this explain why when I click the buttons it never goes into the event handler for clicking a button in a button field? How should that be fixed? – Sohee Sep 06 '17 at 20:05
  • I'll bet you $100 that putting those in a `if (!IsPostBack) { }` block fixes *that* part of the issue, as for the other bit: I'm not certain, I don't use `GridView`, but it's possible that this would fix that issue as well because ASP.NET might be rebinding those buttons. Try making sure you have `onrowcommand="gridViewStudent_RowCommand"` in your markup, or a binding to the event in code. – Der Kommissar Sep 06 '17 at 20:08
  • What do you mean by markup? Where exactly would I put this? – Sohee Sep 07 '17 at 01:19
  • I'm referring to dropping the `onrowcommand` into the ASPX page, but you can also wire-up the event in the `Page_Load` method as well. (Really, the choice is up to you.) – Der Kommissar Sep 07 '17 at 12:20

0 Answers0