📄 default.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// bind();
}
if (ViewState["count"] != null)
{
for (int i = 0; i < Convert.ToInt16(ViewState["count"]); i++)
{
addTextBox();
}
}
else
{
Button2.Enabled = false;
}
SqlConnection con = new SqlConnection("Server=(Local);user id=sa;pwd=;DataBase=mrdb");
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_10 order by id desc", con);
DataSet ds = new DataSet();
ada.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
con.Close();
//SqlDataAdapter myada = new SqlDataAdapter("select * from tb_10", con);
//DataSet myds = new DataSet();
//ada.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
if (this.RadioButtonList2.SelectedIndex == 1)
{
this.Panel7.Visible = false;
this.Panel8.Visible = true;
this.Panel9.Visible = false;
this.Panel10.Visible = true;
}
else
{
this.Panel7.Visible = true;
this.Panel8.Visible = false;
this.Panel9.Visible = true;
this.Panel10.Visible = false;
}
}
//动态添加Label控件和TextBox控件的方法
public void addTextBox()
{
TableRow tr = new TableRow();
TableCell td1 = new TableCell();
TableCell td2 = new TableCell();
TableCell td3 = new TableCell();
TableCell td4 = new TableCell();
TableCell td5 = new TableCell();
TableCell td6 = new TableCell();
TableCell td7 = new TableCell();
TableCell td8 = new TableCell();
//图书名称:
TextBox txt1 = new TextBox();
txt1.ID = "txt1" + Table1.Rows.Count;
txt1.Width = 80;
Label lab1 = new Label();
lab1.Width = 64;
lab1.Font.Size = FontUnit.Point(9);
lab1.Text = "名星人物:";
lab1.ID = "lab1" + Table1.Rows.Count;
//图书价格:
Label lab2 = new Label();
lab2.Width = 64;
lab2.Font.Size = FontUnit.Point(9);
lab2.Text = "打榜次数:";
lab2.ID = "lab2" + Table1.Rows.Count;
TextBox txt2 = new TextBox();
txt2.ID = "txt2" + Table1.Rows.Count;
txt2.Width = 80;
//图书数量:
Label lab3 = new Label();
lab3.Width = 64;
lab3.Font.Size = FontUnit.Point(9);
lab3.Text = "打榜歌曲:";
lab3.ID = "lab3" + Table1.Rows.Count;
TextBox txt3 = new TextBox();
txt3.ID = "txt3" + Table1.Rows.Count;
txt3.Width = 80;
//
Label lab4 = new Label();
lab4.Width = 64;
lab4.Font.Size = FontUnit.Point(9);
lab4.Text = "人气指数";
lab4.ID = "lab4" + Table1.Rows.Count;
TextBox txt4 = new TextBox();
txt4.ID = "txt4" + Table1.Rows.Count;
txt4.Width = 80;
// lab1.ID = "lab1" + Table1.Rows.Count;
td1.Controls.Add(lab1);
td2.Controls.Add(txt1);
td3.Controls.Add(lab2);
td4.Controls.Add(txt2);
td5.Controls.Add(lab3);
td6.Controls.Add(txt3);
td7.Controls.Add(lab4);
td8.Controls.Add(txt4);
tr.Cells.Add(td1);
tr.Cells.Add(td2);
tr.Cells.Add(td3);
tr.Cells.Add(td4);
tr.Cells.Add(td5);
tr.Cells.Add(td6);
tr.Cells.Add(td7);
tr.Cells.Add(td8);
Table1.Rows.Add(tr);
}
//动态添加控件按钮
protected void Button1_Click(object sender, EventArgs e)
{
addTextBox();
ViewState["count"] = Convert.ToInt16(ViewState["count"]) + 1;
Button2.Enabled = true;
}
//循环添加数据
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=mrdb;uid=sa;pwd=;");
con.Open();
string sql = null;
try
{
for (int i = 0; i < Table1.Rows.Count; i++)
{
string name = ((TextBox)Table1.Rows[i].FindControl("txt1" + i)).Text;
string cishu = ((TextBox)Table1.Rows[i].FindControl("txt2" + i)).Text;
string SongName = ((TextBox)Table1.Rows[i].FindControl("txt3" + i)).Text;
string amout = ((TextBox)Table1.Rows[i].FindControl("txt4" + i)).Text;
sql = "insert into tb_10(name,age,songname,zhishu) values('" + name + "','" + cishu + "','" + SongName + "','" + Convert.ToInt32(amout) + "')";
SqlCommand com = new SqlCommand(sql, con);
com.ExecuteNonQuery();
Response.Write("<script>alert('数据添加成功!');location='Default.aspx'</script>");
Response.Write(sql);
}
}
catch (Exception error)
{
Response.Write(error.Message.ToString());
Response.Write("<script>alert('添加的数据不能为空且要注意数据的字符串格式!')</script>");
}
finally
{
con.Close();
}
}
protected void btnBackup_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Server=(Local);user id=sa;pwd=;DataBase=mrdb");
con.Open();
string str = "select count(*) from tb_10 where zhishu='" + TextBox2.Text.ToString() + "'";
SqlCommand mycom = new SqlCommand(str, con);
int intcont = Convert.ToInt32(mycom.ExecuteScalar());
if (intcont > 0)
{
Response.Write("<script language=javascript>alert('对不起!更新的人气指数要唯一!!')</script>");
}
else
{
try
{
string SqlStr = "update tb_10 set zhishu='" + TextBox2.Text + "' where age='" + TextBox1.Text + "'";
SqlCommand com = new SqlCommand(SqlStr, con);
com.ExecuteNonQuery();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_10", con);
DataSet ds = new DataSet();
ada.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
Response.Write("<script>alert('批量更新成功!')</script>");
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
Response.Write("<script>alert('对不起!指量更新失败!')</script>");
}
}
}
protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView2.PageIndex = e.NewPageIndex;
GridView2.DataBind();
}
//自定义一个GetDataSet方法,返回DataSet数据集
public DataSet GetDataSet(string sqlstr)
{
SqlConnection con = new SqlConnection("Server=(Local);user id=sa;pwd=;DataBase=mrdb");
con.Open();
SqlCommand com = new SqlCommand(sqlstr, con);
com.ExecuteNonQuery();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_10", con);
DataSet ds = new DataSet();
ada.Fill(ds);
con.Close();
return ds;
}
protected void btnRestore_Click(object sender, EventArgs e)
{
if (DropDownList1.Text == "打榜次数")
{
try
{
string sqlstr1 = "delete from tb_10 where age='" + TextBox3.Text + "'";
DataSet ds = GetDataSet(sqlstr1);
GridView1.DataSource = ds;
GridView1.DataBind();
Response.Write("<script language=javascript>alert('删除成功!')</script>");
}
catch
{
Response.Write("<script language=javascript>alert('批量删除失败!')</script>");
}
}
if (DropDownList1.Text == "人气指数")
{
try
{
string sqlstr2 = "delete from tb_10 where zhishu='" + TextBox3.Text + "'";
GetDataSet(sqlstr2);
DataSet ds = GetDataSet(sqlstr2);
GridView1.DataSource = ds;
GridView1.DataBind();
Response.Write("<script language=javascript>alert('删除成功!')</script>");
}
catch
{
Response.Write("<script language=javascript>alert('批量删除失败!')</script>");
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -