GridView 및 Access 데이터베이스를 통한 데이터 바인딩(asp.net)
40645 단어 GridView
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 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
GridView의 데이터를 EXCEL로 내보내기백그라운드에서 데이터 조회 작업을 할 때, 우리는 GridView 컨트롤러를 사용하여 데이터 표시를 할 수 있습니다. 때로는 GridView 컨트롤러에 표시된 데이터를 EXCEL 파일로 내보내야 하고, GridVie...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.