我正在尝试做这些:
- 访问我网站的内容并从我的数据库中读取主题
- 在不使用网格视图的情况下动态创建表格并在其中动态生成图像按钮以编辑和删除我的内容。
我成功地为我的一个图像按钮定义了一个回发 URL,并将其发布到具有我的内容 ID 的新页面以对其进行编辑,但我未能定义一个事件并触发它以删除我的内容而无需转到新页面。
这是我的代码:
var cn = new SqlConnection();
var cmd = new SqlCommand();
int i=0;
try
{
using (cn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnString"].ToString()))
{
cn.Open();
using (cmd = new SqlCommand("ContentArchiveCategory", cn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CatID", txtCategories.SelectedValue);
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
i++;
if (i % 2 != 0)
{
var tr = new TableRow();
tr.CssClass = "ADACLTOdd".ToString();
var no = new TableCell();
no.Text = i.ToString();
no.CssClass = "ADACLTNoColumn".ToString();
tr.Cells.Add(no);
var date = new TableCell();
date.Text = dr["DateCreated"].ToString();
date.CssClass = "ADACLTDateColumn".ToString();
tr.Cells.Add(date);
var title = new TableCell();
title.Text = dr["Title"].ToString();
title.CssClass = "ADACLTTitleColumn".ToString();
tr.Cells.Add(title);
var edit = new TableCell();
edit.CssClass = "ADACLTEditColumn".ToString();
var ice = new ImageButton();
ice.ImageUrl = "~/Images/ICSEdit.png".ToString();
ice.PostBackUrl = "ContentEdit.aspx?id=" + dr["ContentID"].ToString();
ice.ToolTip = "ویرایش".ToString();
edit.Controls.Add(ice);
tr.Cells.Add(edit);
var delete = new TableCell();
delete.CssClass = "ADACLTDeleteColumn".ToString();
var icd = new ImageButton();
icd.ImageUrl = "~/Images/ICSDelete.png".ToString();
icd.ToolTip = "پاک".ToString();
icd.ID = dr["ContentID"].ToString();
icd.Click += new ImageClickEventHandler(icd_Click);
delete.Controls.Add(icd);
tr.Cells.Add(delete);
ContentListTable.Rows.Add(tr);
}
else
{
var tr = new TableRow();
tr.CssClass = "ADACLTEven".ToString();
var no = new TableCell();
no.Text = i.ToString();
no.CssClass = "ADACLTNoColumn".ToString();
tr.Cells.Add(no);
var date = new TableCell();
date.Text = dr["DateCreated"].ToString();
date.CssClass = "ADACLTDateColumn".ToString();
tr.Cells.Add(date);
var title = new TableCell();
title.Text = dr["Title"].ToString();
title.CssClass = "ADACLTTitleColumn".ToString();
tr.Cells.Add(title);
var edit = new TableCell();
edit.CssClass = "ADACLTEditColumn".ToString();
var ice = new ImageButton();
ice.ImageUrl = "~/Images/ICSEdit.png".ToString();
ice.PostBackUrl = "ContentEdit.aspx?id=" + dr["ContentID"].ToString();
ice.ToolTip = "ویرایش".ToString();
edit.Controls.Add(ice);
tr.Cells.Add(edit);
var delete = new TableCell();
delete.CssClass = "ADACLTDeleteColumn".ToString();
var icd = new ImageButton();
icd.ImageUrl = "~/Images/ICSDelete.png".ToString();
icd.ToolTip = "پاک".ToString();
icd.ID = dr["ContentID"].ToString();
icd.Click += icd_Click;
delete.Controls.Add(icd);
tr.Cells.Add(delete);
ContentListTable.Rows.Add(tr);
}
ContentListTable.Visible = true;
}
}
}
}
catch
{
CatMessage.Visible = true;
CatMessage.BorderColor = System.Drawing.ColorTranslator.FromHtml("#930000");
CatMessage.BackColor = System.Drawing.ColorTranslator.FromHtml("#F4B7B7");
CatMsg1.Text = string.Format("{0} عزیز! متاسفانه در برقراری ارتباط با سرور مشکلی پیش آمده، لطفاً این مورد را به مدیریت سایت گزارش کنید", Session["FirstName"]);
}
}
}
protected void icd_Click(object sender, EventArgs e)
{
ImageButton icd = sender as ImageButton;
CatMessage.Visible = true;
CatMessage.BorderColor = System.Drawing.ColorTranslator.FromHtml("#930000");
CatMessage.BackColor = System.Drawing.ColorTranslator.FromHtml("#F4B7B7");
CatMsg1.Text = string.Format("{0}",icd.ID);
}
我尝试了两种方法 icd.Click += icd_Click;
并 icd.Click += new ImageClickEventHandler(icd_Click);
触发了我的活动,但它根本不起作用。