Chức năng viết bài,
Sau khi đăng nhập hệ thống, người dfng có thể thấy chức năng viết bài trên thanh menu trên cfng, bên tay phải màn hình.
Nhấp vào chức năng này để tạo bài viết mới.
Nhấp vào thêm mới nếu muốn tạo bài viết mới Nhấn vào sửa- để sửa bài viết của mình Nhấn vào xoá để xoá bài viết tương tứng
Chức năng đăng nhập
Để đăng nhập hệ thống, nhấp vào chữ đăng nhập- ở thanh menu trên cfng, bên phải màn hình
Hiện nên màn hình đăng nhập, Nhập tài khoản và mật khẩu vào Sau đó nhấn đăng nhập.
Nếu tài khoản và mật khẩu đúng thì sẽ đăng nhập thành công Nếu tài khoản và mật khẩu sai thì sẽ nhập lại hoặc nhấn vào đăng ký- Chức năng đăng nhập
protected void btndangnhap_Click(object sender, EventArgs e) {
dangnhap1(txtemail.Text,
MD5.MD5.GetMD5Hash(txtsodienthoai.Text));
}
public void dangnhap1(string tendangnhap123, string matkhau123) {
bool kiemtra;
kiemtra = ac.kiemtra_dangnhap("taikhoannd", tendangnhap123, matkhau123);
(kiemtra)if
27
{
Session["ChuaDangNhap"] = true; Session["emailkhachhang"] = txtemail.Text;
load_thongtin(txtemail.Text);
Session["keycode"] = 1;
Response.Redirect("dangnhapthanhcong.aspx");
} else {
lblthongbao.Text = "Đăng nhập không thành công"; txtemail.Focus();
} } Phần server
1. public Boolean kiemtra_dangnhap(string tenbang, string uid, string pass)
2. {
3. c = 0;int
4. string sql = "select count(*) from " + tenbang + " where ten='" + uid + "' and matkhau = '" + pass + "' and trangthai='1'";
5. SqlConnection cn = new SqlConnection();
6. SqlCommand cmd = new SqlCommand();
7. try 8. {
9. cn = getConnect();
10.
11. cmd.CommandText = sql;
12. cmd.Connection = cn;
13. c = (int)cmd.ExecuteScalar();
14. cn.Close();
15. cmd.Dispose();
16. } 17.
18. catch 19. {
20. return false; 21. }
22. finally 23. {
24. cn.Close();
25. cmd.Dispose();
26. } 27.
28. return (c != 0);
29. } DANH MỤC TIN
<div class="list-group">
<ahref="#"class="list-group-item active">
<asp Label: ID="lblthongbao"runat="server"Text="Danh mục"></asp:Label>
</ >a
<asp:DataList ID="DataList1" runat="server" DataKeyField="id"
Width="100%">
<ItemTemplate>
<ahref="listtin.aspx?c=<% Eval(# "id")%>&n=<
% doiten2(Eval(# "name"))%>" class="list-group-item">
<%#rutgonten(Eval("name"))%>
<spanclass="badge">
<%#Eval("sobanghi")%> </span>
</ >a
29
</ItemTemplate>
</asp:DataList>
</div>
protected void Page_Load(object sender, EventArgs e) {
domain = Request.ApplicationPath.ToString();
(!IsPostBack)if {
loaddata();
} }
BasicPage xulychung = new BasicPage();
public string sobaiviet;
public void loaddata() {
var dt = xulychung.Load_Theo_ma("CatalogNews_Hien_blog", Session["keycode"].ToString());
(dt != if null && dt.Rows.Count > 0) {
DataList1.DataSource = dt;
DataList1.DataBind();
} else {
lblthongbao.Text = "Chưa có tin mới"; }
}
public string doiten(object obj) {
string b = obj.ToString();
string kq = ReplaceUnicode.ReplaceUnicodes(b);
return xulychung.xoakhoangtrang(b);
}
public string doiten2(object obj) {
string b = obj.ToString();
string kq = xulychung.CatChuoi(b, 50);
return xulychung.xoakitudacbiet(kq);
}
public string rutgonten(object obj) {
string b = obj.ToString();
return xulychung.CatChuoi(b, 50);
} }
ĐĂNG KÝ
<script type="text/javascript">
$(document).ready(function () { $("#btnSelectImg").click(function () { var finder = new CKFinder();
finder.selectActionFunction = function (fileUrl) { $('#<% txtImgUrl.ClientID %>').val(fileUrl);= };
finder.popup();
});
});
</script>
<div class="panel panel-primary">
<divclass="panel-heading">
<h3class="panel-title text-center">
31
<asp Label: ID="lblthongbao"runat="server"Text="ĐĂNG KÍ TÀI KHOẢN"></asp:Label>
</ >h3
<divclass="text-center">
<asp Label: ID="lblresult"runat="server"ForeColor=""
Text="* Sau khi tài khoản được kích hoạt, bạn có thể đăng bài viết, bình luận, và chia sẻ nhiều hơn. Cảm ơn bạn đã tham gia với chúng
tôi!"></asp:Label>
</div>
</div>
<divclass="panel-body">
<divclass="form-horizontal">
<divclass="form-group">
<labelclass="control-label col-sm-3">TÊN ĐĂNG NHẬP:</label>
<divclass="col-sm-9">
<asp TextBox: ID="txtten"runat="server"class="form- control"></asp:TextBox>
<asp CustomValidator: ID="CustomValidator1"runat="server"
ControlToValidate="txtten" ErrorMessage="* Không để trống, Không được có khoảng trắng"></asp:CustomValidator>
</div>
</div>
<divclass="form-group">
<labelclass="control-label col-sm-3">MẬT KHẨU:</label>
<divclass="col-sm-9">
<asp TextBox: ID="txtmatkhau"runat="server"class="form- control" TextMode="Password"></asp:TextBox>
</div>
</div>
<divclass="form-group">
<labelclass="control-label col-sm-3">NHẬP LẠI MẬT KHẨU :</label>
<divclass="col-sm-9">
<asp TextBox: ID="txtmatkhaumoi"runat="server"class="form- control" TextMode="Password"></asp:TextBox>
<asp CompareValidator: ID="CompareValidator1"runat="server"
ErrorMessage="* Kiểm tra lại mật khẩu" ControlToCompare="txtmatkhau"
ControlToValidate="txtmatkhaumoi"></asp:CompareValidator>
</div>
</div>
<divclass="form-group">
<labelclass="control-label col-sm-3">TÊN HIỂN THỊ:</label>
<divclass="col-sm-9">
<asp TextBox: ID="txttenhienthi"runat="server"class="form- control"></asp:TextBox>
</div>
</div>
<divclass="form-group">
<labelclass="control-label col-sm-3">ẢNH ĐẠI DIỆN:</label>
<divclass="col-sm-9">
<inputtype="button"id="btnSelectImg"value="Chọn ..." />
<asp Image: ID="Image1"runat="server"Height="90px"
Width="90px" ImageUrl="/Uploads/noimage.png" />
<asp TextBox: ID="txtImgUrl"runat="server"
Width="364px">/Uploads/noimage.png</asp:TextBox>
</div>
</div>
<divclass="form-group">
<labelclass="control-label col-sm-3">ĐIỆN THOẠI:</label>
33
<divclass="col-sm-9">
<asp TextBox: ID="txtdienthoai"runat="server"class="form- control"></asp:TextBox>
</div>
</div>
<divclass="form-group">
<labelclass="control-label col-sm-3">EMAIL:</label>
<divclass="col-sm-9">
<asp TextBox: ID="txtemail"runat="server"class="form- control"></asp:TextBox>
</div>
</div>
<divclass="form-group">
<labelclass="control-label col-sm-3">ĐỊA CHỈ:</label>
<divclass="col-sm-9">
<asp TextBox: ID="txtdiachi"runat="server"class="form- control"></asp:TextBox>
</div>
</div>
</div>
</div>
<divclass="panel-footer">
<divclass="row text-center">
<asp TextBox: ID="txtma"runat="server"ReadOnly="True"
Width="61px" Visible="False"> </0 asp:TextBox>
<asp TextBox: ID="txtidnhanvien"runat="server"ReadOnly="True"
Width="61px" Visible="False"> </0 asp:TextBox>
<asp TextBox: ID="txtiduser"runat="server"ReadOnly="True"
Width="61px" Visible="False"> </0 asp:TextBox>
<asp Button: ID="btnluu"runat="server"Text="Lưu thông tin"
OnClick="btnluu_Click" CssClass="btn btn-warning " />
</div>
</div>
</div>
PHẦN SERVER
protected void Page_Load(object sender, EventArgs e) {
txtma.Text = "0"; }
BasicPage xuly = new BasicPage();
taikhoandtond xulydoituong = new taikhoandtond();
public void loaddoituong() {
xulydoituong.id = int.Parse(txtma.Text);
xulydoituong.ten = txtten.Text;
xulydoituong.idquyen = 1;
xulydoituong.tenhienthi = txttenhienthi.Text;
xulydoituong.anh = txtImgUrl.Text;
xulydoituong.dienthoai = txtdienthoai.Text;
xulydoituong.email = txtemail.Text;
xulydoituong.diachi = txtdiachi.Text;
xulydoituong.ngaytao = DateTime.Today.ToString("dd/MM/yyyy");
xulydoituong.trangthai = "0"; xulydoituong.rank = "10"; xulydoituong.matkhau =
MD5.MD5.GetMD5Hash(txtmatkhaumoi.Text);
}
public void them() {
35
// load doi tuong try
{
loaddoituong();
xulydoituong.them(xulydoituong);
xuly.ShowAlert(this.Page, "Cảm ơn bạn đã đăng ký, tài khoản của bạn sẽ được duyệt trước khi bạn có thể sử dụng. Vui lòng chờ!. Cảm ơn");
}
catch (Exception ex) {
lblresult.Text = "Lỗi khi thao tác, vui lòng thử lại. " + ex.ToString();
}
}
protected void btnluu_Click(object sender, EventArgs e) {
them();
}
PHẦN DAL
public class taikhoandtond {
publictaikhoandtond() {
//
// TODO: Add constructor logic here //
}
public int id { get set; ; } public string ten { get set; ; } public string matkhau { get set; ; } public int idquyen { get set; ; } public string tenhienthi { get set; ; } public string anh { get set; ; } public string dienthoai { get set; ; } public string email { get set; ; } public string diachi
{ get set; ; }
public string ngaytao { get set; ; } public string trangthai { get set; ; } public string rank { get set; ; } DataAccess ac = new DataAccess();
public DataTable hien() {
return ac.GetDataTable_Store("blogtaikhoannd_hien");
}
public void them(taikhoandtond doituong) {
string chuoi = " INSERT INTO taikhoannd "; chuoi += " VALUES ( ";
chuoi += " N'" + doituong.ten + "',"; chuoi += " N'" + doituong.matkhau + "',"; chuoi += " N'" + doituong.idquyen + "',"; chuoi += " N'" + doituong.tenhienthi + "',"; chuoi += " N'" + doituong.anh + "',"; chuoi += " N'" + doituong.dienthoai + "',"; chuoi += " N'" + doituong.email + "',"; chuoi += " N'" + doituong.diachi + "',"; chuoi += " N'" + doituong.ngaytao + "',";
37
chuoi += " N'" + doituong.trangthai + "',"; chuoi += " N'" + doituong.rank + "') "; try
{
ac.ExecuteCommandtext(chuoi);
}
catch (Exception ex) {
ex.ToString();
} }
public void sua(taikhoandtond doituong) {
string chuoi = " UPDATE taikhoannd SET "; chuoi += " ten =N'" + doituong.ten + "',";
chuoi += " matkhau =N'" + doituong.matkhau + "',"; chuoi += " idquyen =N'" + doituong.idquyen + "',"; chuoi += " tenhienthi =N'" + doituong.tenhienthi + "',"; chuoi += " anh =N'" + doituong.anh + "',";
chuoi += " dienthoai =N'" + doituong.dienthoai + "',"; chuoi += " email =N'" + doituong.email + "',"; chuoi += " diachi =N'" + doituong.diachi + "',"; chuoi += " ngaytao =N'" + doituong.ngaytao + "',"; chuoi += " rank =N'" + doituong.rank + "',"; chuoi += " trangthai = N'" + doituong.trangthai + ;"'"
chuoi += " WHERE id = N'" + doituong.id + ;"'"
try {
ac.ExecuteCommandtext(chuoi);
}
catch (Exception ex)
{
ex.ToString();
} }
public void suacanhan(taikhoandtond doituong) {
string chuoi = " UPDATE taikhoannd SET "; chuoi += " matkhau =N'" + doituong.matkhau + "',"; chuoi += " tenhienthi =N'" + doituong.tenhienthi + "',"; chuoi += " anh =N'" + doituong.anh + "',";
chuoi += " dienthoai =N'" + doituong.dienthoai + "',"; chuoi += " email =N'" + doituong.email + "',"; chuoi += " diachi =N'" + doituong.diachi + ;"'"
chuoi += " WHERE id = N'" + doituong.id + ;"'"
try {
ac.ExecuteCommandtext(chuoi);
}
catch (Exception ex) {
ex.ToString();
} }
public void xoa(taikhoandtond doituong) {
string chuoi = "DELETE FROM taikhoannd "; chuoi += " WHERE id = N'" + doituong.id + "' "; try
{
ac.ExecuteCommandtext(chuoi);
}
39
catch (Exception ex) {
ex.ToString();
} } }
PHẦN TIN TỨC public class tintucdto {
publictintucdto() {
//
// TODO: Add constructor logic here //
}
public int id { get set; ; } public string ten { get set; ; } public string noidung { get set; ; } public int idchuyenmuc { get set; ; } public int idnhanvien { get set; ; } public string ngaytao { get set; ; } public string trangthai { get set; ; } public int iduser { get set; ; } public int luotxem { get set; ; } public string noibat { get set; ; } DataAccess ac = new DataAccess();
public DataTable hien() {
return ac.GetDataTable_Store("blogtintuc_hien");
}
public void them(tintucdto doituong) {
string chuoi = " INSERT INTO tintuc "; chuoi += " VALUES ( ";
chuoi += " N'" + doituong.ten + "',"; chuoi += " N'" + doituong.noidung + "',"; chuoi += " N'" + doituong.idchuyenmuc + "',"; chuoi += " N'" + doituong.idnhanvien + "',"; chuoi += " N'" + doituong.ngaytao + "',"; chuoi += " N'" + doituong.trangthai + "',"; chuoi += " N'" + doituong.iduser + "',"; chuoi += " N'" + doituong.luotxem + "',"; chuoi += " N'" + doituong.noibat + "') "; try
{
ac.ExecuteCommandtext(chuoi);
}
catch (Exception ex) {
ex.ToString();
} }
public void sua(tintucdto doituong) {
string chuoi = " UPDATE tintuc SET "; chuoi += " ten=N'" + doituong.ten + "',";
chuoi += " noidung=N'" + doituong.noidung + "',";
chuoi += " idchuyenmuc=N'" + doituong.idchuyenmuc + "',"; chuoi += " idnhanvien=N'" + doituong.idnhanvien + "',"; chuoi += " ngaytao=N'" + doituong.ngaytao + "',";
41
chuoi += " trangthai=N'" + doituong.trangthai + "',"; chuoi += " iduser=N'" + doituong.iduser + "',"; chuoi += " luotxem=N'" + doituong.luotxem + "',"; chuoi += " noibat = N'" + doituong.noibat + ;"'"
chuoi += " WHERE id = N'" + doituong.id + ;"'"
try {
ac.ExecuteCommandtext(chuoi);
}
catch (Exception ex) {
ex.ToString();
}
}
public void xoa(tintucdto doituong) {
string chuoi = "DELETE FROM tintuc "; chuoi += " WHERE id = N'" + doituong.id + "' "; try
{
ac.ExecuteCommandtext(chuoi);
}
catch (Exception ex) {
ex.ToString();
} } GIAO DIỆN
<div class="alert alert-dismissable alert-info">
DANH SÁCH TIN
<asp Label: ID="lblresult"runat="server"Text="* Thay đổi thông tin"
ForeColor="Red"></asp:Label>
<divclass="pull-right">
<asp ImageButton: ID="themmoi"runat="server"
ImageUrl="~/Uploads/add.png" OnClick="themmoi_Click" />
<asp ImageButton: ID="btnlamtuoi"runat="server"
ImageUrl="~/Uploads/refresh.png" OnClick="btnlamtuoi_Click" />
<asp ImageButton: ID="timkiem"runat="server"
ImageUrl="~/Uploads/find.png" />
</div>
</div>
<div class="dieukhien">
<asp Label: ID="Label2"runat="server"Text="Trạng thái: "></asp:Label>
<asp:DropDownList ID="droptrangthai" runat="server"
AutoPostBack="True">
<asp ListItem: Value="1">HOẠT ĐỘNG</asp:ListItem>
<asp ListItem: Value="0">DỪNG</asp:ListItem>
</asp:DropDownList>
<asp Button: ID="btnhientheotrangthai"runat="server"Text="Hiện theo Trạng thái"OnClick="btnhientheotrangthai_Click"CssClass="btn btn- primary btn-sm" />
<asp Button: ID="btncapnhat"runat="server"Text="Cập nhật Trạng thái"
CssClass="btn btn-success btn-sm" OnClick="btncapnhat_Click" />
<asp Label: ID="Label1"runat="server"Text="CHUYÊN MỤC:
"></asp:Label>
<asp DropDownList: ID="dropchuyenmuc"runat="server"
AutoPostBack="True"
43
DataSourceID="chuyenmucs"DataTextField="name"
DataValueField="id">
</asp:DropDownList>
<asp SqlDataSource: ID="chuyenmucs"runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [CatalogNews] ORDER BY [orderby]"></asp:SqlDataSource>
<asp Button: ID="btnxems"runat="server"Text="Hiện theo loại"
CssClass="btn btn-primary btn-sm" OnClick="btnxems_Click" />
</div>
<asp:GridView ID="gvDetails" runat="server" AllowPaging="True"
AutoGenerateColumns="False"DataKeyNames="id"
HeaderStyle-BackColor="#61A6F8"HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="White"
OnPageIndexChanging="gvDetails_PageIndexChanging"
OnRowCancelingEdit="gvDetails_RowCancelingEdit"
OnRowCommand="gvDetails_RowCommand"
OnRowDataBound="gvDetails_RowDataBound"
OnRowDeleting="gvDetails_RowDeleting"
OnRowEditing="gvDetails_RowEditing" ShowFooter="True"
Width="100%"PageSize="30">
<Columns>
<asp TemplateField: HeaderText="STT">
<EditItemTemplate>
<asp TextBox: ID="txtma"runat="server"Text='<% Eval("id"# )
%>' />
</EditItemTemplate>
<ItemTemplate>
<asp Label: ID="lblma"runat="server"Text='<% Eval("id"# ) %> ' />
</ItemTemplate>
</asp:TemplateField>
<asp TemplateField: HeaderText="Tên ">
<ItemTemplate>
<% Eval(# "ten") %>
</ItemTemplate>
</asp:TemplateField>
<asp TemplateField: HeaderText="Chuyên mục">
<ItemTemplate>
<asp Label: ID="lblchuye"runat="server"Text='<%# Eval("tenchuyenmuc") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp TemplateField: HeaderText="Người đăng">
<ItemTemplate>
<asp Label: ID="lblchuyrc"runat="server"Text='<%# Eval("tenhienthi") %> ' />
</ItemTemplate>
</asp:TemplateField>
<asp TemplateField: HeaderText="Ngày đăng">
<ItemTemplate>
<asp Label: ID="lblngay"runat="server"Text='<%# Eval("ngaytao") %> ' />
</ItemTemplate>
</asp:TemplateField>
<asp TemplateField: HeaderText="Lượt xem">
<ItemTemplate>
<asp Label: ID="lblngaynhan"runat="server"Text='<%# Eval("luotxem") %> 'ForeColor="#6666FF" />
</ItemTemplate>
</asp:TemplateField
45
<asp TemplateField: HeaderText="Tin nổi bật">
<ItemTemplate>
<asp Label: ID="vcxvtrangthai"runat="server"
Text='<%# Check(Eval("noibat")) %> 'ForeColor="Red" />
</ItemTemplate>
</asp:TemplateField>
<asp TemplateField: HeaderText="Trạng thái">
<ItemTemplate>
<asp Label: ID="ttrangtais"runat="server"
Text='<%# checktrangthai(Eval("trangthai")) %> ' ForeColor="Red" />
</ItemTemplate>
</asp:TemplateField>
<asp TemplateField: HeaderText="Lựa chọn">
<HeaderTemplate>
<asp CheckBox: ID="chkAll"runat="server"AutoPostBack="True"
OnCheckedChanged="chkAll_CheckedChanged"Text="ALL" />
</HeaderTemplate>
<ItemTemplate>
<asp CheckBox: ID="chkDel"runat="server" />
</ItemTemplate>
<HeaderStyle/>
<ItemStyle/>
</asp:TemplateField>
<asp TemplateField: >
<ItemTemplate>
<asp ImageButton: ID="imgbtnEdit"runat="server"
CommandName="Edit" ImageUrl="~/Uploads/edit.png" ToolTip="Xem chi tiết" />
<asp ImageButton: ID="imgbtnxoa"runat="server"
CommandName="xoa" ImageUrl="~/Uploads/delete.png" ToolTip="Xoá" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Không có dữ liệu phf hợp!!!
</EmptyDataTemplate>
<HeaderStyleBackColor="#61A6F8"Font-Bold="True"
ForeColor="White" />
</asp:GridView>
CODE
#region Khởi tạo các biến
DataAccess ac = new DataAccess();
public string chuoiketnoi = DataAccess.chuoi;
private SqlConnection con;
string sqlconnect;
DataTable dt;
int ma;
int maquyen;
#endregion
tintucdto xuly = new tintucdto();
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
47
load_luoi();
} }
public string Check(object obj) {
int b = int.Parse(obj.ToString());
string kq = "";
if (b == 0) {
return kq = "Không";
} else {
if (b == 1) {
return kq = "Nổi bật";
} } return kq;
}
public string checktrangthai(object obj) {
int b = int.Parse(obj.ToString());
string kq = "";
if (b == 0) {
return kq = "Chưa duyệt";
} else {
if (b == 1) {
return kq = "Đã duyệt";
} } return kq;
}
#region CHUC NANG UPDATE _ DELETE TRUC TIEP //phương thức sua nhiều bản ghi
private void updateSelect(string luachon, string thamso) {
try {
//duyệt từng dòng trong gridview
foreach (GridViewRow row in gvDetails.Rows) {
49
//tìm checkbox "chkDel" và kiểm tra trạng thái CheckBox chkDel =
(CheckBox)row.Cells[0].FindControl("chkDel");
if (chkDel.Checked) {
//lấy Id catalog của dòng chọn int id =
int.Parse(gvDetails.DataKeys[row.RowIndex].Value.ToString());
//gọi phương thức xóa catalog SelectCatalog(id, luachon, thamso);
} } }
catch (Exception ex) {
lblresult.Text = ex.Message;
} }
//phương thức xóa dữ liệu trong bảng catalog với tham số id // lua chon la ten truong du lieu
private void SelectCatalog(int id, string luachon, string thamso) {
try
{
using (SqlConnection con = new SqlConnection(chuoiketnoi)) {
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "update tintuc set " + luachon + " = " + thamso + " where id=@id";
cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
cmd.ExecuteNonQuery();
} }
catch (Exception ex) {
lblresult.Text = ex.Message;
} }
protected void chkAll_CheckedChanged(object sender, EventArgs e) {
try {
//tìm checkbox all và lấy trạng thái CheckBox chkAll =
(CheckBox)gvDetails.HeaderRow.Cells[0].FindControl("chkAll");
51
bool status = chkAll.Checked;
//duyệt từng dòng trong gridview để gán trạng thái foreach (GridViewRow row in gvDetails.Rows) {
//tìm checkbox "chkDel" để gán trạng thái CheckBox chkDel =
(CheckBox)row.Cells[0].FindControl("chkDel");
chkDel.Checked = status;
} }
catch (Exception ex) {
lblresult.Text = ex.Message;
} }
//phương thức xóa nhiều bản ghi private void DeleteAll() {
try {
//duyệt từng dòng trong gridview
foreach (GridViewRow row in gvDetails.Rows) {
//tìm checkbox "chkDel" và kiểm tra trạng thái CheckBox chkDel =
(CheckBox)row.Cells[0].FindControl("chkDel");
if (chkDel.Checked) {
//lấy Id catalog của dòng chọn int id =
int.Parse(gvDetails.DataKeys[row.RowIndex].Value.ToString());
//gọi phương thức xóa catalog DeleteCatalog(id);
} } }
catch (Exception ex) {
lblresult.Text = ex.Message;
} }
//phương thức xóa dữ liệu trong bảng catalog với tham số id private void DeleteCatalog(int id)
{ try {
53
using (SqlConnection con = new SqlConnection(chuoiketnoi)) {
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "delete from tintuc where id=@id";
cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
cmd.ExecuteNonQuery();
} }
catch (Exception ex) {
lblresult.Text = ex.Message;
} }
#endregion
#region KICH VAO THEM-SUA-XOA tren GRIDVIEW /// <summary>
/// Nhaan vao nut sua de tai -- truyen tham so sang /// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int macode =
Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
Response.Redirect("~/quantri.aspx?ctl=QuanTri/TinTuc/qltintuc_blogedit&c
=" + macode + "&action=EDIT");
}
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
int macode =
Convert.ToInt32(gvDetails.DataKeys[e.NewEditIndex].Values["id"].ToString ());
//// chuyen sang trang update
Response.Redirect("~/quantri.aspx?ctl=QuanTri/TinTuc/qltintuc_blogedit&c
=" + macode + "&action=EDIT");
}
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// xoa lua chon int fid =
Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["id"].ToString());
DeleteCatalog(fid);