在GridView的动态按钮 - Button.Click事件不触发 [英] Dynamic buttons in GridView - Button.Click event not firing
问题描述
我必须绑定到的AccessDataSource GridView控件。选择行后,我创建所选行内的表。我添加按钮此表。他们的Click事件永远不会被解雇。我读到重新创建按钮之类的东西,但仍没有运气问题的解决。感谢您的帮助!
的.aspx:
<%@页面语言=C#AutoEventWireup =真codeFILE =AdminSurvey.aspx.cs继承=AdminSurvey%GT;!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
< /头>
<身体GT;
<表ID =form1的=服务器>
< ASP:GridView控件ID =id_survey_grid_view=服务器的DataSourceID =id_access_data_source
SkinID =default_skinAllowSorting =真的AutoGenerateColumns =假
OnRowCreated =SurveyGridView_RowCreated>
<柱体和GT;
< ASP:HyperLinkField字段的HeaderText =标题SORTEX pression =TITLE
DataTextField =TITLEDataNavigateUrlFields =SURVEY_ID
DataNavigateUrlFormatString =〜\\ AdminSurvey.aspx survey_id = {0}?>
< / ASP:HyperLinkField字段>
< /专栏>
< / ASP:GridView的>
< ASP:的AccessDataSource ID =id_access_data_source=服务器数据文件=〜/ App_Data文件/ database.mdb
OldValuesParameterFormatString =original_ {0}
的OnLoad =InitAccessDataSource>
< / ASP:&的AccessDataSource GT;
< /表及GT;
< /身体GT;
< / HTML>
的.cs:
公共部分类AdminSurvey:System.Web.UI.Page
{
私人常量字符串ID_BUTTON_SUBMIT =SUBMIT_BUTTON;
私人常量字符串ID_BUTTON_DELETE =SUBMIT_DELETE;
私人字符串_selected_survey;保护无效SurveyGridView_RowCreated(对象发件人,GridViewRowEventArgs E)
{
如果(e.Row.RowType == DataControlRowType.DataRow&放大器;&安培;!e.Row.DataItem = NULL&放大器;&安培;!的IsPostBack)
{
字符串调查=((DataRowView的)e.Row.DataItem).Row.ItemArray [0]的ToString();
如果(survey.Equals(_selected_survey))
{
e.Row.Cells [0] .Controls.Clear();
//创建表
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 [标题] =((DataRowView的)e.Row.DataItem).Row.ItemArray [1]的ToString();
的ViewState [说明] =((DataRowView的)e.Row.DataItem).Row.ItemArray [2]的ToString();
}
}否则如果(e.Row.RowType == DataControlRowType.DataRow&放大器;&安培;!e.Row.DataItem = NULL&放大器;&放大器;的IsPostBack){
字符串row_idx =(字符串)的ViewState [ROW_INDEX]; 如果(row_idx = NULL&放大器;!&安培; e.Row.RowIndex.Equals(Convert.ToInt32(row_idx)))
{
_selected_survey =(字符串)的ViewState [survey_id];
字符串标题=(字符串)的ViewState [标题];
字符串描述=(字符串)的ViewState [说明]; e.Row.Cells [0] .Controls.Clear();
//重新创建表
e.Row.Cells [0] .Controls.Add(createSurveyTable(_selected_survey,标题,描述));
}
}
}私人表createSurveyTable(字符串survey_id,字符串名称,字符串描述)
{
表的表=新表();
连续的TableRow =新的TableRow();
TableCell的细胞=新的TableCell(); 表questions_table = createQuestionsTable(survey_id); cell.Controls.Add(questions_table); row.Cells.Add(细胞);
table.Rows.Add(行); 返回表;
}私人表createQuestionsTable(字符串survey_id)
{ //提交排
的TableRow submit_row =新的TableRow();
TableCell的submit_cell =新的TableCell(); submit_button =新按钮();
submit_button.Text =提交;
submit_button.ID = ID_BUTTON_SUBMIT;
submit_button.Click + =新的EventHandler(submitButton_Click);
submit_cell.Controls.Add(submit_button); delete_button =新按钮();
delete_button.Text =删除;
delete_button.ID = ID_BUTTON_DELETE;
delete_button.Click + =新的EventHandler(deleteButton_Click);
submit_cell.Controls.Add(delete_button); submit_row.Cells.Add(submit_cell);
table.Rows.Add(submit_row); 返回表;
}私人无效submitButton_Click(对象发件人,EventArgs的发送)
{
}私人无效deleteButton_Click(对象发件人,EventArgs的发送)
}} //类
我不想回答我的问题,但我希望在看问题的时候可以节省时间的人。原来,那是错误的,如果条件:
如果(e.Row.RowType == DataControlRowType.DataRow&放大器;&安培;!e.Row.DataItem = NULL&放大器;&安培;!的IsPostBack)
{
// ...
}否则如果(e.Row.RowType == DataControlRowType.DataRow&放大器;&安培;!e.Row.DataItem = NULL&放大器;&放大器;的IsPostBack){
// ...
}
e.Row.DataItem!= NULL
被错误present在这两个条件,第二个条件,与的IsPostBack
等于真实的,从来没有执行。
正确的code是:
如果(e.Row.RowType == DataControlRowType.DataRow&放大器;&安培;!e.Row.DataItem = NULL&放大器;&安培;!的IsPostBack)
{
// ...
}否则如果(e.Row.RowType == DataControlRowType.DataRow&放大器;&放大器;的IsPostBack)
{
// ...
}
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
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)
{
// ...
}
这篇关于在GridView的动态按钮 - Button.Click事件不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!