Repeater动态增加行,PostBack保留数据

Repeater动态增加行,PostBack保留数据

前台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
 
<! 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="btnNew" runat="server" Text="新建" onclick="btnNew_OnClick" />
      < asp:Repeater  ID="rpCustomerInfo" runat="server"
             onitemcommand="rpCustomerInfo_ItemCommand">
      < HeaderTemplate >
      < table >
      < tr >
      < th >ID</ th >< th >Name</ th >< th >Telephone</ th >< th >RegisterDate</ th >< th >删除</ th >
      </ tr >
      </ HeaderTemplate >
      < ItemTemplate >
      < tr >
      < td >
          < asp:TextBox  ID="TextBox1" runat="server" Text='<%#Eval("ID")%>' ></ asp:TextBox ></ td >
          < td >< asp:TextBox  ID="TextBox2" runat="server" Text='<%#Eval("Name")%>' ></ asp:TextBox ></ td >
          < td >< asp:TextBox  ID="TextBox3" runat="server" Text='<%#Eval("Telephone")%>' ></ asp:TextBox ></ td >
          < td >< asp:TextBox  ID="TextBox4" runat="server" Text='<%#Eval("RegisterDate")%>' ></ asp:TextBox ></ td >
      < td >
          < asp:LinkButton  ID="lbnDelete" runat="server" CommandName="Delete" >删除</ asp:LinkButton >
      </ tr >
      </ ItemTemplate >
      < FooterTemplate >
      </ table >
      </ FooterTemplate >
      </ asp:Repeater >
     </ div >
     </ form >
</ body >
</ html >

 

后台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Data;
 
namespace  WebApplication1
{
     public  partial  class  WebForm2 : System.Web.UI.Page
     {
         protected  void  Page_Load( object  sender, EventArgs e)
         {
             if  (!IsPostBack)
             {
                 InitData();
                 DataBinds();
             }
         }
 
         protected  void  InitData()
         {
             DateTime time = DateTime.Now;
 
             DataTable dt = new  DataTable();
 
             dt.Columns.Add( "ID" , typeof ( string ));
             dt.Columns.Add( "Name" , typeof ( string ));
             dt.Columns.Add( "Telephone" , typeof ( string ));
             dt.Columns.Add( "RegisterDate" , typeof ( string ));
 
             for  ( int  i = 0; i < 2; i++)
             {
                 DataRow dr=dt.NewRow();
                 dr[ "ID" ] = i + 1;
                 dr[ "Name" ] = "user"  + (i + 1);
                 dr[ "Telephone" ] = "123456" ;
                 dr[ "RegisterDate" ] =time.AddDays(i * 10).ToString( "yyyy-MM-dd" );
                 dt.Rows.Add(dr);
             }
 
             ViewState.Add( "Data" ,dt);
         }
 
         protected  void  DataBinds()
         {
             rpCustomerInfo.DataSource=ViewState[ "Data" ] as  DataTable;
             rpCustomerInfo.DataBind();
         }
 
         protected  DataTable CopyFormData()
         {
             DataTable dt = (ViewState[ "Data" ] as  DataTable).Clone();
 
             foreach  (RepeaterItem ri in  rpCustomerInfo.Items)
             {
                 DataRow dr = dt.NewRow();
 
                 dr[ "ID" ]=(ri.FindControl( "TextBox1" ) as  TextBox).Text;
                 dr[ "Name" ] = (ri.FindControl( "TextBox2" ) as  TextBox).Text;
                 dr[ "Telephone" ] = (ri.FindControl( "TextBox3" ) as  TextBox).Text;
                 dr[ "RegisterDate" ] = (ri.FindControl( "TextBox4" ) as  TextBox).Text;
                 dt.Rows.Add(dr);
             }
 
             return  dt;
         }
 
         protected  void  btnNew_OnClick( object  sender, EventArgs e)
         {
             DataTable dt = CopyFormData();
 
             DataRow dr = dt.NewRow();
 
             dt.Rows.Add(dr);
 
             ViewState.Add( "Data" ,dt);
 
             DataBinds();
         }
 
         protected  void  rpCustomerInfo_ItemCommand( object  source, RepeaterCommandEventArgs e)
         {
             if  (e.CommandName== "Delete" )
             {
                 DataTable dt = CopyFormData();
 
                 dt.Rows.RemoveAt(e.Item.ItemIndex);
 
                 ViewState.Add( "Data" , dt);
 
                 DataBinds();
             }
         }
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现这个功能可以通过以下步骤: 1. 将上传的文件保存到服务器指定的文件夹中,可以使用`FileUpload`控件来实现文件上传。 2. 在上传完成后,将文件的相关信息存储到数据库中,如文件名、路径、大小、上传时间等。 3. 在页面上显示已上传的文件列表,可以使用`GridView`或`Repeater`控件来展示,同时从数据库中查询文件信息并绑定到控件上。 4. 如果需要在页面上动态添加数据,可以使用`GridView`的`DataSource`属性绑定数据源,然后通过`GridView.Rows.Add()`方法添加数据,最后调用`GridView.DataBind()`方法将数据展示出来。 下面是一个示例代码,用于在上传文件后向`GridView`中添加数据: ```c# protected void btnUpload_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { // 保存文件到指定路径 string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string filePath = Server.MapPath("~/Uploads/") + fileName; FileUpload1.SaveAs(filePath); // 将文件信息保存到数据库中 string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO Files (FileName, FilePath, FileSize, UploadTime) VALUES (@FileName, @FilePath, @FileSize, @UploadTime)", conn); cmd.Parameters.AddWithValue("@FileName", fileName); cmd.Parameters.AddWithValue("@FilePath", filePath); cmd.Parameters.AddWithValue("@FileSize", FileUpload1.PostedFile.ContentLength); cmd.Parameters.AddWithValue("@UploadTime", DateTime.Now); cmd.ExecuteNonQuery(); } // 添加数据到GridView中 DataTable dt = (DataTable)GridView1.DataSource; DataRow dr = dt.NewRow(); dr["FileName"] = fileName; dr["FilePath"] = filePath; dr["FileSize"] = FileUpload1.PostedFile.ContentLength; dr["UploadTime"] = DateTime.Now; dt.Rows.Add(dr); GridView1.DataBind(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值