📄 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;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=;database=mrdb");
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_16", con);
DataSet ds = new DataSet();
ada.Fill(ds, "tb_16");
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();
Image1.ImageUrl = "Img/" + DropDownList1.SelectedItem.Text;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName != "")
{
try
{
string ImgPath = FileUpload1.PostedFile.FileName;
string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1);
string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1).ToLower();
if (!(ImgExtend == "bmp" || ImgExtend == "jpg" || ImgExtend == "gif"))
{
return;
}
string ServerPath = Server.MapPath("Img/") + ImgName;
FileUpload1.PostedFile.SaveAs(ServerPath);
SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=;database=mrdb");
con.Open();
string str = "select count(*) from tb_16 where name='" + ImgName + "'";
SqlCommand mycom = new SqlCommand(str, con);
//mycom.ExecuteNonQuery();
//con.Close();
int intcont = Convert.ToInt32(mycom.ExecuteScalar());
if (intcont > 0)
{
Response.Write("<script language=javascript>alert('对不起!不允许上传相同记录!')</script>");
return;
}
else
{
SqlCommand com = new SqlCommand("insert into tb_16(name)values('" + ImgName + "')", con);
com.ExecuteNonQuery();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_16", con);
DataSet ds = new DataSet();
ada.Fill(ds, "tb_16");
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();
Image1.ImageUrl = "Img/" + DropDownList1.SelectedItem.Text;
Response.Write("<script language=javascript>alert('上传成功!')</script>");
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Image1.ImageUrl = "Img/" + DropDownList1.SelectedItem.Text;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -