您的位置:首页 > 运维架构 > 网站架构

自己建网站用到的一些小东西

2015-03-06 09:24 218 查看
新手,求轻喷!

1.在.aspx文件中显示自己定义的变量或者session:

<h4><b>Hello,<%=Session["username"]%></b></h4>

2.显示数据库中的新闻,方便的显示:

<asp:DataList ID="DataList11" runat="server" CellPadding="10" DataSourceID="LinqDataSource11" ForeColor="#333333" ShowFooter="False" ShowHeader="False" Width="578px" Height="387px">

<AlternatingItemStyle BackColor="White" />

<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

<ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />

<ItemTemplate>

news1:

<asp:Label ID="news1Label" runat="server" Text='<%# Eval("news1") %>' />

<br />

<br />

</ItemTemplate>

<SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

</asp:DataList>

<asp:LinqDataSource ID="LinqDataSource11" runat="server" ContextTypeName="jpkc.loginDataContext" EntityTypeName="" Select="new (news1)" TableName="news">

</asp:LinqDataSource>

CellPadding是显示每个页面显示多少条新闻

可以在datalist任务-》自动套用格式,选择自己喜欢的样式

3.建c#网站,最好使用masterpage技术,要是想大规模的改网页的相同的地方就很方便了。与不用masterpage技术的网页相比,这是没有head、body等标签的。

4.连接数据库,最好使用linq。需要添加命名空间using System.Linq;然后在项目上添加新项,添加一个lINQ to SQL类,然后把数据库中想使用的表拖到这个dbml文件中,这样就可使用了。

在.aspx.cs文件中pageload函数前面添加private loginDataContext ud = new loginDataContext();然后在函数中写入:

var loginin_table = ud.GetTable<loginin>();//loginin是想使用的表的名称

loginin loginin = new loginin();

loginin.username = username.Text;

loginin.number++;

loginin_table.InsertOnSubmit(loginin);//插入数据库

ud.SubmitChanges();//这句很重要,一定要写,不然所做的改变对数据库没有影响。

Login t = new Login();

var n = a.loginin.Single(u => u.username == username.Text);//username和password都是数据库中的属性

if (n.password.Trim().Equals(password.Text)){

if (n.username.Trim().Equals("cjy") || n.username.Trim().Equals("dhb"))

{

Session.Remove("username");

Session["username"] = n.username.ToString();

Response.Redirect("administerlogin.aspx");

}

else

{

Session["username"] = n.username.ToString();

Response.Redirect("userlogin.aspx");

}

}

else

{

Response.Write("No name in database or wrong password!");

Response.Redirect("default.aspx");

}

5.上传文件,可以使用控件<dx:ASPxUploadControl ID="FileUpload1" runat="server" ClientIDMode="AutoID"></dx:ASPxUploadControl>,需要命名空间using DevExpress.Web.ASPxUploadControl;

在.aspx.cs函数中:

UploadedFile UploadedFiles = new UploadedFile();//连接数据库

var uploadfile_table = ud.GetTable<uploadfile>();

uploadfile uploadfile = new uploadfile();

string name = FileUpload1.FileName;//获取已上传文件的名字

string size = FileUpload1.PostedFile.ContentLength.ToString();//获取已上传文件的大小

string type = FileUpload1.PostedFile.ContentType;//获取已上传文件的MIME内容类型,但是不直观

string ipath = Server.MapPath("uploadfile") + "\\" + name;//获取文件上传的实际路径,这里的uploadfile是新建的文件夹名称,把上传的文件都存到uploadfile文件夹中。

FileUpload1.SaveAs(ipath);//将文件保存到path这个路径

uploadfile.filename = name;

uploadfile.path = ipath;

uploadfile.type = type;

uploadfile_table.InsertOnSubmit(uploadfile);

ud.SubmitChanges();

6.asp;gridview需要sqldatasource控件,sqldatasource控件连接数据库,这样girdview就能显示数据库中的内容了。但在vs2013中我还不知道怎么自己写删除选中行。

gridview也可以添加新列,选择字段类型,想干嘛干嘛

我添加了一个按钮,实现下载文件的功能:

int a = GridView1.SelectedIndex;//取得想要删除行的索引值

string filename = GridView1.Rows[a].Cells[1].Text;//取该行的第一列

string origion = Server.MapPath("");

origion = origion + "\\uploadfile\\";

Response.Write("<script>alert('xiazai2!')</script>");

if (!string.IsNullOrEmpty(filename))//下面这段我只知道能够下载文件,但实现原理我还不懂

{

string filePath = origion + filename;

FileInfo fileInfo = new FileInfo(filePath);

Response.Clear();

Response.ClearContent();

Response.ClearHeaders();

Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);

Response.AddHeader("Content-Length", fileInfo.Length.ToString());

Response.AddHeader("Content-Transfer-Encoding", "binary");

Response.ContentType = "application/octet-stream";

Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

Response.WriteFile(fileInfo.FullName);

Response.Flush();

Response.End();

}

7.目录索引,类似于百度百科中的右侧的那个小目录,其实实现起来很简单:

同一页面下<h3>目录</h3>

<h4><a href="#课程内容">1.课程内容。</a></h4>

<h4><a href="#重难点">2.重难点。</a></h4>

<h4><a href="#章节介绍">3.章节介绍。</a></h4>

这样点击1.课程内容时浏览器就会自动显示同页面下name=课程内容的地方

8.在项目中建立js文件,然后在其中定义函数。比如:

function showtime()

{

var greeting;

var nowDate = new Date();

var nowHour = nowDate.getHours();

var nowMinute = nowDate.getMinutes();

var nowSecond = nowDate.getSeconds();

if (nowMinute < 10) {

nowMinute += "0";

}

if (nowSecond < 10) {

nowSecond += "0";

}

if (nowHour < 12) {

greeting = "Good Morning!";

}

else if (nowHour < 17) {

greeting = "Good Afternoon!";

}

else {

greeting = "Good Evening!"

}

document.write("<h4>" + greeting + "</h4>");

document.write("<h4>" + "Now Time: " + nowHour + ":" + nowMinute + ":" +nowSecond + "</h4>");

}

然后在.aspx文件中任意位置加入<script type="text/javascript">showtime()</script>(再次提示,我没有body和head标签),在页面中就可以显示登录时间了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: