Asp.net study(9)
DataGrid 控件 (学习DataGrid Column 的5种类型)
用到的DataGrid属性:id,runat,HeaderStyle-BackColor,AlternatingItemStyle-BackColor,AutoGenerateColumns,OnItemCommand
1.BoundColumn
用到的属性:DataField,HeaderText
2.HyperLinkColumn
用到的属性:HeaderText,DataTextField,DataNavigateUrlField,DataNavigateUrlFormatString
3.ButtonColumn
用到的属性:HeaderText,Text,ButtonType
ButtonType在使用的过程中出现了问题,当设置ButtonType=PushButton时调试出错:System.ArgumentException: 回发或回调参数无效
(明天再研究)
今天的代码
<%@ Page Language=”C#” ContentType=”text/html” ResponseEncoding=”gb2312″ %>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.OleDb” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<script language=”c#” runat=”server”>
public void Page_Load(Object src,EventArgs e)
{
string Myconnstring=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=”+Server.MapPath(“.”)+”.\\data\\score.mdb;”;
OleDbConnection Myconn=new OleDbConnection(Myconnstring);
Myconn.Open();
string strsel=”select * from score”;
OleDbDataAdapter MyAdapter=new OleDbDataAdapter(strsel,Myconn);
DataSet ds=new DataSet();
MyAdapter.Fill(ds,”table1″);
score.DataSource=ds;
score.DataMember=”table1″;
score.DataBind();
}
public void DataGrid_ItemCommand(Object sender,DataGridCommandEventArgs e)
{
TableRow tr=e.Item;
lblname.Text=tr.Cells[1].Text;
lblmath.Text=tr.Cells[2].Text;
lblenglish.Text=tr.Cells[3].Text;
lblchinese.Text=tr.Cells[4].Text;
}
</script>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />
<title>DataGrid</title>
</head>
<body>
<table>
<tr>
<td>
<form runat=”server” action=”BoundColumn.aspx” method=”get”>
<asp:DataGrid id=”score” runat=”server”
HeaderStyle-BackColor=”#666666″
AlternatingItemStyle-BackColor=”#99CC00″
AutoGenerateColumns=”false”
OnItemCommand=”DataGrid_ItemCommand”
>
<Columns>
<asp:ButtonColumn HeaderText=”Play area” Text=”Show details” ButtonType=”PushButton” />
<asp:HyperLinkColumn HeaderText =”Name” DataTextField=”s_Name” DataNavigateUrlField=”s_Name” DataNavigateUrlFormatString=”boundcolumn.aspx?name={0}”/>
<asp:BoundColumn DataField=”s_Math” HeaderText=”Math Score”/>
<asp:BoundColumn DataField=”s_English” HeaderText=”English Score”/>
<asp:BoundColumn DataField=”s_Chinese” HeaderText=”Chinese Score”/>
</Columns>
</asp:DataGrid>
</form>
</td>
<td valign=”top”>
<table>
<tr><td>Chosed name</td><td><asp:Label ID=”lblname” runat=”server”/></td></tr>
<tr><td>Math</td><td><asp:Label ID=”lblmath” runat=”server”/></td></tr>
<tr><td>English</td><td><asp:Label ID=”lblenglish” runat=”server”/></td></tr>
<tr><td>Chinese</td><td><asp:Label ID=”lblchinese” runat=”server”/></td></tr>
<td>
</table>
</tr>
</table>
</body>
</html>