Repeater06-Repeater动态添加一行
来源:互联网 发布:广州大学生数据分析 编辑:程序博客网 时间:2024/06/05 02:53
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterToAdd.aspx.cs" Inherits="RepeaterToAdd" %><%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style> #tablePrint { width: 100%; margin-bottom: 5px; } #tablePrint, #tablePrint th, #tablePrint td { border: 1px solid #ccc; border-collapse: collapse; padding: 2px; } #tablePrint tr:nth-child(odd) { background-color: rgb(235, 240, 255); } .btnDiv { padding:5px 10px 10px 10px; } .dispalyNone { display:none; } </style></head><body> <form id="form1" runat="server"> <div> <div class="btnDiv"> <asp:Button ID="btnAddNewRow" runat="server" OnClick="btnAddNewRow_Click" Text="新增" /> <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="保存" /> </div> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table id="tablePrint" class="tbShow"> <tr class="th"> <td nowrap width="35px;" align="center">序号</td> <td nowrap>学校</td> <td nowrap>班级</td> <td nowrap>姓名</td> <td nowrap>性别</td> <td nowrap>手机</td> <td nowrap>邮箱</td> </tr> </HeaderTemplate> <ItemTemplate> <tr id="trIdrep" onmouseover="this.bgColor='#C4DFF7'" onmouseout="this.bgColor='#ffffff'"> <td nowrap> <%#Container.ItemIndex+1%> <asp:Label CssClass="dispalyNone" ID="idx" runat="server" Text='<%#Eval("idx")%>'></asp:Label> </td> <td> <asp:TextBox ID="userSchool" runat="server" Text='<%#Eval("userSchool")%>'></asp:TextBox></td> <td> <asp:TextBox ID="userClass" runat="server" Text='<%#Eval("userClass")%>'></asp:TextBox></td> <td> <asp:TextBox ID="userName" runat="server" Text='<%#Eval("userName")%>'></asp:TextBox></td> <td> <asp:TextBox ID="userSex" runat="server" Text='<%#Eval("userSex")%>'></asp:TextBox></td> <td> <asp:TextBox ID="userPhone" runat="server" Text='<%#Eval("userPhone")%>'></asp:TextBox></td> <td> <asp:TextBox ID="userEmail" runat="server" Text='<%#Eval("userEmail")%>'></asp:TextBox></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </div> </form></body></html>
后台代码:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class RepeaterToAdd : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = DataBindRepeater(); ViewState["DataTable"] = dt; this.Repeater1.DataSource = dt; this.Repeater1.DataBind(); } } private DataTable DataBindRepeater() { string strSql = "select * from UserInfo"; DataTable dt = SqlHelper.ExecuteDataset(CommandType.Text, strSql).Tables[0]; return dt; } protected void btnAddNewRow_Click(object sender, EventArgs e) { DataTable dt = DataBindRepeater(); dt.Clear(); //首先,恢复数据源 foreach (RepeaterItem item in Repeater1.Items) { DataRow newRow = dt.NewRow(); newRow["idx"] = ((Label)item.FindControl("idx")).Text.ToInt32(); newRow["userSchool"] = ((TextBox)item.FindControl("userSchool")).Text; newRow["userClass"] = ((TextBox)item.FindControl("userClass")).Text; newRow["userName"] = ((TextBox)item.FindControl("userName")).Text; newRow["userSex"] = ((TextBox)item.FindControl("userSex")).Text; newRow["userPhone"] = ((TextBox)item.FindControl("userPhone")).Text; newRow["userEmail"] = ((TextBox)item.FindControl("userEmail")).Text; dt.Rows.Add(newRow); } //添加一行 DataRow row = dt.NewRow(); dt.Rows.Add(row); //将页面中的数据缓存 ViewState["DataTable"] = dt; Repeater1.DataSource = dt; Repeater1.DataBind(); } protected void btnSave_Click(object sender, EventArgs e) { foreach (RepeaterItem item in this.Repeater1.Items) { //是否存在记录 int userIdx = ((Label)item.FindControl("idx")).Text.ToInt32(); int countNum = IsHaveRecord(((Label)item.FindControl("idx")).Text.ToInt32()); if (countNum == 0) { string strSql = @"insert into UserInfo (userSchool,userClass,userName,userSex,userPhone,userEmail) values(@userSchool,@userClass,@userName,@userSex,@userPhone,@userEmail); select @@identity ;"; int idx = SqlHelper.ExecuteScalar(CommandType.Text, strSql , new SqlParameter("@userSchool", ((TextBox)item.FindControl("userSchool")).Text) , new SqlParameter("@userClass", ((TextBox)item.FindControl("userClass")).Text) , new SqlParameter("@userName", ((TextBox)item.FindControl("userName")).Text) , new SqlParameter("@userSex", ((TextBox)item.FindControl("userSex")).Text) , new SqlParameter("@userPhone", ((TextBox)item.FindControl("userPhone")).Text) , new SqlParameter("@userEmail", ((TextBox)item.FindControl("userEmail")).Text) ).ToInt32(); } else { string strSql = @"update UserInfo set userSchool=@userSchool,userClass=@userClass,userName=@userName,userSex=@userSex, userPhone=@userPhone,userEmail=@userEmail where idx=@idx"; int rows = SqlHelper.ExecuteNonQuery(CommandType.Text, strSql , new SqlParameter("@userSchool", ((TextBox)item.FindControl("userSchool")).Text) , new SqlParameter("@userClass", ((TextBox)item.FindControl("userClass")).Text) , new SqlParameter("@userName", ((TextBox)item.FindControl("userName")).Text) , new SqlParameter("@userSex", ((TextBox)item.FindControl("userSex")).Text) , new SqlParameter("@userPhone", ((TextBox)item.FindControl("userPhone")).Text) , new SqlParameter("@userEmail", ((TextBox)item.FindControl("userEmail")).Text) , new SqlParameter("idx", ((Label)item.FindControl("idx")).Text.ToInt32()) ); } } Cmn.Js.AlertToUrl("操作成功", "RepeaterToAdd.aspx"); } private int IsHaveRecord(int idx) { string sql = "select count(0) from UserInfo where idx=@idx"; int countNum = SqlHelper.ExecuteScalar(CommandType.Text, sql, new SqlParameter("@idx", idx)).ToInt32(); if (countNum == 0) { return 0; } else { return 1; } }}
页面展示:
0 0
- Repeater06-Repeater动态添加一行
- repeater 动态添加一行
- repeater 动态添加一行
- repeater 动态添加一行
- repeater 动态添加一行
- repeater动态创建一行或者删除一行
- repeater动态添加行
- repeater 动态添加行
- 怎么给repeater 第一行添加样式
- Repeater動態添加一行刪除一行
- js 动态表格添加一行删除一行
- 表格动态添加删除一行
- 动态添加表格一行(javascript)
- jquery table动态添加一行
- Repeater 固定表头,动态添加行
- js动态表格添加一行删除一行保存一行
- 动态添加一行文本框,指定删除一行,取值
- js动态添加table表的一行和删除一行
- 第二十一课 I/O库
- zynq 加载bit方法总结
- Masonry的简单使用
- 保存图片控制器方法
- Linux中find常见用法示例
- Repeater06-Repeater动态添加一行
- hebernate,JPA select 子查询语句问题
- const和#define的区别
- mybatis的resultType
- 在ios开发中怎么获取应用崩溃日志
- Java对象在虚拟机中的创建过程
- web笔记
- for循环
- UE4 关卡流切换注意问题