GridViewとAccessデータベース実装データバインド(asp.net)
40645 ワード
フロントコード:
バックグラウンドコード:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" .aspx.cs" Inherits=" " %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <title></title>
7 <link href="Styles/main.css" rel="stylesheet" type="text/css" />
8 </head>
9 <body bgcolor="#ffffcc">
10 <form id="form1" runat="server">
11 <div style="font-family: ; font-size: larger; color: #FF00FF; height: 29px; width: 1340px;">
12 <asp:GridView ID="gvw" DataKeyNames="id" runat="server" AllowPaging="True" AllowSorting="True"
13 AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
14 AutoGenerateSelectButton="True" DataSourceID="AccessDataSrc" Style="font-size: large"
15 Width="732px" OnRowUpdating="gvw_RowUpdating" OnRowDataBound="gvw_RowDataBound">
16 <Columns>
17 <asp:BoundField DataField="id" ReadOnly="true" InsertVisible="true" HeaderText="id" />
18 <asp:CommandField CancelText=" " EditText=" " UpdateText=" " DeleteText=" " />
19 <asp:TemplateField HeaderText=" " runat="Server" SortExpression="num">
20 <EditItemTemplate>
21 <asp:TextBox Text='<%# Bind("num") %>' ID="txtNum" runat="server" />
22 </EditItemTemplate>
23 <ItemTemplate>
24 <asp:Label ID="lblNum" runat="server" Text='<%# Bind("num") %>'></asp:Label>
25 </ItemTemplate>
26 </asp:TemplateField>
27 <asp:TemplateField HeaderText=" ">
28 <EditItemTemplate>
29 <asp:TextBox Text='<%# Bind("name") %>' ID="txtName" runat="server" />
30 </EditItemTemplate>
31 <ItemTemplate>
32 <asp:Label ID="lblName" runat="server" Text='<%# Bind("name") %>'></asp:Label>
33 </ItemTemplate>
34 </asp:TemplateField>
35 <asp:TemplateField HeaderText=" ">
36 <EditItemTemplate>
37 <asp:TextBox Text='<%# Bind("sex") %>' ID="txtSex" runat="server" />
38 </EditItemTemplate>
39 <ItemTemplate>
40 <asp:Label ID="lblSex" runat="server" Text='<%# Bind("sex") %>'></asp:Label>
41 </ItemTemplate>
42 </asp:TemplateField>
43 <asp:TemplateField HeaderText=" " SortExpression="contact">
44 <EditItemTemplate>
45 <asp:TextBox Text='<%# Bind("contact") %>' ID="txtContact" runat="server" />
46 </EditItemTemplate>
47 <ItemTemplate>
48 <asp:Label ID="lblContact" runat="server" Text='<%# Bind("contact") %>'></asp:Label>
49 </ItemTemplate>
50 </asp:TemplateField>
51 <asp:TemplateField HeaderText=" ">
52 <EditItemTemplate>
53 <asp:TextBox Text='<%# Bind("address") %>' ID="txtAddress" runat="server" />
54 </EditItemTemplate>
55 <ItemTemplate>
56 <asp:Label ID="lblAddress" runat="server" Text='<%# Bind("address") %>'></asp:Label>
57 </ItemTemplate>
58 </asp:TemplateField>
59 </Columns>
60 </asp:GridView>
61 <asp:LinkButton ID="lbtnAddNew" runat="Server" OnClick="lkbtnAddNew_Click">AddNew</asp:LinkButton>
62 <asp:Panel runat="server" ID="pnlAdd" Visible="false">
63 <table id="addNew">
64 <tr>
65 <td>
66 <label for="txtNum">
67 :</label>
68 </td>
69 <td>
70 <asp:TextBox ID="txtNum" runat="Server"></asp:TextBox>
71 </td>
72 </tr>
73 <tr>
74 <td>
75 <label for="txtName">
76 :</label>
77 </td>
78 <td>
79 <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
80 </td>
81 </tr>
82 <tr>
83 <td>
84 <label for="txtSex">
85 :</label>
86 </td>
87 <td>
88 <asp:TextBox ID="txtSex" runat="server"></asp:TextBox>
89 </td>
90 </tr>
91 <tr>
92 <td>
93 <label for="txtContact">
94 :</label>
95 </td>
96 <td>
97 <asp:TextBox ID="txtContact" runat="server"></asp:TextBox>
98 </td>
99 </tr>
100 <tr>
101 <td>
102 <label for="txtAddress">
103 :</label>
104 </td>
105 <td>
106 <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
107 </td>
108 </tr>
109 </table>
110 <asp:LinkButton ID="lbtnConfirm" runat="Server" OnClick="btnConfirm_Click">Submit</asp:LinkButton>
111 <asp:LinkButton ID="lbtnCancel" runat="server" OnClick="btnCancel_Click">Cancel</asp:LinkButton>
112 </asp:Panel>
113 <asp:AccessDataSource ID="AccessDataSrc" runat="server" DataFile="~/website/App_Data/people2003.mdb"
114 SelectCommand="SELECT * FROM [t_01]" DeleteCommand="delete from [t_01] where id=@id"
115 InsertCommand="insert into [t_01]([num],[name],[sex],[contact],[address]) values(@num,@name,@sex,@contact,@address)">
116 <DeleteParameters>
117 <asp:Parameter Name="id" />
118 </DeleteParameters>
119 <InsertParameters>
120 <asp:ControlParameter ControlID="txtNum" Name="num" Type="String" PropertyName="Text" />
121 <asp:ControlParameter ControlID="txtName" Name="name" Type="String" PropertyName="Text" />
122 <asp:ControlParameter ControlID="txtSex" Name="sex" Type="String" PropertyName="Text" />
123 <asp:ControlParameter ControlID="txtContact" Name="contact" Type="String" PropertyName="Text" />
124 <asp:ControlParameter ControlID="txtAddress" Name="address" Type="String" PropertyName="Text" />
125 </InsertParameters>
126 </asp:AccessDataSource>
127 </div>
128 </form>
129 </body>
130 </html>
バックグラウンドコード:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 public partial class : System.Web.UI.Page {
9 protected void Page_Load(object sender, EventArgs e) {
10 }
11 //update.
12 protected void gvw_RowUpdating(object sender, GridViewUpdateEventArgs e) {
13 AccessDataSrc.UpdateCommand = "update [t_01] set [num]=@num,name=@name,sex=@sex,contact=@contact,address=@address where [id]=@id";
14
15 string num = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtNum")).Text.Trim();
16 string name = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtName")).Text.Trim();
17 string sex = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtSex")).Text.Trim();
18 string contact = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtContact")).Text.Trim();
19 string address = ((TextBox)gvw.Rows[e.RowIndex].FindControl("txtAddress")).Text.Trim();
20
21 AccessDataSrc.UpdateParameters.Add("num", TypeCode.String, num);
22 AccessDataSrc.UpdateParameters.Add("name", TypeCode.String, name);
23 AccessDataSrc.UpdateParameters.Add("sex", TypeCode.String, sex);
24 AccessDataSrc.UpdateParameters.Add("contact", TypeCode.String, contact);
25 AccessDataSrc.UpdateParameters.Add("address", TypeCode.String, address);
26 AccessDataSrc.DataBind();
27 }
28 //Confirm to perform delete operation.
29 protected void gvw_RowDataBound(object sender, GridViewRowEventArgs e) {
30 if (e.Row.RowType == DataControlRowType.DataRow) {
31 if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) {
32 LinkButton btnDelete = (LinkButton)e.Row.Cells[0].Controls[2];
33 btnDelete.Attributes["onclick"] = "if(!confirm('Centain to delete?')) return false";
34 //var c = gvw.Rows[e.Row.RowIndex].Cells[0].Controls[0];
35 }
36 }
37 }
38 //Confirm to add new.
39 protected void btnConfirm_Click(object sender, EventArgs e) {
40 try {
41 AccessDataSrc.Insert();
42 txtNum.Text = "";
43 txtName.Text = "";
44 txtSex.Text = "";
45 txtContact.Text = "";
46 txtAddress.Text = "";
47 Response.Write("<script>alert(' !')</script>");
48 }
49 catch (System.Exception ex) {
50
51 }
52 }
53 //Cancel to add new.
54 protected void btnCancel_Click(object sender, EventArgs e) {
55 txtNum.Text = "";
56 txtName.Text = "";
57 txtSex.Text = "";
58 txtContact.Text = "";
59 txtAddress.Text = "";
60 pnlAdd.Visible = false;
61 lbtnAddNew.Visible = true;
62 }
63 //Add new.
64 protected void lkbtnAddNew_Click(object sender, EventArgs e) {
65 pnlAdd.Visible = true;
66 lbtnAddNew.Visible = false;
67 }
68 }