第一次接触Asp.net,是因为暑假接的一个项目做起的.
现在这个项目已经发布了.
从这个项目中,我学到的具体编程知识(关于GridView)有具体说来,有以下几点:
1.
在后台代码程序里绑定数据源:
<
asp:GridView
ID
="GridView1"
runat
="server"
AllowPaging
="True"
AutoGenerateColumns
="False"
CellPadding ="4" DataKeyNames ="InformID" ForeColor ="#31639C" GridLines ="None"
HorizontalAlign ="Left" OnPageIndexChanging ="GridView1_PageIndexChanging"
PageSize ="7" Style ="left: 37px; position: relative; top: -25px" >
< FooterStyle BackColor ="#31639C" Font-Bold ="True" ForeColor ="White" BorderColor ="#31639C" />
< Columns >
< asp:TemplateField HeaderText ="消息" >
< ItemTemplate >
< asp:Image ID ="Image1" runat ="server" ImageUrl ="~/Inform/9.gif" Style ="position: relative" />
</ ItemTemplate >
< ItemStyle Width ="80px" />
< HeaderStyle HorizontalAlign ="Left" />
</ asp:TemplateField >
< asp:BoundField DataField ="IsNew" HeaderText ="状态" >
< ItemStyle Font-Size ="Small" ForeColor ="Navy" Width ="100px" />
< HeaderStyle HorizontalAlign ="Left" />
</ asp:BoundField >
< asp:HyperLinkField DataNavigateUrlFields ="InformID" DataNavigateUrlFormatString ="InformDetail.aspx?ID={0}"
DataTextField ="InformTitle" HeaderText ="消息主题" Target ="_blank" >
< ItemStyle Font-Size ="Small" Width ="580px" />
< HeaderStyle HorizontalAlign ="Left" />
</ asp:HyperLinkField >
< asp:BoundField DataField ="InformDate" HeaderText ="发布日期" >
< ItemStyle ForeColor ="Gray" />
</ asp:BoundField >
</ Columns >
< EditRowStyle BackColor ="#31639C" />
< SelectedRowStyle BackColor ="#C5BBAF" Font-Bold ="True" ForeColor ="#333333" />
< PagerStyle BackColor ="#31639C" ForeColor ="White" HorizontalAlign ="Center" />
< HeaderStyle BackColor ="#31639C" Font-Bold ="True" ForeColor ="White" Wrap ="False" />
< AlternatingRowStyle BackColor ="White" />
< PagerSettings PageButtonCount ="30" />
</ asp:GridView >
CellPadding ="4" DataKeyNames ="InformID" ForeColor ="#31639C" GridLines ="None"
HorizontalAlign ="Left" OnPageIndexChanging ="GridView1_PageIndexChanging"
PageSize ="7" Style ="left: 37px; position: relative; top: -25px" >
< FooterStyle BackColor ="#31639C" Font-Bold ="True" ForeColor ="White" BorderColor ="#31639C" />
< Columns >
< asp:TemplateField HeaderText ="消息" >
< ItemTemplate >
< asp:Image ID ="Image1" runat ="server" ImageUrl ="~/Inform/9.gif" Style ="position: relative" />
</ ItemTemplate >
< ItemStyle Width ="80px" />
< HeaderStyle HorizontalAlign ="Left" />
</ asp:TemplateField >
< asp:BoundField DataField ="IsNew" HeaderText ="状态" >
< ItemStyle Font-Size ="Small" ForeColor ="Navy" Width ="100px" />
< HeaderStyle HorizontalAlign ="Left" />
</ asp:BoundField >
< asp:HyperLinkField DataNavigateUrlFields ="InformID" DataNavigateUrlFormatString ="InformDetail.aspx?ID={0}"
DataTextField ="InformTitle" HeaderText ="消息主题" Target ="_blank" >
< ItemStyle Font-Size ="Small" Width ="580px" />
< HeaderStyle HorizontalAlign ="Left" />
</ asp:HyperLinkField >
< asp:BoundField DataField ="InformDate" HeaderText ="发布日期" >
< ItemStyle ForeColor ="Gray" />
</ asp:BoundField >
</ Columns >
< EditRowStyle BackColor ="#31639C" />
< SelectedRowStyle BackColor ="#C5BBAF" Font-Bold ="True" ForeColor ="#333333" />
< PagerStyle BackColor ="#31639C" ForeColor ="White" HorizontalAlign ="Center" />
< HeaderStyle BackColor ="#31639C" Font-Bold ="True" ForeColor ="White" Wrap ="False" />
< AlternatingRowStyle BackColor ="White" />
< PagerSettings PageButtonCount ="30" />
</ asp:GridView >
后台代码中像正常一样绑定数据一样:
private void createBind()
... {
SqlConnection conn;
SqlCommand cmd;
SqlDataAdapter da;
// string strConn = "server=2E97168B31034F3;database=hr; Trusted_Connection=yes";
string strConn = System.Configuration.ConfigurationManager.AppSettings["hrConnection"].ToString();
conn = new SqlConnection(strConn);
cmd=new SqlCommand("DisplayInform34",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@Employee12",SqlDbType.Int,4);
cmd.Parameters["@Employee"].Value=liangjingbo;
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet() ;
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
2.在这种情况下,实现分页,可以这样在代码:
protected
void
GridView1_PageIndexChanging(
object
sender, GridViewPageEventArgs e)
... {
GridView1.PageIndex = e.NewPageIndex;
createBind();
}
... {
GridView1.PageIndex = e.NewPageIndex;
createBind();
}
3.从数据库中取出数据经过处理再绑定到数据库
<
asp:GridView
ID
="GridView1"
runat
="server"
BackColor
="White"
BorderColor
="#7CA3CF"
BorderStyle ="Double" BorderWidth ="3px" CellPadding ="4" GridLines ="Horizontal"
Height ="211px" Style ="left: 81px; position: relative; top: 2px" Width ="533px" >
< FooterStyle BackColor ="White" ForeColor ="#333333" />
< Columns >
< asp:TemplateField HeaderText ="评价统计" >
< HeaderStyle HorizontalAlign ="Center" />
< ItemTemplate >
< asp:Image ID ="Image1" runat ="server" ImageUrl ="~/Praise/4.gif" Style ="position: relative" />
</ ItemTemplate >
</ asp:TemplateField >
< asp:HyperLinkField DataNavigateUrlFields ="院系列表" HeaderText ="具体查看" Text ="查看" DataNavigateUrlFormatString ="PCollegeDetail.aspx?name={0}" />
</ Columns >
< RowStyle BackColor ="White" ForeColor ="#333333" HorizontalAlign ="Center" />
< SelectedRowStyle BackColor ="#339966" Font-Bold ="True" ForeColor ="White" />
< PagerStyle BackColor ="#336666" ForeColor ="White" HorizontalAlign ="Center" />
< HeaderStyle BackColor ="#7CA3CF" Font-Bold ="True" ForeColor ="White" HorizontalAlign ="Center" />
</ asp:GridView >
BorderStyle ="Double" BorderWidth ="3px" CellPadding ="4" GridLines ="Horizontal"
Height ="211px" Style ="left: 81px; position: relative; top: 2px" Width ="533px" >
< FooterStyle BackColor ="White" ForeColor ="#333333" />
< Columns >
< asp:TemplateField HeaderText ="评价统计" >
< HeaderStyle HorizontalAlign ="Center" />
< ItemTemplate >
< asp:Image ID ="Image1" runat ="server" ImageUrl ="~/Praise/4.gif" Style ="position: relative" />
</ ItemTemplate >
</ asp:TemplateField >
< asp:HyperLinkField DataNavigateUrlFields ="院系列表" HeaderText ="具体查看" Text ="查看" DataNavigateUrlFormatString ="PCollegeDetail.aspx?name={0}" />
</ Columns >
< RowStyle BackColor ="White" ForeColor ="#333333" HorizontalAlign ="Center" />
< SelectedRowStyle BackColor ="#339966" Font-Bold ="True" ForeColor ="White" />
< PagerStyle BackColor ="#336666" ForeColor ="White" HorizontalAlign ="Center" />
< HeaderStyle BackColor ="#7CA3CF" Font-Bold ="True" ForeColor ="White" HorizontalAlign ="Center" />
</ asp:GridView >
后台代码如下:
SqlCommand com1
=
new
SqlCommand(
"
PFiguregood
"
, conn);
com1.CommandType = CommandType.StoredProcedure;
// 放到Arraylist name 中,以便与在下面使用
DataTable mess = new DataTable();
mess.Columns.Add( new DataColumn( " 列表 " , typeof ( string )));
mess.Columns.Add( new DataColumn( " 次数1 " , typeof ( int )));
mess.Columns.Add( " 次数2 " , typeof ( int ));
for ( int j = 0 ; j < name.Count; j ++ )
... {
DataRow dr = mess.NewRow();
dr[0] = name[j].ToString()+" ";
dr[1] = Convert.ToInt32(goodCount[j]);
dr[2] = Convert.ToInt32(badCount[j]);
mess.Rows.Add(dr);
}
DataSet dataset = new DataSet();
dataset.Tables.Add(mess);
dataset.Tables[ 0 ].DefaultView.Sort = " 表扬次数 DESC , 批评次数 " ;
this .GridView1.DataSource = dataset.Tables[ 0 ].DefaultView;
this .GridView1.DataBind();
com1.CommandType = CommandType.StoredProcedure;
// 放到Arraylist name 中,以便与在下面使用
DataTable mess = new DataTable();
mess.Columns.Add( new DataColumn( " 列表 " , typeof ( string )));
mess.Columns.Add( new DataColumn( " 次数1 " , typeof ( int )));
mess.Columns.Add( " 次数2 " , typeof ( int ));
for ( int j = 0 ; j < name.Count; j ++ )
... {
DataRow dr = mess.NewRow();
dr[0] = name[j].ToString()+" ";
dr[1] = Convert.ToInt32(goodCount[j]);
dr[2] = Convert.ToInt32(badCount[j]);
mess.Rows.Add(dr);
}
DataSet dataset = new DataSet();
dataset.Tables.Add(mess);
dataset.Tables[ 0 ].DefaultView.Sort = " 表扬次数 DESC , 批评次数 " ;
this .GridView1.DataSource = dataset.Tables[ 0 ].DefaultView;
this .GridView1.DataBind();
4.GridView中的循环
foreach
(GridViewRow rr
in
this
.GridView1.Rows)
... {
if ((rr.Cells[0].Text.ToString()) == "soff")
((CheckBox)rr.FindControl("one")).Checked = true;
}
... {
if ((rr.Cells[0].Text.ToString()) == "soff")
((CheckBox)rr.FindControl("one")).Checked = true;
}