如何动态创建每个按钮上的表行在c#中单击? [英] how to dynamic create table row on each button click in c#?
本文介绍了如何动态创建每个按钮上的表行在c#中单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在c#中单击每个按钮动态创建表行?
解决方案
单击 [ ^ ]
on .aspx页面我创建了一个包含表格的占位符。
<% @ Page 语言 = C# AutoEventWireup = true CodeBehind = WebForm2.aspx.cs 继承 = WebApplication2。 WebForm2 EnableViewState = false %>
< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >
< html < span class =code-attribute> xmlns = http://www.w3.org/1999/xhtml >
< head runat = 服务器 >
< title > < / title >
< span class =code-keyword>< / head >
< body >
< 表格 id = form1 runat = 服务器 >
< div >
< asp:按钮 ID = btnSubmit runat = server 文字 = 提交 OnClick = btnSubmit_Click / >
< / div >
< asp:PlaceHolder ID = < span class =code-keyword> resultHolder runat = server > < / asp:PlaceHolder >
< / form >
< / body >
< / html >
在页面后面的代码上定义逻辑
使用系统;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
命名空间 WebApplication2
{
public partial class WebForm2:System.Web.UI.Page
{
static 表tableResult;
受保护 void Page_Load(对象发件人,EventArgs e)
{
如果(!Page.IsPostBack)
{
tableResult = new Table();
tableResult.GridLines = GridLines.Both;
TableHeaderRow dr = new TableHeaderRow();
TableHeaderCell dc = new TableHeaderCell();
dc.Text = 名称;
dr.Cells.Add(dc);
tableResult.Rows.Add(dr);
}
}
受保护 void btnSubmit_Click( object sender,EventArgs e)
{
TableRow dr = new TableRow() ;
TableCell dc = new TableCell();
dc.Text = Sandeep;
dr.Cells.Add(dc);
tableResult.Rows.Add(dr);
resultHolder.Controls.Add(tableResult);
}
}
}
这是basci逻辑要在按钮单击上添加一行,您可以根据需要动态更改单元格的文本。
谢谢
Form.aspx
< pre lang =xml> < asp:UpdatePanel ID = UpdatePanel1 runat = server >
< ContentTemplate >
< asp:Panel ID < span class =code-keyword> = panelTable runat = server >
< / asp:Panel >
< span class = style1 >
< asp:按钮 ID = btnAddRow runat = server 文字 = 添加更多.. OnClick = btnAddRow_Click / >
< / ContentTemplate >
< / asp:UpdatePanel >
Form.cs
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;使用System.Drawing
;
使用System.IO;
使用BusinessLayerLogic.Registration;
使用System.Text;
使用System.Configuration;
使用System.Data;
公共部分类表格:System.Web.UI.Page
{
#region变量
//将保留的全局变量当前行数
//我们将值设置为2,以便在页面加载时生成默认行
private int numOfRows = 2;
#endregion
#regionPageLoad
protected void Page_Load(object sender,EventArgs e)
{
try
{
// GenerateTable(numOfRows);
if(!IsPostBack)
{
GenerateTable(numOfRows);
}
#endregion
}
catch(exception ex)
{
}
}
#endregion
#region按钮点击事件
protected void btnAddRow_Click(object sender,EventArgs e)
{
if(ViewState [RowsCount]!= null)
{
numOfRows = Convert.ToInt32 (ViewState的[ RowsCount]的ToString());
GenerateTable(numOfRows);
}
}
#endregion
#region方法/函数
//动态获取previos表中的工作实验数据,并将其设置在新生成的表中
private void SetPreviousData(int rowsCount,int colsCount)
{
rowsCount--;
try
{
Table table =(Table)Page.FindControl(Table1);
if(table!= null)
{
for(int i = 0; i< rowsCount; i ++)
{
for(int j = 0; j< colsCount; j ++)
{
//从表中提取动态控制
TextBox tb =(TextBox)table.Rows [i] .Cells [j] .FindControl(TextBoxRow_+ i +Col_+ j);
//使用Request对象获取动态文本框的先前数据
// if(j == 0)
// {
// int Sno = i;
// tb.Text = Convert.ToString(++ Sno);
//}
// else
tb.Text = Request.Form [TextBoxRow_+ i +Col_+ j];
}
}
}
}
catch(exception ex)
{
throw ex;
}
}
//为表工作实验量化表格并添加到页面
private void GenerateTable(int rowsCount)
{
Table table = new Table();
table.ID =Table1;
panelTable.Controls.Add(table);
//要生成的列数
const int colsCount = 6;
//将行行添加到表
TableHeaderRow headRow = new TableHeaderRow();
headRow.BackColor = Color.Gray;
headRow.ForeColor = Color.WhiteSmoke;
headRow.Height = 33;
TableHeaderCell headCell = new TableHeaderCell();
headCell.Text =S.No;
headCell.Width = 10;
headRow.Cells.Add(headCell);
TableHeaderCell headCell1 = new TableHeaderCell();
headCell1.Text =Ogranisation的名称;
headRow.Cells.Add(headCell1);
TableHeaderCell headCell6 = new TableHeaderCell();
headCell6.Text =指定和主要经验领域;
headRow.Cells.Add(headCell6);
TableHeaderCell headCel3 = new TableHeaderCell();
headCel3.Text =主要成就;
headRow.Cells.Add(headCel3);
TableHeaderCell headCell4 = new TableHeaderCell();
headCell4.Text =经验来自;
headRow.Cells.Add(headCell4);
TableHeaderCell headCell5 = new TableHeaderCell();
headCell5.Text =体验到;
headRow.Cells.Add(headCell5);
table.Rows.Add(headRow);
//现在遍历表并添加控件
for(int i = 0; i< rowsCount; i ++)
{
TableRow row = new TableRow();
for(int j = 0; j< colsCount; j ++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
//为每个添加的TextBox设置一个唯一的ID
tb.ID =TextBoxRow_+ i +Col_+ j;
tb.Height = 25;
if(j == 2)
{
tb.Width = 228;
}
//将控件添加到TableCell
cell.Controls.Add(tb);
//将TableCell添加到TableRow
row.Cells.Add(cell);
}
//最后,将TableRow添加到表
table.Rows.Add(行);
}
//在PostBacks上设置以前的数据
SetPreviousData(rowsCount,colsCount);
//将当前行数存储在ViewState中
rowsCount ++;
ViewState [RowsCount] = rowsCount;
}
#endregion
}
how to dynamic create table row on each button click in c#?
解决方案
click[^]
on .aspx page I create a placeholder that will contain a Table.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" EnableViewState="false" %> <!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="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /> </div> <asp:PlaceHolder ID="resultHolder" runat="server"></asp:PlaceHolder> </form> </body> </html>
There after define logic on code behind page
using System; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class WebForm2 : System.Web.UI.Page { static Table tableResult; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { tableResult = new Table(); tableResult.GridLines = GridLines.Both; TableHeaderRow dr = new TableHeaderRow(); TableHeaderCell dc = new TableHeaderCell(); dc.Text = "Name"; dr.Cells.Add(dc); tableResult.Rows.Add(dr); } } protected void btnSubmit_Click(object sender, EventArgs e) { TableRow dr = new TableRow(); TableCell dc = new TableCell(); dc.Text = "Sandeep"; dr.Cells.Add(dc); tableResult.Rows.Add(dr); resultHolder.Controls.Add(tableResult); } } }
This is basci logic to add a row on button click you can change dynamically text for cell as you want.
Thanks
Form.aspx
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Panel ID="panelTable" runat="server"> </asp:Panel> <span class="style1"> <asp:Button ID="btnAddRow" runat="server" Text="Add More.." OnClick="btnAddRow_Click" /> </ContentTemplate> </asp:UpdatePanel>
Form.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.IO; using BusinessLayerLogic.Registration; using System.Text; using System.Configuration; using System.Data; public partial class Form : System.Web.UI.Page { #region "Variable" //A global variable that will hold the current number of Rows //We set the values to 2 so that it will generate a default Row when the page loads private int numOfRows = 2; #endregion #region "PageLoad" protected void Page_Load(object sender, EventArgs e) { try { //GenerateTable(numOfRows); if (!IsPostBack) { GenerateTable(numOfRows); } #endregion } catch (Exception ex) { } } #endregion #region "Button Click Event" protected void btnAddRow_Click(object sender, EventArgs e) { if (ViewState["RowsCount"] != null) { numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString()); GenerateTable(numOfRows); } } #endregion #region "Methods/Functions" //Dyanamically gets the data of work experiance from previos table and set it in the newly Genrated Table private void SetPreviousData(int rowsCount, int colsCount) { rowsCount--; try { Table table = (Table)Page.FindControl("Table1"); if (table != null) { for (int i = 0; i < rowsCount; i++) { for (int j = 0; j < colsCount; j++) { //Extracting the Dynamic Controls from the Table TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j); //Use Request objects for getting the previous data of the dynamic textbox //if (j == 0) //{ // int Sno = i; // tb.Text = Convert.ToString(++Sno); //} //else tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j]; } } } } catch (Exception ex) { throw ex; } } //Genrates the table dyanmically for the Table Work Experiance and add the to the page private void GenerateTable(int rowsCount) { Table table = new Table(); table.ID = "Table1"; panelTable.Controls.Add(table); //The number of Columns to be generated const int colsCount = 6; //adding the head Row to the Table TableHeaderRow headRow = new TableHeaderRow(); headRow.BackColor = Color.Gray; headRow.ForeColor = Color.WhiteSmoke; headRow.Height = 33; TableHeaderCell headCell = new TableHeaderCell(); headCell.Text = "S.No"; headCell.Width = 10; headRow.Cells.Add(headCell); TableHeaderCell headCell1 = new TableHeaderCell(); headCell1.Text = "Name of Ogranisation"; headRow.Cells.Add(headCell1); TableHeaderCell headCell6 = new TableHeaderCell(); headCell6.Text = "Designation & Key Area of Experience"; headRow.Cells.Add(headCell6); TableHeaderCell headCel3 = new TableHeaderCell(); headCel3.Text = "Key Achievements"; headRow.Cells.Add(headCel3); TableHeaderCell headCell4 = new TableHeaderCell(); headCell4.Text = "Experience From"; headRow.Cells.Add(headCell4); TableHeaderCell headCell5 = new TableHeaderCell(); headCell5.Text = "Experience To"; headRow.Cells.Add(headCell5); table.Rows.Add(headRow); // Now iterate through the table and add your controls for (int i = 0; i < rowsCount; i++) { TableRow row = new TableRow(); for (int j = 0; j < colsCount; j++) { TableCell cell = new TableCell(); TextBox tb = new TextBox(); // Set a unique ID for each TextBox added tb.ID = "TextBoxRow_" + i + "Col_" + j; tb.Height = 25; if (j == 2) { tb.Width = 228; } // Add the control to the TableCell cell.Controls.Add(tb); // Add the TableCell to the TableRow row.Cells.Add(cell); } // And finally, add the TableRow to the Table table.Rows.Add(row); } //Set Previous Data on PostBacks SetPreviousData(rowsCount, colsCount); //Store the current Rows Count in ViewState rowsCount++; ViewState["RowsCount"] = rowsCount; } #endregion }
这篇关于如何动态创建每个按钮上的表行在c#中单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文