1

I have a GridView control bound to an AccessDataSource. After selecting a row I'm creating a table inside the selected row. I'm adding Buttons to this table. Their Click event never gets fired. I read about recreating the buttons and stuff, but still no luck solving the issue. Thanks for help!

.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdminSurvey.aspx.cs" Inherits="AdminSurvey" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="id_survey_grid_view" runat="server" DataSourceID="id_access_data_source"  
              SkinID="default_skin" AllowSorting="True" AutoGenerateColumns="false" 
              OnRowCreated="SurveyGridView_RowCreated">
<Columns>
        <asp:HyperLinkField HeaderText="Title" SortExpression="TITLE"
            DataTextField="TITLE" DataNavigateUrlFields="SURVEY_ID"
            DataNavigateUrlFormatString="~\AdminSurvey.aspx?survey_id={0}">
        </asp:HyperLinkField>
    </Columns>
</asp:GridView>
<asp:AccessDataSource ID="id_access_data_source" runat="server" DataFile="~/App_Data/database.mdb" 
    OldValuesParameterFormatString="original_{0}"
    OnLoad="InitAccessDataSource">
</asp:AccessDataSource>
</form>
</body>
</html>

.cs:

public partial class AdminSurvey : System.Web.UI.Page
{
 private const string ID_BUTTON_SUBMIT = "SUBMIT_BUTTON";
 private const string ID_BUTTON_DELETE = "SUBMIT_DELETE";
 private string _selected_survey;

protected void SurveyGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null && !IsPostBack)
    {
        string survey = ((DataRowView)e.Row.DataItem).Row.ItemArray[0].ToString();
        if (survey.Equals(_selected_survey))
        {
            e.Row.Cells[0].Controls.Clear();
            // create table
            e.Row.Cells[0].Controls.Add(createSurveyTable(((DataRowView)e.Row.DataItem).Row.ItemArray[0].ToString(),
                                                    ((DataRowView)e.Row.DataItem).Row.ItemArray[1].ToString(),
                                                    ((DataRowView)e.Row.DataItem).Row.ItemArray[2].ToString()));

            ViewState["row_index"] = Convert.ToString(e.Row.RowIndex);
            ViewState["survey_id"] = ((DataRowView)e.Row.DataItem).Row.ItemArray[0].ToString();
            ViewState["title"] = ((DataRowView)e.Row.DataItem).Row.ItemArray[1].ToString();
            ViewState["description"] = ((DataRowView)e.Row.DataItem).Row.ItemArray[2].ToString();
        }
    } else if(e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null && IsPostBack) {
        string row_idx = (string)ViewState["row_index"];

        if (row_idx != null && e.Row.RowIndex.Equals(Convert.ToInt32(row_idx)))
        {
            _selected_survey = (string)ViewState["survey_id"];
            string title = (string)ViewState["title"];
            string description = (string)ViewState["description"];

            e.Row.Cells[0].Controls.Clear();
            // recreate table
            e.Row.Cells[0].Controls.Add(createSurveyTable(_selected_survey, title, description));
        }
    }
}

private Table createSurveyTable(string survey_id, string title, string description)
{
    Table table = new Table();
    TableRow row = new TableRow();
    TableCell cell = new TableCell();

    Table questions_table = createQuestionsTable(survey_id);

    cell.Controls.Add(questions_table);

    row.Cells.Add(cell);
    table.Rows.Add(row);

    return table;
}

private Table createQuestionsTable(string survey_id)
{

    // submit row
    TableRow submit_row = new TableRow();
    TableCell submit_cell = new TableCell();

    submit_button = new Button();
    submit_button.Text = "Submit";
    submit_button.ID = ID_BUTTON_SUBMIT;
    submit_button.Click += new EventHandler(submitButton_Click);
    submit_cell.Controls.Add(submit_button);

    delete_button = new Button();
    delete_button.Text = "Delete";
    delete_button.ID = ID_BUTTON_DELETE;
    delete_button.Click += new EventHandler(deleteButton_Click);
    submit_cell.Controls.Add(delete_button);

    submit_row.Cells.Add(submit_cell);
    table.Rows.Add(submit_row);

    return table;
}

private void submitButton_Click(object sender, EventArgs e)
{
}

private void deleteButton_Click(object sender, EventArgs e)
}

} // class
1
  • your createQuestionsTable method called after page_load and you are adding a event for dynamic buttons, so you are very late in the page life cycle to add this button. You have to create button and events at most Page_Load event to work correctly. Any controls like button and having events added after Page_Load wont fire the event handler appropriately. Commented Jan 18, 2013 at 7:47

2 Answers 2

1

I hate to answer my question, but I hope it saves anyone time when looking at the problem. It turned out, that the error was in the if conditions:

if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null && !IsPostBack)
{
    // ...
} else if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null && IsPostBack) {
    // ...
}

e.Row.DataItem != null was erroneously present in both conditions and the second condition, with IsPostBack equal to true, was never executed.

The correct code is:

if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null && !IsPostBack)
{
    // ...
} else if (e.Row.RowType == DataControlRowType.DataRow && IsPostBack)
{
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try assigning a command name, and attach to the RowCommand event of the grid to listen for the click of the button, as an alternative to tapping into the click event.

3 Comments

I assigned a commandName to the submit_button and added a RowCommandEventHandler to the GridView. Clicking the button results in a simple postback. I also tried adding a asp:buttonfield to the .aspx code. The buttonfield was correctly firing the event. Clicking my old buttons then, resolved in raising the RowCommand event with an incorrect commandname - the buttonfield's commandname.
Wow, OK, that is odd... try also setting UseSubmitBehavior to false on the button, so it will use the __doPostBack method approach to posting back...
I did submit_button.CommandName = "SUBMIT"; submit_button.UseSubmitBehavior = false; and the "void SurveyGridView_RowCommand(object sender, GridViewCommandEventArgs e)" still dosn't get fired...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.