Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic buttons in GridView - Button.Click event not firing
    primarykey
    data
    text
    <p>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!</p> <p>.aspx:</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdminSurvey.aspx.cs" Inherits="AdminSurvey" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:GridView ID="id_survey_grid_view" runat="server" DataSourceID="id_access_data_source" SkinID="default_skin" AllowSorting="True" AutoGenerateColumns="false" OnRowCreated="SurveyGridView_RowCreated"&gt; &lt;Columns&gt; &lt;asp:HyperLinkField HeaderText="Title" SortExpression="TITLE" DataTextField="TITLE" DataNavigateUrlFields="SURVEY_ID" DataNavigateUrlFormatString="~\AdminSurvey.aspx?survey_id={0}"&gt; &lt;/asp:HyperLinkField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:AccessDataSource ID="id_access_data_source" runat="server" DataFile="~/App_Data/database.mdb" OldValuesParameterFormatString="original_{0}" OnLoad="InitAccessDataSource"&gt; &lt;/asp:AccessDataSource&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>.cs:</p> <pre><code>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 &amp;&amp; e.Row.DataItem != null &amp;&amp; !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 &amp;&amp; e.Row.DataItem != null &amp;&amp; IsPostBack) { string row_idx = (string)ViewState["row_index"]; if (row_idx != null &amp;&amp; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload