001.
using
System;
002.
using
System.Data;
003.
using
System.Configuration;
004.
using
System.Collections;
005.
using
System.Web;
006.
using
System.Web.Security;
007.
using
System.Web.UI;
008.
using
System.Web.UI.WebControls;
009.
using
System.Web.UI.WebControls.WebParts;
010.
using
System.Web.UI.HtmlControls;
011.
using
System.Data.SqlClient;
012.
013.
public
partial
class
DataList1 : System.Web.UI.Page
014.
{
015.
SqlConnection objConn;
016.
SqlCommand objCmd;
017.
String strSQL;
018.
019.
protected
void
Page_Load(
object
sender, EventArgs e)
020.
{
021.
String strConnString;
022.
strConnString =
"Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
;
023.
objConn =
new
SqlConnection(strConnString);
024.
objConn.Open();
025.
026.
if
(!Page.IsPostBack)
027.
{
028.
BindData();
029.
}
030.
}
031.
032.
protected
void
BindData()
033.
{
034.
String strSQL;
035.
strSQL =
"SELECT * FROM category"
;
036.
037.
SqlDataReader dtReader;
038.
objCmd =
new
SqlCommand(strSQL, objConn);
039.
dtReader = objCmd.ExecuteReader();
040.
041.
042.
myDataList.DataSource = dtReader;
043.
myDataList.DataBind();
044.
045.
dtReader.Close();
046.
dtReader =
null
;
047.
}
048.
049.
protected
void
Page_UnLoad()
050.
{
051.
objConn.Close();
052.
objConn =
null
;
053.
}
054.
055.
protected
void
myDataList_ItemDataBound(
object
sender, DataListItemEventArgs e)
056.
{
057.
058.
Image img = (Image)(e.Item.FindControl(
"imgPicture"
));
059.
if
(img !=
null
)
060.
{
061.
img.ImageUrl = (
string
)DataBinder.Eval(e.Item.DataItem,
"Picture"
);
062.
063.
064.
065.
}
066.
067.
068.
HyperLink hplCate = (HyperLink)(e.Item.FindControl(
"hplCategory"
));
069.
if
(hplCate !=
null
)
070.
{
071.
hplCate.Text = (
string
)DataBinder.Eval(e.Item.DataItem,
"CategoryName"
);
072.
hplCate.ToolTip = (
string
)DataBinder.Eval(e.Item.DataItem,
"CategoryName"
);
074.
}
075.
076.
}
077.
078.
protected
void
myDataList_EditCommand(
object
source, DataListCommandEventArgs e)
079.
{
080.
myDataList.EditItemIndex = e.Item.ItemIndex;
081.
BindData();
082.
}
083.
084.
protected
void
myDataList_CancelCommand(
object
source, DataListCommandEventArgs e)
085.
{
086.
myDataList.EditItemIndex = -1;
087.
BindData();
088.
}
089.
090.
protected
void
myDataList_UpdateCommand(
object
source, DataListCommandEventArgs e)
091.
{
092.
093.
Label lblCateID = (Label)(e.Item.FindControl(
"lblCateID"
));
094.
095.
TextBox txtCategory = (TextBox)(e.Item.FindControl(
"txtCategory"
));
096.
097.
098.
strSQL =
"UPDATE category SET CategoryName = '"
+ txtCategory.Text +
"' "
+
099.
" WHERE CategoryID = "
+ lblCateID.Text +
" "
;
100.
objCmd =
new
SqlCommand(strSQL, objConn);
101.
objCmd.ExecuteNonQuery();
102.
103.
104.
HtmlInputFile filPicture = (HtmlInputFile)(e.Item.FindControl(
"filPicture"
));
105.
String strFileName;
106.
if
(filPicture.PostedFile.FileName !=
""
)
107.
{
108.
strFileName = System.IO.Path.GetFileName(filPicture.Value);
109.
filPicture.PostedFile.SaveAs(Server.MapPath(
"images/"
+ strFileName));
110.
strSQL =
"UPDATE category SET Picture = 'images/"
+ strFileName +
"' "
+
111.
" WHERE CategoryID = "
+ lblCateID.Text +
" "
;
112.
objCmd =
new
SqlCommand(strSQL, objConn);
113.
objCmd.ExecuteNonQuery();
114.
}
115.
116.
myDataList.EditItemIndex = -1;
117.
BindData();
118.
}
119.
}