001.
<%@ Page Language=
"C#"
Debug=
"true"
%>
002.
003.
<script runat=
"server"
>
004.
SqlDataSource myDSource;
005.
string
strSQL;
006.
007.
008.
void
Page_Load(
object
sender, EventArgs e)
009.
{
010.
011.
myDSource =
new
SqlDataSource();
012.
myDSource.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings[
"ConnectionString"
];
013.
014.
if
(!Page.IsPostBack) {
015.
BindData();
016.
}
017.
}
018.
019.
void
BindData()
020.
{
021.
022.
myDSource.SelectCommand =
"SELECT * FROM [customer]"
;
023.
024.
025.
myGridView.DataSource = myDSource;
026.
myGridView.DataBind();
027.
028.
myDSource =
null
;
029.
030.
}
031.
032.
void
modEditCommand(
object
sender, GridViewEditEventArgs e)
033.
{
034.
myGridView.EditIndex = e.NewEditIndex;
035.
myGridView.ShowFooter =
false
;
036.
BindData();
037.
}
038.
039.
void
modCancelCommand(
object
sender, GridViewCancelEditEventArgs e)
040.
{
041.
myGridView.EditIndex = -1;
042.
myGridView.ShowFooter =
true
;
043.
BindData();
044.
}
045.
046.
void
modDeleteCommand(
object
sender, GridViewDeleteEventArgs e)
047.
{
048.
strSQL =
"DELETE FROM customer WHERE CustomerID = '"
+ myGridView.DataKeys[e.RowIndex].Value +
"'"
;
049.
050.
myDSource.DeleteCommand = strSQL;
051.
myDSource.DeleteCommandType = SqlDataSourceCommandType.Text;
052.
myDSource.Delete();
053.
054.
myGridView.EditIndex = -1;
055.
BindData();
056.
}
057.
058.
059.
void
myGridView_RowCommand(
object
source, GridViewCommandEventArgs e)
060.
{
061.
if
(e.CommandName ==
"Add"
) {
062.
063.
TextBox txtCustomerID = (TextBox)myGridView.FooterRow.FindControl(
"txtAddCustomerID"
);
064.
065.
TextBox txtName = (TextBox)myGridView.FooterRow.FindControl(
"txtAddName"
);
066.
067.
TextBox txtEmail = (TextBox)myGridView.FooterRow.FindControl(
"txtAddEmail"
);
068.
069.
TextBox txtCountryCode = (TextBox)myGridView.FooterRow.FindControl(
"txtAddCountryCode"
);
070.
071.
TextBox txtBudget = (TextBox)myGridView.FooterRow.FindControl(
"txtAddBudget"
);
072.
073.
TextBox txtUsed = (TextBox)myGridView.FooterRow.FindControl(
"txtAddUsed"
);
074.
075.
strSQL =
"INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) "
+
076.
" VALUES ('"
+ txtCustomerID.Text +
"','"
+ txtName.Text +
"','"
+ txtEmail.Text +
"' "
+
077.
" ,'"
+ txtCountryCode.Text +
"','"
+ txtBudget.Text +
"','"
+ txtUsed.Text +
"') "
;
078.
079.
myDSource.InsertCommand = strSQL;
080.
myDSource.InsertCommandType = SqlDataSourceCommandType.Text;
081.
myDSource.Insert();
082.
083.
BindData();
084.
}
085.
}
086.
087.
088.
void
modUpdateCommand(
object
s, GridViewUpdateEventArgs e)
089.
{
090.
091.
092.
TextBox txtCustomerID = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditCustomerID"
);
093.
094.
TextBox txtName = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditName"
);
095.
096.
TextBox txtEmail = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditEmail"
);
097.
098.
TextBox txtCountryCode = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditCountryCode"
);
099.
100.
TextBox txtBudget = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditBudget"
);
101.
102.
TextBox txtUsed = (TextBox)myGridView.Rows[e.RowIndex].FindControl(
"txtEditUsed"
);
103.
104.
strSQL =
"UPDATE customer SET CustomerID = '"
+ txtCustomerID.Text +
"' "
+
105.
" ,Name = '"
+ txtName.Text +
"' "
+
" ,Email = '"
+ txtEmail.Text +
"' "
+
106.
" ,CountryCode = '"
+ txtCountryCode.Text +
"' "
+
" ,Budget = '"
+ txtBudget.Text +
"' "
+
107.
" ,Used = '"
+ txtUsed.Text +
"' "
+
" WHERE CustomerID = '"
+ myGridView.DataKeys[e.RowIndex].Value +
"'"
;
108.
109.
myDSource.UpdateCommand = strSQL;
110.
myDSource.UpdateCommandType = SqlDataSourceCommandType.Text;
111.
myDSource.Update();
112.
113.
myGridView.EditIndex = -1;
114.
myGridView.ShowFooter =
true
;
115.
BindData();
116.
}
117.
118.
119.
</script>
120.
<html>
121.
<head>
122.
<title>ThaiCreate.Com ASP.NET - SqlDataSource</title>
123.
</head>
124.
<body>
125.
<form id=
"form1"
runat=
"server"
>
126.
<asp:GridView id=
"myGridView"
runat=
"server"
AutoGenerateColumns=
"False"
127.
ShowFooter=
"True"
128.
DataKeyNames=
"CustomerID"
129.
OnRowEditing=
"modEditCommand"
130.
OnRowCancelingEdit=
"modCancelCommand"
131.
OnRowDeleting=
"modDeleteCommand"
132.
OnRowUpdating=
"modUpdateCommand"
133.
OnRowCommand=
"myGridView_RowCommand"
>
134.
135.
<Columns>
136.
137.
<asp:TemplateField HeaderText=
"CustomerID"
>
138.
<ItemTemplate>
139.
<asp:Label id=
"lblCustomerID"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'
></asp:Label>
140.
</ItemTemplate>
141.
<EditItemTemplate>
142.
<asp:TextBox id=
"txtEditCustomerID"
size=
"5"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'
></asp:TextBox>
143.
</EditItemTemplate>
144.
<FooterTemplate>
145.
<asp:TextBox id=
"txtAddCustomerID"
size=
"5"
runat=
"server"
></asp:TextBox>
146.
</FooterTemplate>
147.
</asp:TemplateField>
148.
149.
<asp:TemplateField HeaderText=
"Name"
>
150.
<ItemTemplate>
151.
<asp:Label id=
"lblName"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Name") %>'
></asp:Label>
152.
</ItemTemplate>
153.
<EditItemTemplate>
154.
<asp:TextBox id=
"txtEditName"
size=
"10"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Name") %>'
></asp:TextBox>
155.
</EditItemTemplate>
156.
<FooterTemplate>
157.
<asp:TextBox id=
"txtAddName"
size=
"10"
runat=
"server"
></asp:TextBox>
158.
</FooterTemplate>
159.
</asp:TemplateField>
160.
161.
<asp:TemplateField HeaderText=
"Email"
>
162.
<ItemTemplate>
163.
<asp:Label id=
"lblEmail"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Email") %>'
></asp:Label>
164.
</ItemTemplate>
165.
<EditItemTemplate>
166.
<asp:TextBox id=
"txtEditEmail"
size=
"20"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Email") %>'
></asp:TextBox>
167.
</EditItemTemplate>
168.
<FooterTemplate>
169.
<asp:TextBox id=
"txtAddEmail"
size=
"20"
runat=
"server"
></asp:TextBox>
170.
</FooterTemplate>
171.
</asp:TemplateField>
172.
173.
<asp:TemplateField HeaderText=
"CountryCode"
>
174.
<ItemTemplate>
175.
<asp:Label id=
"lblCountryCode"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'
></asp:Label>
176.
</ItemTemplate>
177.
<EditItemTemplate>
178.
<asp:TextBox id=
"txtEditCountryCode"
size=
"2"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'
></asp:TextBox>
179.
</EditItemTemplate>
180.
<FooterTemplate>
181.
<asp:TextBox id=
"txtAddCountryCode"
size=
"2"
runat=
"server"
></asp:TextBox>
182.
</FooterTemplate>
183.
</asp:TemplateField>
184.
185.
<asp:TemplateField HeaderText=
"Budget"
>
186.
<ItemTemplate>
187.
<asp:Label id=
"lblBudget"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Budget") %>'
></asp:Label>
188.
</ItemTemplate>
189.
<EditItemTemplate>
190.
<asp:TextBox id=
"txtEditBudget"
size=
"6"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Budget") %>'
></asp:TextBox>
191.
</EditItemTemplate>
192.
<FooterTemplate>
193.
<asp:TextBox id=
"txtAddBudget"
size=
"6"
runat=
"server"
></asp:TextBox>
194.
</FooterTemplate>
195.
</asp:TemplateField>
196.
197.
<asp:TemplateField HeaderText=
"Used"
>
198.
<ItemTemplate>
199.
<asp:Label id=
"lblUsed"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Used") %>'
></asp:Label>
200.
</ItemTemplate>
201.
<EditItemTemplate>
202.
<asp:TextBox id=
"txtEditUsed"
size=
"6"
runat=
"server"
Text=
'<%# DataBinder.Eval(Container, "DataItem.Used") %>'
></asp:TextBox>
203.
</EditItemTemplate>
204.
<FooterTemplate>
205.
<asp:TextBox id=
"txtAddUsed"
size=
"6"
runat=
"server"
></asp:TextBox>
206.
<asp:Button id=
"btnAdd"
runat=
"server"
Text=
"Add"
CommandName=
"Add"
></asp:Button>
207.
</FooterTemplate>
208.
</asp:TemplateField>
209.
210.
<asp:CommandField ShowEditButton=
"True"
CancelText=
"Cancel"
DeleteText=
"Delete"
EditText=
"Edit"
UpdateText=
"Update"
HeaderText=
"Modify"
/>
211.
<asp:CommandField ShowDeleteButton=
"True"
HeaderText=
"Delete"
/>
212.
213.
</Columns>
214.
</asp:GridView>
215.
</form>
216.
</body>
217.
</html>