动态生成table,row是table生成之后动态添加的现在的问题是,PostBack之后table能保存,但各个table里面的row不知道怎么保存,求教怎么保存!-----------------前台.aspx
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<style type="text/css">
table
{
border-collapse: collapse;
border:solid 1px #93c2f1;
font-size:10pt;
}
th
{
border-collapse: collapse;
border:solid 1px #93c2f1;
font-size:10pt;
}
td
{
border-collapse: collapse;
border:solid 1px #93c2f1;
font-size:10pt;
}
</style>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="增加原因" />
<br /> <asp:Table id="tab0" runat="server">
<asp:TableRow runat="server" TableSection="TableHeader">
<asp:TableCell runat="server">
<asp:Button id="Button1" runat="server" Text="增加措施"
onclick="Button1_Click"></asp:Button>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="tr" runat="server">
<asp:TableCell runat="server">
<asp:TextBox ID="tb0" runat="server"></asp:TextBox>
</asp:TableCell>
<asp:TableCell runat="server">
<asp:DropDownList ID="dpl0" runat="server">
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:PlaceHolder id="PlaceHolder1" runat="server">
<asp:Button ID="btn" runat="server" Text="提交" onclick="btn_Click" />
</asp:PlaceHolder><br/><br/>
</form>
</body>
</html>--------------后台.csprotected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ZNS"]);
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select id from t_user";
cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
dpl0.DataSource = ds.Tables[0].DefaultView;
dpl0.DataTextField = "id"; //dropdownlist的Text的字段
dpl0.DataValueField = "id";//dropdownlist的Value的字段
dpl0.DataBind();
}
else if (ViewState["Count"] != null)
{
for (int i = 0; i < Convert.ToInt16(ViewState["Count"]); i++)
AddTable();
}
if (IsPostBack)
{
if (ViewState["CountR"] != null)
{
for (int j = 0; j < Convert.ToInt16(ViewState["CountR"]); j++)
{
Button1_Click(sender, e);
}
}
}
} private void AddTable()
{
Table tb = new Table();
int count=0;
foreach (System.Web.UI.Control control in this.Form.Controls)
{
if (control is Table)
{
count++;
}
}
tb.ID = "tab" + count;
TableHeaderRow headerRow = new TableHeaderRow();
Button b = new Button();
b.ID = "btn" + count;
b.Text="增加措施";
b.Click += Button1_Click;
TableCell cell = new TableCell();
cell.Controls.Add(b);
headerRow.Cells.Add(cell);
tb.Attributes.Add("runat" ,"server");
tb.Controls.Add(headerRow);
AddTextBoxs(tb);
Page.Form.Controls.Add(tb); //PlaceHolder1.Controls.Add(tb);
}
private void AddTextBoxs(Table tab)
{
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TextBox t = new TextBox();
t.ID = tab.ID+"tb" + tab.Rows.Count;
tc1.Controls.Add(t);
TableCell tc2 = new TableCell();
DropDownList dpl = new DropDownList();
dpl.ID = tab.ID + "dpl" + tab.Rows.Count;
//for (int i = 0; i < 10; i++) dpl.Items.Add(i.ToString());
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ZNS"]);
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select id from t_user";
cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
dpl.DataSource = ds.Tables[0].DefaultView;
dpl.DataTextField = "id"; //dropdownlist的Text的字段
dpl.DataValueField = "id";//dropdownlist的Value的字段
dpl.DataBind(); tc2.Controls.Add(dpl);
tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
tab.Rows.Add(tr1 );
} protected void Button1_Click(object sender, EventArgs e)
{
Table tab = new Table();
Button bottun = (Button)sender;
tab.ID = bottun.Parent.Parent.Parent.ID;
//Response.Write(ftab.ID);
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TextBox t = new TextBox();
t.ID = tab.ID + "tb" + tab.Rows.Count;
tc1.Controls.Add(t);
TableCell tc2 = new TableCell();
DropDownList dpl = new DropDownList();
dpl.ID = tab.ID + "dpl" + tab.Rows.Count;
//for (int i = 0; i < 10; i++) dpl.Items.Add(i.ToString());
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ZNS"]);
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select id from t_user";
cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
dpl.DataSource = ds.Tables[0].DefaultView;
dpl.DataTextField = "id"; //dropdownlist的Text的字段
dpl.DataValueField = "id";//dropdownlist的Value的字段
dpl.DataBind(); tc2.Controls.Add(dpl);
tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
tab.Rows.Add(tr1);
//if (ViewState["Count"] == null) AddButton();
ViewState["CountR"] = Convert.ToInt16(ViewState["CountR"]) + 1;
} protected void btn_Click(object sender, EventArgs e)
{
''''保存
} protected void Button2_Click(object sender, EventArgs e)
{
AddTable();
ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
}
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<style type="text/css">
table
{
border-collapse: collapse;
border:solid 1px #93c2f1;
font-size:10pt;
}
th
{
border-collapse: collapse;
border:solid 1px #93c2f1;
font-size:10pt;
}
td
{
border-collapse: collapse;
border:solid 1px #93c2f1;
font-size:10pt;
}
</style>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="增加原因" />
<br /> <asp:Table id="tab0" runat="server">
<asp:TableRow runat="server" TableSection="TableHeader">
<asp:TableCell runat="server">
<asp:Button id="Button1" runat="server" Text="增加措施"
onclick="Button1_Click"></asp:Button>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="tr" runat="server">
<asp:TableCell runat="server">
<asp:TextBox ID="tb0" runat="server"></asp:TextBox>
</asp:TableCell>
<asp:TableCell runat="server">
<asp:DropDownList ID="dpl0" runat="server">
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:PlaceHolder id="PlaceHolder1" runat="server">
<asp:Button ID="btn" runat="server" Text="提交" onclick="btn_Click" />
</asp:PlaceHolder><br/><br/>
</form>
</body>
</html>--------------后台.csprotected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ZNS"]);
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select id from t_user";
cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
dpl0.DataSource = ds.Tables[0].DefaultView;
dpl0.DataTextField = "id"; //dropdownlist的Text的字段
dpl0.DataValueField = "id";//dropdownlist的Value的字段
dpl0.DataBind();
}
else if (ViewState["Count"] != null)
{
for (int i = 0; i < Convert.ToInt16(ViewState["Count"]); i++)
AddTable();
}
if (IsPostBack)
{
if (ViewState["CountR"] != null)
{
for (int j = 0; j < Convert.ToInt16(ViewState["CountR"]); j++)
{
Button1_Click(sender, e);
}
}
}
} private void AddTable()
{
Table tb = new Table();
int count=0;
foreach (System.Web.UI.Control control in this.Form.Controls)
{
if (control is Table)
{
count++;
}
}
tb.ID = "tab" + count;
TableHeaderRow headerRow = new TableHeaderRow();
Button b = new Button();
b.ID = "btn" + count;
b.Text="增加措施";
b.Click += Button1_Click;
TableCell cell = new TableCell();
cell.Controls.Add(b);
headerRow.Cells.Add(cell);
tb.Attributes.Add("runat" ,"server");
tb.Controls.Add(headerRow);
AddTextBoxs(tb);
Page.Form.Controls.Add(tb); //PlaceHolder1.Controls.Add(tb);
}
private void AddTextBoxs(Table tab)
{
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TextBox t = new TextBox();
t.ID = tab.ID+"tb" + tab.Rows.Count;
tc1.Controls.Add(t);
TableCell tc2 = new TableCell();
DropDownList dpl = new DropDownList();
dpl.ID = tab.ID + "dpl" + tab.Rows.Count;
//for (int i = 0; i < 10; i++) dpl.Items.Add(i.ToString());
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ZNS"]);
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select id from t_user";
cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
dpl.DataSource = ds.Tables[0].DefaultView;
dpl.DataTextField = "id"; //dropdownlist的Text的字段
dpl.DataValueField = "id";//dropdownlist的Value的字段
dpl.DataBind(); tc2.Controls.Add(dpl);
tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
tab.Rows.Add(tr1 );
} protected void Button1_Click(object sender, EventArgs e)
{
Table tab = new Table();
Button bottun = (Button)sender;
tab.ID = bottun.Parent.Parent.Parent.ID;
//Response.Write(ftab.ID);
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TextBox t = new TextBox();
t.ID = tab.ID + "tb" + tab.Rows.Count;
tc1.Controls.Add(t);
TableCell tc2 = new TableCell();
DropDownList dpl = new DropDownList();
dpl.ID = tab.ID + "dpl" + tab.Rows.Count;
//for (int i = 0; i < 10; i++) dpl.Items.Add(i.ToString());
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ZNS"]);
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select id from t_user";
cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
dpl.DataSource = ds.Tables[0].DefaultView;
dpl.DataTextField = "id"; //dropdownlist的Text的字段
dpl.DataValueField = "id";//dropdownlist的Value的字段
dpl.DataBind(); tc2.Controls.Add(dpl);
tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
tab.Rows.Add(tr1);
//if (ViewState["Count"] == null) AddButton();
ViewState["CountR"] = Convert.ToInt16(ViewState["CountR"]) + 1;
} protected void btn_Click(object sender, EventArgs e)
{
''''保存
} protected void Button2_Click(object sender, EventArgs e)
{
AddTable();
ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
}
AddTable();
放到
if(!IsPostBack)
之前
if (ViewState["CountR"] != null)
{
for (int j = 0; j < Convert.ToInt16(ViewState["CountR"]); j++)
{
Button1_Click(sender, e);
}
}
出错异常详细信息: System.InvalidCastException: 无法将类型为“ASP.xx_sm_aspx”的对象强制转换为类型“System.Web.UI.WebControls.Button”。源错误: protected void Button1_Click(object sender, EventArgs e)
{
Table tab = new Table();
Button bottun = (Button)sender;
tab.ID = bottun.Parent.Parent.Parent.ID;
用js生成之后,我不知道怎么绑定DDL和提交保存。
function addRow(id) {
var t = document.getElementById(id),
c = t.rows.length,
newRow = t.insertRow(-1),
newCell0 = newRow.insertCell(-1),
newCell1 = newRow.insertCell(-1);
newCell0.innerHTML = "<input type='text' ID='" + id + "t" + c + "' />";
var ddlst = document.createElement("select", "select");
ddlst.id = "ddlst";
ddlst.name = "ddlst"; var ddlList = document.body.getElementsById("d1");
var ds = test.GetDataSet().value;
newCell1.appendChild(ddlst);
}
function addTable() {
var t = document.createElement("table"),
count = document.getElementsByTagName("table").length + 1;
t.id = "tab" + count;
var c = document.createElement("caption");
c.innerHTML = "原因" + count;
document.body.appendChild(t);
t.appendChild(c);
newRow0 = t.insertRow(-1);
newCell = newRow0.insertCell(-1);
newCell.colspan = "2";
newCell.innerHTML = "<input type='button' value='增加措施' onclick='addRow(\"" + t.id + "\")'/>";
newRow1 = t.insertRow(-1);
newCell0 = newRow1.insertCell(-1);
newCell1 = newRow1.insertCell(-1);
newCell0.colspan = "2";
newCell1.colspan = "2";
var rc = t.rows.length;
newCell0.innerHTML = "<input type='text' ID='" + t.id + "t" + rc + "' />";
newCell1.innerHTML = "<select id='" + t.id + "d" + rc + "'></select>";
}
function addRow(id) {
var t = document.getElementById(id),
c = t.rows.length,
newRow = t.insertRow(-1),
newCell0 = newRow.insertCell(-1),
newCell1 = newRow.insertCell(-1); newCell0.innerHTML = "<input type='text' ID='" + id + "t" + c + "' />";
newCell1.innerHTML = "<select id='" + id + "d" + c + "'></select>";
var ddlst = document.createElement("select", "select");
ddlst.id = "ddlst";
ddlst.name = "ddlst"; var ddlList = document.body.getElementsById("d1");
var ds = test.GetDataSet().value;
newCell1.appendChild(ddlst);
}
Public Function DoGird(ByVal tableid As String) As String
Dim objAssembly As System.Reflection.Assembly
objAssembly = objAssembly.Load("QUE")
Dim typClassType As Type = objAssembly.GetType("QUE.ITR.ITR_STAFF_STATUS")
Dim table As DataTable = queStaff.getStaffFlowLinkInfo(Me.lb_staffid.Text, tableid, Session("FlowInfoBatchid"), Me.lb_processid.Text) ''获取干预环节信息数据
For Each row As DataRow In table.Rows
Dim status_role As Integer = row.Item("status_role")
row.Item("干预情况") = QUE.ITR.ITR_STAFF_STATUS.初始状态.GetName(typClassType, status_role)
Next
Dim str_header As String = "<table class=""ewTable"" cellspacing=""1"" cellpadding=""4"" rules=""all"" border=""0"" id=""" & tableid & """ style=""border-width:0px;width:100%;display:none""> " & _
"<tr><td class=""TableHeader"">干预人</td><td class=""TableHeader"" >角色</td><td class=""TableHeader"">干预时间</td><td class=""TableHeader"">课程名</td>" & _
"<td class=""TableHeader"">状态</td></tr>"
Dim str_end As String = "</table>"
If table.Rows.Count > 0 Then
Dim str As String = ""
Dim i As Integer = 1
''绑定数据到table中
For Each row As DataRow In table.Rows
str = str & "<tr class=""TableData"" onmouseover =""SetDataGridBackColor('LightBlue',false,'" & row("process_list_id") & "','" & row("intervenor_show") & "');style.cursor='default';btn_feedback.click();"" " & _
" onmouseout=""SetDataGridBackColor('white',true,'','');""><td >" & _
row("intervenor") & "</td><td >" & row("sso_role_name") & "</td><td >" & row("start_time") & _
"</td><td >" & row("course_name") & "</td><td>" & row("干预情况") & "</td></tr>"
Next
str = str & "<tr class=""TableLine3""><td> </td><td> </td><td> </td><td> </td><td> </td></tr>"
Return str_header & str & str_end
Else
Return str_header & "<tr class=""TableLine3""><td> </td><td> </td><td> </td><td> </td><td> </td></tr>" & str_end
End If
End Function
这样在后台绑定不可以吗?
AddTable()直接放到Page_Load里面就行了,但增加行的方法我不知道怎么写,因为需要知道增加的行所在的table具体是哪个,放在Page_Load里就不好确定所在的表式哪个。
试了几次,还是没能成功,希望你能我的代码运行一遍,这样我的问题你会更明白的我现在的主要问题是这里不知道怎么写
for (int j = 0; j < Convert.ToInt16(ViewState["CountR"]); j++)
{
AddTextBoxs(tab0);
}
AddTextBoxs里的参数只有是页面本来就有的tab0的时候才能加进table,其他动态生成的table都加不进行,搞不懂为什么
现在动态加了很多table,里面的row在PostBack之后没法重建了,因为在方法Page_Load里面没法指定row建在哪个table里
{
for (int j = 0; j < Convert.ToInt16(ViewState["CountR"]); j++)
{
if (control.name=="table_"+j)
{
table tb=control as table
AddTextBoxs(tb);
}
}
}
我想到一个办法,就是记录每个table的行数,而不是记录一个总行数