时间:2021-04-16
添加gridview数据到前端
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Height="318px" Width="961px" AutoGenerateColumns="false"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowCreated="GridView1_RowCreated">
<Columns>
<asp:BoundField HeaderText="编号" DataField="Uid" ReadOnly="true" />
<asp:TemplateField HeaderText="姓名">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"UserName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"UserName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="操作" ShowEditButton="true" ShowCancelButton="true" ShowDeleteButton="true"
EditText="编辑" UpdateText="修改" DeleteText="<img style="text-decoration:none; border:0px;" src="images/delete.gif"onclick="JavaScript:return confirm ('确认删除吗?')" />" CancelText="取消" />
</Columns>
</asp:GridView>
<asp:Button ID="btnAdd" runat="server" Text="新增" OnClick="btnAdd_Click" />
</div>
</form>
后端处理增删该查事件
CEducationEntities context = new CEducationEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBind();
}
}
//绑定GridView数据
private void DataBind()
{
List<User> users = context.User.Where(m => m.Uid != string.Empty).ToList();
this.GridView1.DataSource = users;
this.GridView1.DataBind();
}
//编辑选中列表
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
int index = e.NewEditIndex;
GridView1.EditIndex = index;
DataBind();
}
//修改数据和插入更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex;
GridViewRow rows = this.GridView1.Rows[index];
string name = ((TextBox)rows.FindControl("txtName")).Text.Trim();
string ID = rows.Cells[0].Text;
if (!string.IsNullOrEmpty(ID) && ID != " ")
{
var sc = context.User.First(p => p.Uid.Equals(ID));
sc.UserName = name;
context.SaveChanges();
}
else
{
char[] pattern = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
int n = pattern.Length;
string result = "";
Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < 4; i++)
{
int rnd = random.Next(0, n);
result += pattern[rnd];
}
User u = new User()
{
Uid = Guid.NewGuid().ToString(),
Keyword = result,
UserName = ((TextBox)rows.FindControl("txtName")).Text.Trim(),
userLog = string.Empty
};
context.User.Add(u);
context.SaveChanges();
}
this.GridView1.EditIndex = -1;
DataBind();
}
//取消修改
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataBind();
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strUid = GridView1.Rows[e.RowIndex].Cells[0].Text;
User users = new User() { Uid = strUid };
context.User.Attach(users);
context.User.Remove(users);
context.SaveChanges();
DataBind();
}
//新增按钮
protected void btnAdd_Click(object sender, EventArgs e)
{
List<User> list = context.User.Where(m => m.Uid != string.Empty).ToList();
User firstUser = new User();
list.Insert(list.Count, firstUser);
this.GridView1.DataSource = list;
this.GridView1.EditIndex = list.Count - 1;
this.GridView1.DataBind();
}
//创建新行
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.EmptyDataRow)
{
e.Row.Cells[0].Enabled = false;
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#基于DBContext(EF)实现通用增删改查的REST方法,分享给大家供大家参考。具体如下:我们用ADO.NETEntityDataMode
列表代码删除添加修改用PHP向数据库中实现简单的增删改查Insert:username:password:Deleteusername:Areyousure&#
本文实例为大家分享了vue实现节点增删改功能的具体代码,供大家参考,具体内容如下效果:增删改功能tree.vue组件代码:{{model.name}}+修改ex
名片管理系统有两个模块组成:cards_main.py和cards_tools.py一个是主程序,另一个是封装增删改查函数的被调用程序代码如下cards_mai
ORM对象关系映射在数据库中,实现对数据的增删改查,使用的是SQ语句,在django中,通过python代码,实现对数据库的增删改查,这就是ORM。在pytho