asp.net - GridView动态页脚行创建问题 [英] asp.net - GridView dynamic footer row creation problem
问题描述
protected void Page_Load(object sender,object sender) ,EventArgs e)
{
if(!Page.IsPostBack)
{
CreateGridView();
private void CreateGridView()
{
GridView1.Columns.Clear();
DataTable dataTable = Book.GetBooksDataSet()。Tables [0];
CommandField cf = new CommandField();
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
int colCount = 1;
foreach(dataTable.Columns中的DataColumn c)
{
BoundField boundField = new BoundField();
boundField.DataField = c.ColumnName;
boundField.HeaderText = c.ColumnName;
//boundField.FooterText =---;
if(colCount == 3 || colCount == 5)
{
boundField.ReadOnly = true;
}
GridView1.Columns.Add(boundField);
colCount ++;
}
GridView1.ShowFooter = true;
GridView1.DataSource = dataTable;
GridView1.DataBind();
GridViewRow footerRow = GridView1.FooterRow;
Button b = new Button();
b.Text =添加新的;
int i = 0;
footerRow.Cells [i] .Controls.Add(b);
foreach(dataTable.Columns中的DataColumn c)
{
++ i;
TextBox tb = new TextBox();
footerRow.Cells [i] .Controls.Add(tb);
}
}
....................................
....................................
...... ..............................
}
但问题是,当我点击添加新的按钮时,它立即消失。而且,我也无法添加任何事件处理程序。或者拦截它的动作:
pre $ protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if(e.CommandName ==Edit)
{
GridView1.EditIndex = index;
GridViewRow selectedRow =((GridView)e.CommandSource).Rows [index];
//我们可以得到像这样的单元格数据
string id = selectedRow.Cells [1] .Text;
string isbn = selectedRow.Cells [2] .Text;
//这是GridView出现的必要条件。
CreateGridView();
}
else if(e.CommandName ==Update)
{
LinkButton updateButton =(LinkButton)e.CommandSource;
DataControlFieldCell dcfc =(DataControlFieldCell)updateButton.Parent;
GridViewRow gvr =(GridViewRow)dcfc.Parent;
//更新...................
//将网格数据更新到数据库
UpdateDataInTheDatabase(gvr .Cells [1] .Controls);
// Grid返回正常
GridView1.EditIndex = -1;
//这是GridView出现的必要条件。
CreateGridView();
}
}
还有一点,我看到了一些解决方案来处理GridView的 rowBound
事件。但我需要从 Page_load
事件处理程序或
动态创建控件mus 在每次回复中重新创建。您的添加新的按钮会导致回发,因此动态创建的页脚会消失。是否有一个原因,这个网格必须动态创建?从你发布的代码看来,你可以用标记代替。如果没有,你必须在每一个回传中重新创建动态控件。
编辑后添加:
我玩了一下,什么是下面的工作原理是网格不会消失并且事件被处理,但它实际上并没有做任何事情。希望这会有所帮助。
标记:
< p><< p ; asp:Literal ID =Literal1runat =server/>< / p>
< asp:GridView ID =GridView1runat =serverAutoGenerateColumns =false
OnRowCommand =GridView1_RowCommand
OnRowEditing =GridView1_RowEditing/>
代码:
protected void Page_Load(object sender,EventArgs e)
{
BindGridView();
private DataTable GetBooksDataTable()
{
var dt = new DataTable();
dt.Columns.Add(ID,typeof(int));
dt.Columns.Add(Title,typeof(string));
dt.Columns.Add(Author,typeof(string));
for(int index = 0; index< 10; index ++)
{
dt.Rows.Add(index,Title+ index,Author+ index );
}
return dt;
private void BindGridView()
{
var dt = GetBooksDataTable();
GridView1.Columns.Clear();
GridView1.ShowFooter = true;
var cf = new CommandField();
cf.HeaderText =Action;
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
for(int index = 0; index< dt.Columns.Count; index ++)
{
var boundField = new BoundField();
boundField.DataField = dt.Columns [index] .ColumnName;
boundField.HeaderText = dt.Columns [index] .ColumnName;
GridView1.Columns.Add(boundField);
}
GridView1.DataSource = dt;
GridView1.DataBind();
var footer = GridView1.FooterRow;
var b = new LinkButton();
b.Text =添加新的;
b.CommandName =添加新的;
footer.Cells [0] .Controls.Add(b);
for(int index = 1; index< dt.Columns.Count + 1; index ++)
{
var tb = new TextBox();
footer.Cells [index] .Controls.Add(tb);
}
}
保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
Literal1.Text = e.CommandName;
}
保护无效GridView1_RowEditing(对象发件人,GridViewEditEventArgs E)
{
Literal1.Text = 编辑行索引 + e.NewEditIndex.ToString();
}
I am able to create BoundFields and Footer-rows dynamically like this in my GridView:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CreateGridView();
}
}
private void CreateGridView()
{
GridView1.Columns.Clear();
DataTable dataTable = Book.GetBooksDataSet().Tables[0];
CommandField cf = new CommandField();
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
int colCount = 1;
foreach (DataColumn c in dataTable.Columns)
{
BoundField boundField = new BoundField();
boundField.DataField = c.ColumnName;
boundField.HeaderText = c.ColumnName;
//boundField.FooterText = "---";
if (colCount == 3 || colCount == 5)
{
boundField.ReadOnly = true;
}
GridView1.Columns.Add(boundField);
colCount++;
}
GridView1.ShowFooter = true;
GridView1.DataSource = dataTable;
GridView1.DataBind();
GridViewRow footerRow = GridView1.FooterRow;
Button b = new Button();
b.Text = "Add New";
int i = 0;
footerRow.Cells[i].Controls.Add(b);
foreach (DataColumn c in dataTable.Columns)
{
++i;
TextBox tb = new TextBox();
footerRow.Cells[i].Controls.Add(tb);
}
}
....................................
....................................
....................................
}
But the problem is, when I click the "Add New" - button, it disappears instantly. And, also I am unable to add any event handler to it. Or intercept its actions like this:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "Edit")
{
GridView1.EditIndex = index;
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
//We can get cell data like this
string id = selectedRow.Cells[1].Text;
string isbn = selectedRow.Cells[2].Text;
//This is necessary to GridView to be showed up.
CreateGridView();
}
else if (e.CommandName == "Update")
{
LinkButton updateButton = (LinkButton)e.CommandSource;
DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;
GridViewRow gvr = (GridViewRow)dcfc.Parent;
//The update...................
//Update grid-data to database
UpdateDataInTheDatabase(gvr.Cells[1].Controls);
//Grid goes back to normal
GridView1.EditIndex = -1;
//This is necessary to GridView to be showed up.
CreateGridView();
}
}
One more thing, I have seen some solutions that suggests to handle the GridView's rowBound
event. But I need to do it from within Page_load
event handler, or in, GridView1_RowCommand
event handler.
Dynamically created controls mus be re-created on every postback. Your "Add New" button causes a postback so the dynamically created footer disappears. Is there a reason this grid has to be created dynamically? From the code you posted it appears that you could do this in markup instead. If not, you'll have to re-create the dynamic controls on every postback.
Edited to add: I played with this a little bit and what's below works in that the grid doesn't disappear and events are handled, but it doesn't actually do anything. Hope this helps.
Markup:
<p><asp:Literal ID="Literal1" runat="server" /></p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
OnRowCommand="GridView1_RowCommand"
OnRowEditing="GridView1_RowEditing"/>
Code:
protected void Page_Load(object sender, EventArgs e)
{
BindGridView();
}
private DataTable GetBooksDataTable()
{
var dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("Author", typeof(string));
for (int index = 0; index < 10; index++)
{
dt.Rows.Add(index, "Title" + index, "Author" + index);
}
return dt;
}
private void BindGridView()
{
var dt = GetBooksDataTable();
GridView1.Columns.Clear();
GridView1.ShowFooter = true;
var cf = new CommandField();
cf.HeaderText = "Action";
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
for (int index = 0; index < dt.Columns.Count; index++)
{
var boundField = new BoundField();
boundField.DataField = dt.Columns[index].ColumnName;
boundField.HeaderText = dt.Columns[index].ColumnName;
GridView1.Columns.Add(boundField);
}
GridView1.DataSource = dt;
GridView1.DataBind();
var footer = GridView1.FooterRow;
var b = new LinkButton();
b.Text = "Add New";
b.CommandName = "Add New";
footer.Cells[0].Controls.Add(b);
for (int index = 1; index < dt.Columns.Count + 1; index++)
{
var tb = new TextBox();
footer.Cells[index].Controls.Add(tb);
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Literal1.Text = e.CommandName;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Literal1.Text = "Editing row index " + e.NewEditIndex.ToString();
}
这篇关于asp.net - GridView动态页脚行创建问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!