那么我们就先来学习Home.aspx的写法
2 <div id ="header_panel" >
3 <div id="header_section">
4 <div id="title_section"> B B S </div>
5 <div id="tagline">Welcome to you!</div>
6 </div>
7 </div>
8 <div id ="menu_panel" >
9 <div id="menu_section">
10
11 <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">实事</asp:LinkButton>
12 <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">科学</asp:LinkButton>
13 <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">文学</asp:LinkButton>
14 <asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkButton4_Click">体育</asp:LinkButton>
15 </div>
16 </div>
17 </div>
下面我们进入Homeaspx.cs中看看那三个LinkButton的作用
2 /// 点击不同的帖主题显示不同的内容
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 protected void LinkButton1_Click(object sender, EventArgs e)
7 {
8 flag = 1;
9 showGv(flag, k);
10 }
11 protected void LinkButton2_Click(object sender, EventArgs e)
12 {
13 flag = 2;
14 //this.Page_Load(sender, e);
15 showGv(flag, k);
16 }
17 protected void LinkButton3_Click(object sender, EventArgs e)
18 {
19 flag = 3;
20 //this.Page_Load(sender, e);
21 showGv(flag, k);
22 }
23 protected void LinkButton4_Click(object sender, EventArgs e)
24 {
25 flag = 4;
26 // this.Page_Load(sender, e);
27 showGv(flag, k);
28 }
29
下面我们看看ShowGv的作用
2 /// 在gridview中显示数据
3 /// </summary>
4 /// <param name="flag">主题序号</param>
5 /// <param name="k">页号</param>
6 void showGv(int flag, int k)
7 {
8 if (flag == 0) //所有的部分
9 {
10 string sql = "select gid,tname,tcontent,tdata,tclick,tid,treply from tip order by tclick desc";//order by tclick desc是指按帖子的点击数从高往低排列
11 Sql(sql, k, Lab_NonTip);
12 }
13 if (flag == 1) //主题等于1的部分
14 {
15 string sql = "select gid,tname,tcontent,tdata,tclick,tid,treply from tip where ttheme=1 order by tclick desc ";
16 Sql(sql, k, Lab_NonTip);
17 }
18 if (flag == 2) //主题等于2的部分
19 {
20 string sql = "select gid,tname,tcontent,tdata,tclick,tid,treply from tip where ttheme=2 order by tclick desc";
21 Sql(sql, k, Lab_NonTip);
22 }
23 if (flag == 3) //主题等于3的部分
24 {
25 string sql = "select gid,tname,tcontent,tdata,tclick ,tid,treply from tip where ttheme=3 order by tclick desc";
26 Sql(sql, k, Lab_NonTip);
27 }
28 if (flag == 4) //主题等于4的部分
29 {
30 string sql = "select gid,tname,tcontent,tdata,tclick,tid,treply from tip where ttheme=4 order by tclick desc";
31 Sql(sql, k, Lab_NonTip);
32 }
33 }
然后我们重点看一下负责数据库操作的Sql函数,以及参数 Lab_NonTip的值
/// <summary>
/// 读取数据库数据,并绑定到gridview
/// </summary>
/// <param name="sql">数据库操作语句</param>
/// <param name="j">页码号</param>
protected void Sql(string sql, int j, Label Label1)
{
gvShow.PageIndex = j;
string connString = @"Data Source=.;Initial Catalog=msgBord;Integrated Security=True ";
SqlConnection connection = new SqlConnection(connString);
connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connection);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "test");
if (dataSet.Tables["test"].Rows.Count < 1)
{
Label1.Visible = true;
}
else
{
Label1.Visible = false;
}
gvShow.DataSource = dataSet;
gvShow.DataBind();
connection.Close();
cut();//为了美观将数据截断显示
}
再来看下CUT 函数
/// <summary>
/// 用于将gridview的字符截取掉长度过大项
/// </summary>
protected void cut()
{
for (int i = 0; i < gvShow.Rows.Count; i++)
{
for (int j = 0; j < gvShow.Rows[i].Cells.Count - 1; j++)
{
string s = gvShow.Rows[i].Cells[j].Text;
string t = s;
if (s.Length >= 10)
{
t = s.Substring(0, 8) + ".....";
}
gvShow.Rows[i].Cells[j].Text = t;
}
}
}
这样Home页面头部的动作已经网页显示我们就已经做完了
其他部分代码依旧照此分析,大家原谅我吧,这么一点点写实在是太麻烦了。我现在就把我改过的代码上传,值得注意的是代码中有一个数据库msgBord,大家不要考到MSSQL 的Data目录后在附加,就直接在当前目录中附加就OK了。如果想改数据库配置路径的话,请看程序中的配置文件。