日期:2014-05-18  浏览次数:20439 次

关于点击按钮自动增加行的问题
最近遇到一个问题,在一个表单中,点击一个添加按钮会自动增加一行。点击删除按钮就会删除最后一行。
这一行里面有一个列是下拉列表去选,当选中一个值以后(比如选中某个公司名字),这一行的其他列将填充这一个选中公司的一些信息,然后在点击添加按钮又会添加一个行,和第一行是一样的,但是选择不同的公司,这一行的其他列也会填充另外公司的信息,我想知道怎么实现,想来想去都没办法给下拉列表绑定事件。 哪位高手可以指点一下啊?最好又完整的解决思路谢谢了啊。是用ASP.NET做的。
然后点击删除按钮删除最后一行又怎么做呢?

------解决方案--------------------
你可以参考
http://blog.csdn.net/net_lover/article/details/6678600
现成的源代码。拷贝粘贴成xx.aspx就可以看效果
------解决方案--------------------
可以打开的

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private DataTable dataTable = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.Bind(true);
}
}
  
private DataTable GetDataTable()
{
if (ViewState["dt"] == null)
{
dataTable = new DataTable();
dataTable.Columns.Add("usage", typeof(String));
dataTable.Columns.Add("steelKind", typeof(String));
dataTable.Columns.Add("castingTon", typeof(String));
return dataTable;
}
else
{
return ViewState["dt"] as DataTable;
}
}
  
private void Bind(bool AddBlankRow)
{
dataTable = GetDataTable();
if (AddBlankRow) dataTable.Rows.Add(dataTable.NewRow());
this.GridView1.DataSource = dataTable;
this.GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList x1 = e.Row.FindControl("x1") as DropDownList;
DropDownList x2 = e.Row.FindControl("x2") as DropDownList;
TextBox x3 = e.Row.FindControl("x3") as TextBox;
  
String usage = DataBinder.Eval(e.Row.DataItem, "usage").ToString();
String steelKind = DataBinder.Eval(e.Row.DataItem, "steelKind").ToString();
String castingTon = DataBinder.Eval(e.Row.DataItem, "castingTon").ToString();
  
x3.Text = castingTon;
ListItem xx1 = x1.Items.FindByValue(usage);
if (xx1 != null) xx1.Selected = true;
ListItem xx2 = x2.Items.FindByValue(steelKind);
if (xx2 != null) xx2.Selected = true;
}
}
  
protected void LinkButton1_Click(object sender, EventArgs e)
{
//总是取最后的一行
GridViewRow row = GridView1.Rows[GridView1.Rows.Count - 1];
DropDownList x1 = row.FindControl("x1") as DropDownList;
DropDownList x2 = row.FindControl("x2") as DropDownList;
TextBox x3 = row.FindControl("x3") as TextBox;
  
dataTable = this.GetDataTable();
DataRow newRow = dataTable.NewRow();
if (dataTable.Rows.Count > 0)
{
newRow = dataTable.Rows[dataTable.Rows.Count - 1];
}
  
newRow["usage"] = x1.SelectedValue;
newRow["steelKind"] = x2.SelectedValue;
newRow["castingTon"] = x3.Text;
if (dataTable.Rows.Count == 0)
{
dataTable.Rows.Add(newRow);
}
  
ViewState["dt"] = dataTable;
this.Bind(true);
  
}
protected void LinkButton2_Click(object sender, Even