Field's Space

.NET技术学习

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

代码用的是Microsoft  WebCast上面的讲课代码!

前台代码如下:

 1<%@ Page language="c#" Codebehind="AdjustWidth.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.AdjustWidth" %>
 2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 3<HTML>
 4    <HEAD>
 5        <title>DataGrid使用举例</title>
 6        <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
 7        <meta name="CODE_LANGUAGE" Content="C#">
 8        <meta name="vs_defaultClientScript" content="JavaScript">
 9        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
10    </HEAD>
11    <body MS_POSITIONING="GridLayout">
12        <form id="Form1" method="post" runat="server">
13            <FONT face="宋体">
14                <asp:DataGrid id="dgShow" style="Z-INDEX: 101; LEFT: 31px; POSITION: absolute; TOP: 93px" runat="server" Width="842px" Height="172px" BorderColor="Tan" BorderWidth="1px" BackColor="LightGoldenrodYellow" CellPadding="2" GridLines="None" ForeColor="Black" PageSize="1" AutoGenerateColumns="False">
15                    <SelectedItemStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedItemStyle>
16                    <AlternatingItemStyle BackColor="PaleGoldenrod"></AlternatingItemStyle>
17                    <HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle>
18                    <FooterStyle BackColor="Tan"></FooterStyle>
19                    <Columns>
20                        <asp:BoundColumn DataField="StudentID" ReadOnly="True" HeaderText="学生ID"></asp:BoundColumn>
21                        <asp:BoundColumn DataField="StudentName" HeaderText="学生姓名"></asp:BoundColumn>
22                        <asp:BoundColumn DataField="StudentPass" HeaderText="密码"></asp:BoundColumn>
23                        <asp:BoundColumn DataField="Sex" HeaderText="性别"></asp:BoundColumn>
24                        <asp:BoundColumn DataField="Birthday" HeaderText="生日" DataFormatString="{0:yyyy-M-d}"></asp:BoundColumn>
25                        <asp:BoundColumn DataField="Email" HeaderText="邮件地址"></asp:BoundColumn>
26                        <asp:TemplateColumn HeaderText="性别模板列">
27                            <ItemTemplate>
28                                <asp:RadioButton id=RadioButton2 runat="server" Text="男" Checked='<%# DataBinder.Eval(Container, "DataItem.Sex") %>' Enabled="False">
29                                </asp:RadioButton>
30                                <asp:RadioButton id=RadioButton1 runat="server" Text="女" Checked='<%# !(bool)DataBinder.Eval(Container, "DataItem.Sex") %>' Enabled="False">
31                                </asp:RadioButton>
32                            </ItemTemplate>
33                            <EditItemTemplate>
34                                <asp:RadioButton id=cbSex runat="server" Text="男" Checked='<%# DataBinder.Eval(Container, "DataItem.Sex") %>' GroupName="Sex">
35                                </asp:RadioButton>
36                                <asp:RadioButton id=RadioButton4 runat="server" Text="女" Checked='<%# !(bool)DataBinder.Eval(Container, "DataItem.Sex") %>' GroupName="Sex">
37                                </asp:RadioButton>
38                            </EditItemTemplate>
39                        </asp:TemplateColumn>
40                        <asp:ButtonColumn Text="选择" HeaderText="选择" CommandName="Select"></asp:ButtonColumn>
41                        <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="操作" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
42                        <asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete"></asp:ButtonColumn>
43                        <asp:HyperLinkColumn Text="点击查看" DataNavigateUrlField="StudentID" DataNavigateUrlFormatString="Show.aspx?ID={0}" DataTextField="StudentName" HeaderText="详细信息"></asp:HyperLinkColumn>
44                    </Columns>
45                    <PagerStyle HorizontalAlign="Center" ForeColor="DarkSlateBlue" BackColor="PaleGoldenrod"></PagerStyle>
46                </asp:DataGrid></FONT>
47        </form>
48    </body>
49</HTML>
50

我感觉加高亮的地方是这个DataGrid界面设计应该注意的地方。

后台核心代码如下:

  1private void Page_Load(object sender, System.EventArgs e)
  2        {
  3            // 在此处放置用户代码以初始化页面
  4            if(!IsPostBack)
  5                BindData();
  6            
  7        }

  8        private void BindData()
  9        {
 10            string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
 11            SqlConnection con = new SqlConnection(strCon);
 12            SqlDataAdapter da = new SqlDataAdapter("Select * from tbStudentinfo",con);
 13            DataSet ds = new DataSet();
 14            da.Fill(ds,"studentinfo");
 15            dgShow.DataSource = ds.Tables["studentinfo"].DefaultView;
 16            dgShow.DataBind();
 17            dgShow.Columns[0].Visible = false;            
 18            
 19        }

 20        Web Form Designer generated code
 46
 47        private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 48        {
 49            dgShow.EditItemIndex = e.Item.ItemIndex;
 50            BindData();
 51
 52        }

 53    
 54        private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 55        {
 56            dgShow.EditItemIndex = -1;
 57            BindData();
 58        }

 59        private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
 60        {
 61            dgShow.CurrentPageIndex = e.NewPageIndex;
 62            BindData();
 63        }

 64        private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 65        {
 66            if(dgShow.Items.Count==1)
 67            {
 68                if(dgShow.CurrentPageIndex!=0)
 69                    dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
 70            }

 71            string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
 72            ExecuteSql(strSql);
 73            BindData();
 74
 75        }

 76        ////////////////////////////////////////////////////////////
 77        //说明:执行制定SQL语句/////////////////////////////////////
 78        ///////////////////////////////////////////////////////////
 79        private void ExecuteSql(string strSql)
 80        {
 81            try
 82            {
 83                string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
 84                SqlConnection conn =new SqlConnection(strconn);
 85                SqlCommand com = new SqlCommand(strSql,conn);
 86                conn.Open();
 87                com.ExecuteNonQuery();
 88                conn.Close();
 89            }
 
 90            catch(Exception e)
 91            {
 92                Response.Write("<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
 93                            
 94            }

 95        }

 96        
 97        private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 98        {
 99            string strStudentID = e.Item.Cells[0].Text;//处于非编辑状态
100            string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
101            string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
102            string strSex = ((CheckBox)(e.Item.Cells[3].FindControl("cbSex"))).Checked?"1":"0";
103            string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
104            string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
105            string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
106            strSql +=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
107            ExecuteSql(strSql);
108            dgShow.EditItemIndex = -1;
109            BindData();
110
111        }

112
113        private void dgShow_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
114        {
115            if (e.Item.ItemType == ListItemType.EditItem) 
116            {
117                for (int i=0;i<e.Item.Cells.Count;i++)
118                {
119                    if(e.Item.Cells[i].Controls.Count>0)
120                    {
121                        try
122                        {
123                            TextBox t =(TextBox)e.Item.Cells[i].Controls[0];
124                            t.Width=130;
125                        }

126                        catch(Exception ee)
127                        {
128                        }

129                    }

130                }

131            }

132        }

有兴趣的朋友,我们可以讨论一下DataGrid方面的应用!
posted on 2006-06-29 16:52  Field  阅读(340)  评论(0编辑  收藏  举报