ASP.NET—002:GridView手動增加一行
阿新 • • 發佈:2019-02-04
ASP.NET中的gridview如何增加一行呢,下面介紹一種最簡單的方式。
只使用後臺的資料,在後臺的datatable或者list增加一項,然後重新繫結gridview。
直接看程式碼
效果:
實體類
頁面管理類
前臺
後臺
當然一般來說,使用js增加比較正規一些,下一篇來介紹。
程式碼下載
只使用後臺的資料,在後臺的datatable或者list增加一項,然後重新繫結gridview。
直接看程式碼
效果:
實體類
public class PersonModel { private int personIndex; public int PersonIndex { get { return personIndex; } set { personIndex = value; } } private string personID; public string PersonID { get { return personID; } set { personID = value; } } private string personName; public string PersonName { get { return personName; } set { personName = value; } } private string personSex; public string PersonSex { get { return personSex; } set { personSex = value; } } private int personAge; public int PersonAge { get { return personAge; } set { personAge = value; } } private bool personSelected = false; public bool PersonSelected { get { return personSelected; } set { personSelected = value; } } }
頁面管理類
public class MainManager { private int sIndex = 0; private List<PersonModel> personCollect = new List<PersonModel>(); private static MainManager instance = null; public List<PersonModel> PersonCollect { get { return personCollect; } set { personCollect = value; } } public static MainManager DoGetInstance() { if (instance == null) { instance = new MainManager(); } return instance; } public void DoAddSinglePersons() { sIndex = this.PersonCollect.Count; PersonModel model = new PersonModel(); model.PersonIndex = sIndex; model.PersonID = System.Guid.NewGuid().ToString(); model.PersonName = "測試" + sIndex; model.PersonAge = 17 + sIndex; model.PersonSex = sIndex % 2 == 0 ? "男" : "女"; model.PersonSelected = false; this.PersonCollect.Add(model); } }
前臺
<head runat="server"> <title></title> </head> <body id="MyBody" runat="server" ms_positioning="GridLayout"> <form id="Form1" runat="server" method="post"> <div> <asp:Button ID="btnAdd" runat="server" OnClick="AddItem_Click" Text="增加" /> <asp:GridView ID="dgPersons" runat="server" AutoGenerateColumns="False" EnableViewState="false" CellPadding="4" ForeColor="#333333" DataKeyNames="PersonID"> <Columns> <asp:CommandField ShowSelectButton="True" /> <asp:BoundField DataField="PersonIndex" HeaderText="序號"/> <asp:TemplateField> <ItemTemplate> <input id="radiobutton1" name="pselect" type="radio" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="PersonName" HeaderText="姓名" /> <asp:BoundField DataField="PersonAge" HeaderText="年齡" /> <asp:BoundField DataField="PersonSex" HeaderText="性別" /> </Columns> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#999999" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView> </div> </form> </body>
後臺
public partial class _Default : System.Web.UI.Page
{
private MainManager dManager = null;
protected PersonModel selectItem = null;
protected void Page_Load(object sender, EventArgs e)
{
dManager = MainManager.DoGetInstance();
if (!IsPostBack)
{
dManager.DoAddSinglePersons();
this.dgPersons.DataSource = dManager.PersonCollect;
this.dgPersons.DataBind();
}
}
protected void AddItem_Click(object sender, EventArgs e)
{
dManager.DoAddSinglePersons();
this.dgPersons.DataSource = dManager.PersonCollect;
this.dgPersons.DataBind();
}
}
如果一行中有其他list控制元件比如 下拉列表asp:DropDownList,那麼在實體中增加一個list,對應下拉列表的繫結。而且對於每一行的點選,選擇等操作都要寫相應的事件修改資料,下一次繫結時要將list中的資料修改成操作過的值或狀態,再進行繫結。當然一般來說,使用js增加比較正規一些,下一篇來介紹。
程式碼下載