GridView的绑定,编辑,删除功能 (非常棒)

html代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RightLeftGongGao.aspx.cs" Inherits="Manager" %>

<%@ Register Src="UserControls/lrmovenews.ascx" TagName="lrmovenews" TagPrefix="uc1" %>

<!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>aaa</title>

</head>
<body>
<form id="form1" runat="server">
<table border="0" cellpadding="0" cellspacing="0" style="width: 780px; height: 200px">
<tr>
<td style="width: 780px; height: 25px" align="center" bgcolor="#ffe0c0">
新闻</td>
</tr>
<tr>
<td style="width: 780px; height: 175px">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="100%"
Width="780px" DataKeyNames="rl_bianhao" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" BackColor="#FFE0C0">
<Columns>
<asp:TemplateField HeaderText="编号" SortExpression="rl_bianhao">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("rl_bianhao") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("rl_bianhao") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="标题" SortExpression="rl_title">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("rl_title") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("rl_title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="价格" SortExpression="rl_price">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("rl_price") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("rl_price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="添加时间" SortExpression="rl_time">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("rl_time") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("rl_time") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="Edit" HeaderText="编辑" ShowHeader="True"
Text="编辑" />
<asp:TemplateField HeaderText="取消">
<ItemTemplate>
<asp:Button ID="Button2" runat="server" CausesValidation="false" CommandName="Cancel"
Text="取消" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="更新">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Update"
Text="更新" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:Button ID="Button3" runat="server" CausesValidation="false" CommandName="Delete"
Text="删除" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<table border="1" cellpadding="0" cellspacing="0" style="width: 780px; height: 122px" bgcolor="#ffe0c0">
<tr>
<td style="width: 200px; text-align: center;">
编号</td>
<td style="width: 200px; text-align: center;">
标题</td>
<td style="width: 200px; text-align: center">
价格</td>
<td style="width: 180px; text-align: center">
添加的时间</td>
</tr>
<tr>
<td style="width: 200px; text-align: center;" align="center">
<asp:TextBox ID="txt_bianhao" runat="server"></asp:TextBox></td>
<td style="width: 200px; text-align: center;" align="center">
<asp:TextBox ID="txt_title" runat="server"></asp:TextBox></td>
<td style="width: 200px; text-align: center" align="center">
<asp:TextBox ID="txt_price" runat="server"></asp:TextBox></td>
<td style="width: 180px; text-align: center" align="center">
<asp:Label ID="lab_time" runat="server" Width="116px">点击按钮后自动显示</asp:Label></td>
</tr>
<tr>
<td colspan="4" style="text-align: center" align="center">
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="添 加" Width="107px" />
<asp:Label ID="lab_tishi" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="width: 200px">
</td>
<td style="width: 200px">
</td>
<td style="width: 200px">
</td>
<td style="width: 180px">
</td>
</tr>
</table>
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" bgcolor="#ccccff" height="200" width="780">
<tr>
<td colspan="2" rowspan="4" valign="top" style="width: 763px; height: 242px;">
<table border="0" cellpadding="0" cellspacing="0" height="200" width="780">
<tr>
<td style="width: 100px; height: 238px" valign="top">
<table border="1" cellpadding="0" cellspacing="0" height="200" width="400">
<tr>
<td colspan="2" style="height: 10px" align="center">
公告新闻(添加删除)</td>
</tr>
<tr>
<td style="width: 70px; height: 30px;">
公告编号:</td>
<td style="width: 200px; height: 30px;">
<asp:TextBox ID="txt_gg_bianhao" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 70px; height: 140px;">
公告内容:</td>
<td style="width: 200px; height: 140px;">
<asp:TextBox ID="txt_gg_content" runat="server" TextMode="MultiLine" Height="100px" Width="300px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="height: 20px" align="center">
<asp:Button ID="gg_add" runat="server" OnClick="gg_add_Click" Text="添 加" />
<asp:Button ID="gg_del" runat="server" OnClick="gg_del_Click" Text="删 除" />
<asp:Button ID="gg_sel" runat="server" Text="查询公告(输入编号)" OnClick="gg_sel_Click" />
<asp:Label ID="Label3" runat="server"></asp:Label></td>
</tr>
</table>
</td>
<td style="width: 100px; height: 238px" valign="top">
<table border="1" cellpadding="0" cellspacing="0" height="200" width="380">
<tr>
<td style="width: 100px; height: 10px" align="center">
公告新闻显示</td>
</tr>
<tr>
<td style="width: 100px; height: 190px;" valign="top">
编号:<asp:Label ID="lab_gonggaoid" runat="server"></asp:Label><br />
<asp:Label ID="lab_gonggao" runat="server" Height="165px" Width="375px"></asp:Label></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>
.cs代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Manager : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = DB.Createconnection();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select rl_bianhao,rl_title,rl_price,rl_time from RLNews", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();

con.Open();
SqlCommand cmd = new SqlCommand("select gonggao_id,content from GongGao", con);
SqlDataReader sdr = cmd.ExecuteReader();
if(sdr.Read())
{
lab_gonggaoid.Text = sdr.GetValue(0).ToString();
lab_gonggao.Text = sdr.GetValue(1).ToString();
}
con.Close();
}
}
private void setbind()
{
SqlConnection con = DB.Createconnection();
SqlDataAdapter da = new SqlDataAdapter("select * from RLNews", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button4_Click(object sender, EventArgs e)
{
if (txt_bianhao.Text.Trim() == "")
{
lab_tishi.Text = "编号不能为空!";
lab_tishi.ForeColor = System.Drawing.Color.Red;
}
else
{
SqlConnection con = DB.Createconnection();
con.Open();
SqlCommand cmd = new SqlCommand("select * from RLNews where rl_bianhao=@rl_bianhao", con);
cmd.Parameters.AddWithValue("@rl_bianhao", txt_bianhao.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
lab_tishi.Text = "编号已存在!";
lab_tishi.ForeColor = System.Drawing.Color.Red;
dr.Close();
}
else
{
dr.Close();
lab_time.Text = DateTime.Now.ToString();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = con;
cmd1.CommandText = "insert into RLNews(rl_bianhao,rl_title,rl_price,rl_time)values(@rl_bianhao,@rl_title,@rl_price,@rl_time)";
cmd1.Parameters.AddWithValue("@rl_bianhao", txt_bianhao.Text);
cmd1.Parameters.AddWithValue("@rl_title", txt_title.Text);
cmd1.Parameters.AddWithValue("@rl_price", Convert.ToDouble(txt_price.Text));
cmd1.Parameters.AddWithValue("@rl_time", lab_time.Text);
cmd1.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
setbind();
lab_tishi.Text = "添加成功!";
lab_tishi.ForeColor = System.Drawing.Color.Red;
}
con.Close();
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
int i = e.RowIndex;
GridViewRow grv = GridView1.Rows[i];
string bianhao = (grv.FindControl("TextBox5") as TextBox).Text;
string title = (grv.FindControl("TextBox2") as TextBox).Text;
string price = (grv.FindControl("TextBox1") as TextBox).Text;
string time = (grv.FindControl("TextBox4") as TextBox).Text;
SqlConnection con = DB.Createconnection();
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "update RLNews set rl_title=@rl_title,rl_price=@rl_price,rl_time=@rl_time where rl_bianhao=@rl_bianhao";
cmd.Parameters.AddWithValue("@rl_bianhao", bianhao);
cmd.Parameters.AddWithValue("@rl_title", title);
cmd.Parameters.AddWithValue("@rl_price", Convert.ToDouble(price));
cmd.Parameters.AddWithValue("@rl_time", time);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
setbind();
}
catch
{
Response.Write("<script>alert('请先编辑!')</script>");
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
setbind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int i = e.RowIndex;
string bianhao = GridView1.DataKeys[i].Value.ToString();
SqlConnection con = DB.Createconnection();
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "delete from RLNews where rl_bianhao=@rl_bianhao";
cmd.Parameters.AddWithValue("@rl_bianhao", bianhao);
cmd.ExecuteNonQuery();
con.Close();
setbind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
setbind();
}
protected void gg_add_Click(object sender, EventArgs e)
{
if (txt_gg_bianhao.Text.ToString() == "")
{
Label3.Text = "请填写编号!";
Label3.ForeColor = System.Drawing.Color.Red;
}
else
{
SqlConnection con = DB.Createconnection();
con.Open();
SqlCommand cmd = new SqlCommand("select gonggao_id from GongGao where gonggao_id=@id", con);
cmd.Parameters.AddWithValue("@id",txt_gg_bianhao.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Label3.Text = "编号已存在!";
Label3.ForeColor = System.Drawing.Color.Red;
dr.Close();
}
else
{
dr.Close();
SqlCommand cmd2 = new SqlCommand("insert into GongGao values(@id,@content)", con);
cmd2.Parameters.AddWithValue("@id", txt_gg_bianhao.Text);
cmd2.Parameters.AddWithValue("@content", txt_gg_content.Text);
cmd2.ExecuteNonQuery();
con.Close();
Label3.Text = "添加成功!";
Label3.ForeColor = System.Drawing.Color.Green;
}
}
}
protected void gg_del_Click(object sender, EventArgs e)
{
if (txt_gg_bianhao.Text.ToString() == "")
{
Label3.Text = "请填写编号!";
Label3.ForeColor = System.Drawing.Color.Red;
}
else
{
SqlConnection con = DB.Createconnection();
con.Open();
SqlCommand cmd = new SqlCommand("delete from GongGao where gonggao_id=@id", con);
cmd.Parameters.AddWithValue("@id", txt_gg_bianhao.Text);
cmd.ExecuteNonQuery();
con.Close();
Label3.Text = "删除成功!";
Label3.ForeColor = System.Drawing.Color.Red;
}
}
protected void gg_sel_Click(object sender, EventArgs e)
{
SqlConnection con = DB.Createconnection();
con.Open();
SqlCommand cmd = new SqlCommand("select gonggao_id,content from GongGao where gonggao_id=@id", con);
cmd.Parameters.AddWithValue("@id", txt_gg_bianhao.Text);
cmd.ExecuteNonQuery();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
lab_gonggaoid.Text = sdr.GetValue(0).ToString();
lab_gonggao.Text = sdr.GetValue(1).ToString();
}
con.Close();
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值