WebForm1:
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm_b.WebForm1" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="请输入商品名"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Height="20px" Width="165px"></asp:TextBox>
<%--<asp:DropDownList ID="DropDownList1" runat="server" Height="26px" Width="76px">
<asp:ListItem>¥111</asp:ListItem>
<asp:ListItem>¥100</asp:ListItem>
<asp:ListItem>$200</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" Height="26px">
<asp:ListItem>China</asp:ListItem>
<asp:ListItem>the USA</asp:ListItem>
</asp:DropDownList>--%>
<asp:Button ID="Button1" runat="server" Height="25px" OnClick="Button1_Click" Text="查询" Width="47px" />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="商品名:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Height="22px" Width="132px"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="价格:"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" Height="24px" Width="59px">
<asp:ListItem>¥</asp:ListItem>
<asp:ListItem>$</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox4" runat="server" Height="24px" Width="117px"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server" Height="25px"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Height="24px" OnClick="Button2_Click" Text="增加" Width="42px" />
<br />
<br />
<br />
</div>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Height="169px" Width="734px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="name" HeaderText="商品名" />
<asp:BoundField DataField="price" HeaderText="商品价格" />
<asp:BoundField DataField="place" HeaderText="售卖地区" />
<asp:BoundField DataField="action" HeaderText="Action" />
<asp:ButtonField CommandName="Delete" Text="删除" />
<asp:ButtonField CommandName="Update" Text="修改" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
</form>
</body>
</html>
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebForm_b
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//DataSet data = SQLHelper.GetTable("select * from WebForm_Study_Test", "WebForm_Study_Test");
DataSet data = GetTable("select * from WebForm_Study_Test", "WebForm_Study_Test");
GridView1.DataSource = data;
GridView1.DataBind();
}
TextBox5.Visible = false;
}
string connstr= "server=10.6.10.151;database=ITTest;uid=ittest;pwd=ittest";
//显示出数据库中数据
public DataSet GetTable(string sql, string tableName)
{ //传入 sql语句,表名tableName
SqlConnection connection = new SqlConnection(connstr);
connection.Open();
DataSet data = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
adapter.Fill(data, tableName);
connection.Close();
return data;
}
//“查询”按钮的事件
protected void Button1_Click(object sender, EventArgs e)
{
string text = TextBox1.Text;
if (text!="")
{
string sql = "select * from WebForm_Study_Test where name='" + text + "'";
DataSet data = GetTable(sql, "WebForm_Study_Test");
GridView1.DataSource = data;
GridView1.DataBind();
}
else
{
Response.Write("<script>alert('请输入内容!');</script>");
}
}
public Boolean Delete_Update(string sql)//删除后更新数据表
{ //传入sql语句
SqlConnection connection = new SqlConnection(connstr);
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
int n = command.ExecuteNonQuery();
if (n > 0)
{
connection.Close();
return true;
}
else
{
connection.Close();
return false;
}
}
//删除、编辑框
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")//删除按钮的操作
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
string ID = row.Cells[0].Text.ToString();
string sql = "delete WebForm_Study_Test where name='" + ID + "'";
Response.Write("<script>alert('确定删除吗?');</script>");
if (Delete_Update(sql) == true)
{
Response.Write("<script>alert('删除成功');</script>");
//重新绑定GridView控件
DataSet data = GetTable("select * from WebForm_Study_Test", "WebForm_Study_Test");
GridView1.DataSource = data;
GridView1.DataBind();
}
else
{
Response.Write("<script>alert('删除失败');</script>");
}
}
if (e.CommandName == "Update")//修改/编辑按钮的操作
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
string name = row.Cells[0].Text.ToString();
string price = row.Cells[1].Text.ToString();
string place = row.Cells[2].Text.ToString();
string action = row.Cells[3].Text.ToString();
Session["name"] = name; //Session传值
Session["price"] = price;
Session["place"] = place;
Session["action"] = action;
Response.Redirect("Edit.aspx"); //页面跳转到 修改.aspx
}
}
//“增加按钮”--即插入
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox2.Text==""|| TextBox4.Text=="")
{
Response.Write("<script>alert('请输入要添加的内容!');</script>");
}
else
{
if (getConut() != 0)//判断是否添加了相同的记录
{
Response.Write("alert('已存在此面板!')");
}
else
{
try
{
SqlConnection sqlcon = new SqlConnection(connstr);
sqlcon.Open();
string InsertSql = "insert into WebForm_Study_Test(name,price,place) values ('" + TextBox2.Text + "','" + (DropDownList1.SelectedValue + TextBox4.Text) + "','" + type(DropDownList1.SelectedValue) + "')";
SqlCommand com = new SqlCommand(InsertSql, sqlcon);
com.ExecuteNonQuery();
SqlDataAdapter ada = new SqlDataAdapter("select * from WebForm_Study_Test", sqlcon);
DataSet ds = new DataSet();
ada.Fill(ds, "WebForm_Study_Test ");
GridView1.DataSource = ds;
GridView1.DataBind();
sqlcon.Close();
Response.Write("<script>alert('添加成功');</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('ex.Message.ToString()');</script>");
}
}
}
}
public int getConut()
{//判断是否插入了相同的数据
SqlConnection con = new SqlConnection(connstr);
con.Open();
string str = "select * from WebForm_Study_Test where name='" + TextBox2.Text + "'and price='" + (DropDownList1.SelectedValue+ TextBox4.Text) + "'and place='" + type(DropDownList1.SelectedValue) + "'";
SqlCommand com = new SqlCommand(str, con);
int intcont = Convert.ToInt32(com.ExecuteScalar());
con.Close();
com.Clone();
return intcont;
}
public string type(string type)//根据价格类型,选择相应的地区
{
if (DropDownList1.SelectedValue == "$")
{
return TextBox5.Text = "the USA";
}
else
{
return TextBox5.Text = "China";
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
}
}
其中在编辑中有跳转的页面,跳转到Edit.aspx页面,代码如下:
Edit.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="WebForm_b.Edit" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="商品名:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Height="19px" Width="172px"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="商品价格:"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" Height="24px" Width="40px">
<asp:ListItem>¥</asp:ListItem>
<asp:ListItem>$</asp:ListItem>
<%-- <asp:ListItem>¥111</asp:ListItem>
<asp:ListItem>¥123</asp:ListItem>--%>
</asp:DropDownList>
<asp:TextBox ID="TextBox4" runat="server" Height="21px" Width="124px"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="TextBox3" runat="server" Height="20px" OnTextChanged="TextBox3_TextChanged" Width="166px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Height="24px" OnClick="Button1_Click" Text="修改" Width="65px" />
</div>
</form>
</body>
</html>
Edit.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebForm_b
{
public partial class Edit : System.Web.UI.Page
{
WebForm1 WB = new WebForm1();
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Visible = false;
if (!IsPostBack)
{
if (!IsPostBack)
{
//DropDownList1.DataSource = WB.getTypeList("select distinct price from WebForm_Study_Test", "price");
// distinct去重
//DropDownList1.DataBind();
try
{
TextBox1.Text = WB.Session["name"].ToString();
}
catch { }
try
{
DropDownList1.SelectedValue = WB.Session["price"].ToString();
}
catch { }
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String name = TextBox1.Text;
//string type = TextBox4.Text;
string type = DropDownList1.SelectedValue.ToString();
string aaa= number(TextBox4.Text);
String price = (type+ aaa);
String place = TextBox3_TextChanged(type);
String action = DropDownList1.SelectedValue;
String sql = "update WebForm_Study_Test set name='" + name + "',price='" + price + "',place='" + place + "' where name='" + name + "'";
if (WB.Delete_Update(sql) == true)
{
Response.Write("<script>alert('修改成功');location.href='FindUsers.aspx'</script>");
Response.Redirect("WebForm1.aspx");
}
else
{
Response.Write("<script>alert('修改失败');</script>");
Response.Redirect("WebForm1.aspx");
}
}
public string number(string num)
{
int age = Convert.ToInt32(num);
if (age > 0 && age < 9999)
{
return TextBox4.Text = num;
}
else
{
return TextBox4.Text = "超过价格1~9999范围,请重输!";
}
}
private string TextBox3_TextChanged(string type)
{
if (type == "$")
{
return TextBox3.Text = "the USA";
}
//else if(DropDownList1.SelectedValue=="¥111"|| DropDownList1.SelectedValue == "¥100")
else
{
return TextBox3.Text = "China";
}
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
}
}
}