62,041
社区成员
发帖
与我相关
我的任务
分享
<asp:Panel ID="Panel2" runat="server">
<center>
<b>添加非法信息</b>
<div style="width:600px;">
非法信息名称:<asp:TextBox ID="txterrname" runat="server" Width="350"></asp:TextBox> <br />
<div>
<div >
请输入非法信息记录行数:
<asp:TextBox runat="server" ID="txtTextCount"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtTextCount" ValidationGroup="CreateTextBox" Display="Dynamic"
runat="server" ErrorMessage="只接受数字!" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>
<asp:Button runat="server" ID="btnCreate" Text="创建记录非法信息记录行数" ValidationGroup="CreateTextBox"
onclick="btnCreate_Click" />
</div>
<div runat="server" id="divControls" > </div>
<div runat="server" id="divMessage"> </div>
<asp:Panel ID="Panel4" runat="server">
</asp:Panel>
</div>
</div>
<asp:Button ID="btninsert" runat="server" Text="确认添加"
onclick="btninsert_Click" />
</center>
</asp:Panel>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class Error_ManagerError : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
if (this.txtaddtype.Text.Length < 1)
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('对不起,非法信息类型不能为空!');</script>");
return;
}
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("insert into errorinfo(name) values(@name)", cn))
{
cmd.Parameters.AddWithValue("@name", this.txtaddtype.Text);
int rows = -1;
try
{
cn.Open();
rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('恭喜你,非法信息类型添加成功!');</script>");
this.txtaddtype.Text = string.Empty;
return;
}
}
catch (Exception)
{
throw;
}
}
}
}
private DataSet GetErrorInfo()
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString))
{
using (SqlDataAdapter cmd = new SqlDataAdapter("select * from errorinfo", cn))
{
DataSet ds = new DataSet();
try
{
cn.Open();
cmd.Fill(ds);
}
catch (Exception ex)
{
cn.Close();
throw new Exception(ex.Message);
}
return ds;
}
}
}
private void CreateTextBoxLis(int num)
{
Panel p;
TextBox t;
Label l;
DropDownList dr;
for (int i =0; i < num; i++)
{
//创建Panel
p = new Panel();
p.ID = "ptxt" + i.ToString();
//创建Lable
l = new Label();
l.ID = "lable" + i.ToString();
l.Text = "详细信息" + i.ToString();
//创建TextBox
t = new TextBox();
t.ID = "t" + i.ToString();
Page.Form.Controls.Add(t);
t.Attributes.Add("runat", "server");
//创建DropDownList控件
dr = new DropDownList();
dr.ID = "dr" + i.ToString();
dr.Attributes.Add("runat", "server");
dr.DataSource = GetErrorInfo();
dr.DataTextField = "name";
dr.DataValueField = "id";
dr.DataBind();
Panel4.Controls.Add(p);
Panel4.Controls.Add(l);
Panel4.Controls.Add(t);
Panel4.Controls.Add(dr);
}
}
protected void btnCreate_Click(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (txtTextCount.Text == null || string.IsNullOrEmpty(txtTextCount.Text))
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('很抱歉,你输错了!');</script>");
return;
}
int txtCount = int.Parse(txtTextCount.Text);
// 注意:每次PostBack时,都需要重新动态创建TextBox
CreateTextBoxLis(txtCount);
}
}
protected void btninsert_Click(object sender, EventArgs e)
{
AddError();
}
private void AddError()
{
TextBox txt;
DropDownList dr;
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("insert into error(nickname,website,eid) values(@name,@website,@eid)", cn))
{
cmd.Parameters.AddWithValue("@name", this.txterrname.Text);
int f =int.Parse(this.txtTextCount.Text);
for (int i = 0; i < f; i++)
{
txt=this.Panel4.FindControl("t"+i.ToString())as TextBox;
dr=this.Panel4.FindControl("t"+i.ToString())as DropDownList;
//Response.Write(((TextBox)this.FindControl("t" + i.ToString())).Text);
cmd.Parameters.AddWithValue("@website", txt.Text);
cmd.Parameters.AddWithValue("eid",dr.SelectedValue );
}
int rows = -1;
try
{
cn.Open();
rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
Response.Write("<script>alert('添加成功')</script>");
}
else
Response.Write("<scirpt>alert('添加失败')</script>");
}
catch (Exception)
{
cn.Close();
throw;
}
}
}
}
protected void btnseach_Click(object sender, EventArgs e)
{
Panel1.Visible = false;
Panel2.Visible = false;
Panel3.Visible = true;
txtseach.Text = string.Empty;
}
private void GetShowError()
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select error.id,nickname,website,name,state from errorinfo inner join error on errorinfo.id=error.eid", cn))
{
try
{
cn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
catch (Exception)
{
cn.Open();
throw;
}
}
}
}
private void GetShowErrorByName(string name)
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select error.id,website,errorinfo.name as name,state,nickname from error inner join errorinfo on error.eid=errorinfo.id where nickname LIKE '%' + @no + '%' or website LIKE '%' + @url + '%'", cn))
{
cmd.Parameters.AddWithValue("@no", name);
cmd.Parameters.AddWithValue("@url", name);
try
{
cn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
catch (Exception ex)
{
cn.Close();
throw new Exception(ex.Message);
}
}
}
}
protected void brnseach_Click(object sender, EventArgs e)
{
if (this.txtseach.Text != null)
{
GetShowErrorByName(this.txtseach.Text);
}
else
{
GetShowError();
}
}
}