2010-02-28 168 views
1

我有一個綁定到AccessDataSource的GridView控件。選擇一行後,我在選定的行內創建一個表格。我將Buttons添加到此表中。他們的Click事件永遠不會被解僱。我讀過關於重新創建按鈕和內容的問題,但仍然沒有解決問題的運氣。感謝幫助!GridView中的動態按鈕 - Button.Click事件沒有觸發

的.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 
+0

您createQuestionsTable方法和所添加的動態按鈕的事件,所以你在頁面生命週期很晚添加此按鈕。您必須在最多的Page_Load事件中創建按鈕和事件才能正常工作。在Page_Load之後的任何控件(如按鈕和添加事件)都不會適當地觸發事件處理程序。 – 2013-01-18 07:47:00

回答

0

我討厭回答我的問題,但我希望能夠節省任何時間查看問題。事實證明,該誤差是在如果條件:

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在兩種條件和所述第二條件錯誤地存在,與IsPostBack等於真,從未執行。

正確的代碼是:的Page_Load後調用

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

嘗試分配命令名稱,並連接到電網的RowCommand事件監聽按鈕的點擊,作爲替代點擊點擊事件。

+0

我爲submit_button分配了一個commandName,並向GridView添加了一個RowCommandEventHandler。點擊該按鈕會產生一個簡單的回傳。 我也嘗試在.aspx代碼中添加一個asp:buttonfield。 buttonfield正確地發射了該事件。點擊我的舊按鈕,然後用一個不正確的命令名提升RowCommand事件 - buttonfield的命令名。 – harpun 2010-02-28 18:04:03

+0

哇,好吧,這很奇怪...嘗試在按鈕上設置UseSubmitBehavior爲false,所以它會使用__doPostBack方法來回傳... – 2010-03-01 13:43:50

+0

我做了submit_button.CommandName =「SUBMIT」; submit_button.UseSubmitBehavior = false;和「void SurveyGridView_RowCommand(對象發件人,GridViewCommandEventArgs e)」仍然不會被解僱...... – harpun 2010-03-04 22:10:54