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();
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章