请高手帮忙
需求,部分代码已经写好,还差 删除、添加和取里面的值的功能,
先贴一张图吧 cs 代码// 从数据库里面 填充到ds table里去。
public DataTable date_sa(string ReworkFormNo)
{
SqlConnection conn = dbbase.DB.createconn();
string sql = "select Parts_Name,Part_No,Price from[ReworkForm_Parts] where ReworkFormNo='" + ReworkFormNo + "'";
SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds.Tables[0]; }//加载的时候 在文本框里面显示的数据、
protected void Button1_Click(object sender, EventArgs e)
{
int i;
int j;
DataTable dt = date_sa("RW091013014");
int rowcounts = dt.Rows.Count;
int cellcounts = dt.Columns.Count;
for (i = 0; i < rowcounts; i++)
{
TableRow tr = new TableRow();
Table1.Rows.Add(tr);
for (j = 0; j < cellcounts; j++)
{
TableCell td = new TableCell();
TextBox tb = new TextBox();
if (j % 4 == 0)
{
Button bt = new Button();
bt.ID = "bt" + i + j;
bt.Text = "删除";
td.Controls.Add(bt);
}
tb.ID = "tb" + i + j;
tb.Text = dt.Rows[i][j].ToString();
td.Controls.Add(tb);
tr.Cells.Add(td); } } }谢谢 大家 帮帮忙
需求,部分代码已经写好,还差 删除、添加和取里面的值的功能,
先贴一张图吧 cs 代码// 从数据库里面 填充到ds table里去。
public DataTable date_sa(string ReworkFormNo)
{
SqlConnection conn = dbbase.DB.createconn();
string sql = "select Parts_Name,Part_No,Price from[ReworkForm_Parts] where ReworkFormNo='" + ReworkFormNo + "'";
SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds.Tables[0]; }//加载的时候 在文本框里面显示的数据、
protected void Button1_Click(object sender, EventArgs e)
{
int i;
int j;
DataTable dt = date_sa("RW091013014");
int rowcounts = dt.Rows.Count;
int cellcounts = dt.Columns.Count;
for (i = 0; i < rowcounts; i++)
{
TableRow tr = new TableRow();
Table1.Rows.Add(tr);
for (j = 0; j < cellcounts; j++)
{
TableCell td = new TableCell();
TextBox tb = new TextBox();
if (j % 4 == 0)
{
Button bt = new Button();
bt.ID = "bt" + i + j;
bt.Text = "删除";
td.Controls.Add(bt);
}
tb.ID = "tb" + i + j;
tb.Text = dt.Rows[i][j].ToString();
td.Controls.Add(tb);
tr.Cells.Add(td); } } }谢谢 大家 帮帮忙
TextBox textbox1=new TextBox();
div.controls.add(textbox1);
this.div.Controls.Add(new LiteralControl(" </td> </tr> </table>" + "\n"));
试一下
{
if (!this.IsPostBack)
{
BindGrid();
}
} private DataTable ReadGridView()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("ProductID", typeof(string)));
dt.Columns.Add(new DataColumn("ProductName", typeof(string)));
dt.Columns.Add(new DataColumn("CategoryID", typeof(string)));
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
dr = dt.NewRow();
dr[0] = this.GridView1.Rows[i].Cells[0].Text.Trim();
dr[1] = this.GridView1.Rows[i].Cells[1].Text.Trim();
dr[2] = this.GridView1.Rows[i].Cells[2].Text.Trim();
dt.Rows.Add(dr);
}
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = ReadGridView();
//this.GridView1.DataSource = dt;
//this.GridView1.DataBind();
DataRow row = dt.NewRow();
row.ItemArray = new object[] { "oec2003","oec2003","oec2003" };
dt.Rows.InsertAt(row, 0);
dt.AcceptChanges();
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
} private void BindGrid()
{
string str = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();
using (SqlConnection con = new SqlConnection(str))
{
SqlCommand cmd = new SqlCommand("SELECT top 1 [ProductID], [ProductName], [CategoryID] FROM [Products]", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
sda.Dispose();
ds.Dispose();
} }<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
思路,用gridview去绑定带空行的datatable
{
for (int i = 0; i <3; i++)
{
//创建控件并添加到panel中
Button bt = new Button();
bt.ID = i.ToString();
bt.Text = "删除"; TextBox tb11 = new TextBox();
tb11.ID ="tb"+i.ToString() + "1";
tb11.Text = i.ToString();
TextBox tb12 = new TextBox();
tb12.ID = "tb" + i.ToString() + "2";
tb12.Text = i.ToString();
TextBox tb13 = new TextBox();
tb13.ID = "tb" + i.ToString() + "3";
tb13.Text = i.ToString(); this.Panel2.Controls.Add(bt);
this.Panel2.Controls.Add(tb11);
this.Panel2.Controls.Add(tb12);
this.Panel2.Controls.Add(tb13);
//为按钮注册事件
bt.Click += new EventHandler(bt_Click);
}
} void bt_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
string id = bt.ID;
TextBox tb11 = (TextBox)this.Panel2.FindControl("tb" + id.ToString() + "1");
Response.Write("tb11的text属性为"+tb11.Text);
}
看下这段代码看是否有启发。。这段代码完成注册控件,为控件添加事件。读取需要的textbox的值的功能。
//var tb=document.getElementById("T_model");//这里不能用jq的选择器,
var tb=$("#T_model").get(0);//$("#T_model")[0]//将jquery对象转化为dom对象才能使用下面的方法
var tr=tb.insertRow(tb.rows.length);//添加行
var lasttd=tr.insertCell(tr.cells.length);//插入删除那个单元格,现在要想放下面就把这两行移到for后面就行了
lasttd.innerHTML="<input type='button' onclick='btndel(this)' value='Delete'/>";
var start=tr.cells.length,end=start+3;
for(var i=start;i<end;i++) {
var td=tr.insertCell(i);//添加单元格
td.innerHTML="<input type='text'/>";//往单元格里插入html或者$(td).html("<input type='text'/>");
}
}