我有一个asp.net应用程序,其中有一个用户可以发布消息的页面。这些消息是从数据库中检索出来的,正好在我的页面加载代码中的正文中存储它们。对于每条消息,生成一个新的div,其中有另一个div用于消息,另一个用于用户名。为每个帖子制作一个按钮,问题是我希望按钮从div中删除一个帖子(因此数据库中的消息)。但我无法弄清楚如何做到这一点,因为一切都是在代码背后生成的。也许FindControl()可以帮忙吗?
String connString = System.Configuration.ConfigurationManager.ConnectionStrings["WebAppConnString"].ToString();
conn = new MySqlConnection(connString);
conn.Open();
queryStr = "SELECT berichtID ,bericht, user FROM tblforum ORDER BY berichtID DESC";
cmd = new MySqlCommand(queryStr, conn);
MySqlDataReader Reader = cmd.ExecuteReader();
try {
while (Reader.Read())
{
ImageButton Button = new ImageButton();
Button.ImageUrl = "https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-gear-512.png";
HtmlGenericControl User = new HtmlGenericControl("div");
HtmlGenericControl Div = new HtmlGenericControl("div");
HtmlGenericControl Username = new HtmlGenericControl("p");
HtmlGenericControl Message = new HtmlGenericControl("p");
Button.Style.Add("max-height", "inherit");
Button.Style.Add("width", "auto");
Button.Style.Add("float", "right");
Username.InnerText = Reader["user"].ToString() + " said: ";
Div.Style.Add("border-bottom", "6px solid black");
Div.Style.Add("background-color", "#006633");
Message.Style.Add("color", "#d3d3d3");
forumsection.Controls.Add(Div);
Message.InnerText = Reader["bericht"].ToString();
Div.Controls.Add(User);
User.Controls.Add(Button);
User.Controls.Add(Username);
Div.Controls.Add(Message);
}
Reader.Close();
}
谢谢
答案 0 :(得分:0)
试试这个......
protected void Page_Load(object sender, EventArgs e)
{
DisplayData();
}
private void DisplayData()
{
//your code
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["WebAppConnString"].ToString();
conn = new MySqlConnection(connString);
conn.Open();
queryStr = "SELECT berichtID ,bericht, user FROM tblforum ORDER BY berichtID DESC";
cmd = new MySqlCommand(queryStr, conn);
MySqlDataReader Reader = cmd.ExecuteReader();
try
{
while (Reader.Read())
{
ImageButton Button = new ImageButton();
Button.ImageUrl = "https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-gear-512.png";
//**************ADD THIS TO FIRE DELETE*************
Button.Click += Button_Click;
Button.ID = "btnForDBItem_" + Reader.GetOrdinal("berichtID").ToString();
//**************END OF FIRE DELETE*****************
HtmlGenericControl User = new HtmlGenericControl("div");
HtmlGenericControl Div = new HtmlGenericControl("div");
HtmlGenericControl Username = new HtmlGenericControl("p");
HtmlGenericControl Message = new HtmlGenericControl("p");
Button.Style.Add("max-height", "inherit");
Button.Style.Add("width", "auto");
Button.Style.Add("float", "right");
Username.InnerText = Reader["user"].ToString() + " said: ";
Div.Style.Add("border-bottom", "6px solid black");
Div.Style.Add("background-color", "#006633");
Message.Style.Add("color", "#d3d3d3");
forumsection.Controls.Add(Div);
Message.InnerText = Reader["bericht"].ToString();
Div.Controls.Add(User);
User.Controls.Add(Button);
User.Controls.Add(Username);
Div.Controls.Add(Message);
}
Reader.Close();
}
catch(Exception ex)
{
throw ex;
}
}
private void Button_Click(object sender, ImageClickEventArgs e)
{
//add your database code to do the DELETE here
ImageButton btn = (ImageButton)sender;
var IDtoDELETE = btn.ID.Replace("btnForDBItem_","");
//now call DisplayData() to regenerate the page without the deleted item
DisplayData();
}