DAN疼之后上些基础知识(一)
以下为一些适合新手的基础知识,你要是说无聊,请看上一篇文章..................
接受传直 和 弹出窗口
Response.Write("<script language=javascript>alert('调用response的方法')</script>");
if (!IsPostBack)
{
string id = Request.QueryString["id"].ToString();
上传图片:
string fullname = this.FileUpload1.FileName;
string filename = fullname.Substring(fullname.LastIndexOf("//") + 1);
string type = fullname.Substring(fullname.LastIndexOf(".") + 1);
if (type == "jpg" || type == "bmp" || type == "gif")
{
this.FileUpload1.PostedFile.SaveAs(Server.MapPath("up") + "//" + filename);
this.img.ImageUrl ="up"+"/"+filename ;//图片显示
}
else
Response.Write("<script language=javascript>alert('错了')</script>");
检查用户名是否存在:
第一种方法:
SqlConnection con=new SqlConnection("Server=.;database=text;uid=sa;pwd=;");
con.Open ();
SqlCommand com=new SqlCommand (" Select count(*) from [use] where uername='"+username.Text +"'and pwd='"+userpwd.Text +"'",con);
int count = Convert.ToInt32(com.ExecuteScalar());
if (count > 0)
{
Label1.Text = "用户名已经存在";
}
else
{
Label1.Text = " 用户名不存在";
}
第2种方法:
类中的方法:
public int gvdababind(string strcon)
{
int a;
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connectionstring"]);
con.Open();
SqlCommand com = new SqlCommand(strcon, con);
a = Convert .ToInt32(com.ExecuteScalar ());
con.Close();
return a;
}
在后台中的代码:
int a =dbnew.gvdababind( " Select count(*) from [use] where uername='" + username.Text + "'and pwd='" + userpwd.Text + "'");
if (a > 0)
{
Label1.Text = "用户名已经存在";
}
else
{
Label1.Text = " 用户名不存在";
}
省市连动:
类中的方法:
public void ddlbind(DropDownList ddl,string str,string name,string id)
{
SqlConnection con=new SqlConnection (ConfigurationManager .AppSettings ["Connectionstring"]);
con.Open ();
SqlDataAdapter da=new SqlDataAdapter (str,con);
DataSet ds=new DataSet ();
da.Fill (ds);
ddl.DataSource =ds;
ddl.DataTextField = name;
ddl.DataValueField = id;
ddl.DataBind ();
con.Close();
// return id;
}
前台调用方法:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
db.ddlbind(ddlpro, "select * from provice", "proname", "proid");
}
protected void ddlpro_SelectedIndexChanged(object sender, EventArgs e)
{
db.ddlbind(ddlcity, "select * from city where proid=" + ddlpro.SelectedValue + "", "cityname", "cityid");
}
{
读取xml文件信息:
FileStream fs = new FileStream(Server.MapPath("XMLFile.xml"), FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
DataSet ds1 = new DataSet();
ds1.ReadXml(sr);
gvxml.DataSource = ds1.Tables[0];
gvxml.DataBind();
利用返回dataset的方法 读取dataset 中的表的内容
方法: public DataSet returnds()
{
SqlConnection con = new SqlConnection(datacon());
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from [use] ", con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
con.Close();
}
调用:
DataSet ds = new DataSet();
ds = db.returnds();
DataRowView drv = ds.Tables[0].DefaultView[0];
lbname.Text = Convert.ToString(drv.Row["uername"]);
lbpwd.Text = Convert.ToString(drv.Row["pwd"]);
DataRowView drv1 = ds.Tables[0].DefaultView[1];
lbname2.Text = Convert.ToString(drv1.Row["uername"]);
lbpwd2.Text = Convert.ToString(drv1.Row["pwd"]);
简单的数字验证码:
Random rd = new Random();
lbnumber.Text = rd.Next(1000, 10000).ToString();
数字字母组合验证码
public void generatecheckcode()
{
int number;
char code;
string checkcode = string.Empty;
Random rd = new Random();
for (int i = 0; i < 4; i++)
{
number = rd.Next();
if (number %2==0)
code =(char )('0' +(char )(number %10));
else
code =(char )('A'+(char )(number %26));
checkcode +=code .ToString ();
}
Label1 .Text =checkcode ;
}
repeater 的模板编程:
在html中 <ItemTemplate >
<%# DataBinder .Eval (Container.DataItem, "uername") %>
</ItemTemplate>
<AlternatingItemTemplate >
<%# DataBinder.Eval(Container.DataItem, "uername")%>
</AlternatingItemTemplate>
<FooterTemplate >页脚</FooterTemplate>
<HeaderTemplate >页眉</HeaderTemplate>
<SeparatorTemplate ></SeparatorTemplate>
设为首页 和 收藏本站代码(注意不能用超级连接等 只能用普通汉字在html中设置)
<a href ="#" onclick ="this.style.behavior='url(#default#homepage)';this.sethomepage('http://www.sina.com')"><font color="#00cc99">设为首页</font></a>
<a href ="#" onclick ="window.external.addFavorite('http://www.sina.com','大学生论坛');"><font color="#00cc99">收藏本站</font> </a>
找控件::
protected void dldgdd_ItemCommand(object source, DataListCommandEventArgs e)
{
string content = ((LinkButton)e.Item.FindControl("linkbutton1")).Text;
Session["content"] = content;
Response.Redirect("main.aspx?id=" + content + " ");
}
datalist 分页 :
public void dlbind()
{
string id = Request.QueryString["id"].ToString();
int curpage = Convert.ToInt32(this.Label12.Text);
PagedDataSource ps = new PagedDataSource();
DataSet ds = null;
ds = db.returndataset("select * from tb_return where r_fatherid='" + id + "' order by r_time desc", "tb_motif");
ps.DataSource = ds.Tables["tb_motif"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 10;
ps.CurrentPageIndex = curpage - 1;
this.lkbtnnoback.Enabled = true;
this.lkbtnback.Enabled = true;
this.lkbtnn.Enabled = true;
this.lkbtn1.Enabled = true;
if (curpage == 1)
{
this.lkbtn1.Enabled = false;
this.lkbtnnoback.Enabled = false;
}
if (curpage == ps.PageCount)
{
this.lkbtnback.Enabled = false;
this.lkbtnn.Enabled = false;
}
Label15.Text = Convert.ToString(ps.PageCount);
this.dlreturn.DataSource = ps;
this.dlreturn.DataKeyField = "r_id";
this.dlreturn.DataBind();
}
protected void lkbtn1_Click(object sender, EventArgs e)
{
this.Label12.Text = "1";
this.dlbind();
}
protected void lkbtnnoback_Click(object sender, EventArgs e)
{
this.Label12.Text = Convert.ToString(Convert.ToInt32(this.Label12.Text) - 1);
this.dlbind();
}
protected void lkbtnback_Click(object sender, EventArgs e)
{
this.Label12.Text = Convert.ToString(Convert.ToInt32(this.Label12.Text) + 1);
this.dlbind();
}
protected void lkbtnn_Click(object sender, EventArgs e)
{
this.Label12.Text = this.Label15.Text;
this.dlbind();
}