私のOleDbデータベースで特定のユーザーを検索する検索機能があります。ユーザーごとに、テーブル内の行が作成されます。テーブルの行には、自分の名前、管理者かどうかを示すブール値、削除ボタンが含まれます。削除ボタンは期待通りに機能していません。リンクされているメソッドは実行されません。私のコードはSearch_Clickイベントにあり、管理者が検索ボタンをクリックして特定のユーザーを検索した直後に実行されます。 私のコードから、代わりにPage_Loadにあらかじめ定義されたボタンを配置しようとしましたが、期待通りに機能します。 Search_Clickからボタンを操作するにはどうしたらいいですか? 基本的には、ユーザーの検索テキストに従ってLinkButtonを動的に作成し、検索ボタン(Search_Click)をクリックした後、クリックイベントをページロードイベントに登録する方法を見つける必要があります。リンクボタンには検索ボタンのクリックイベントで作成する必要がありますが、ページロードによって登録する必要があります。ASP.NET(C#)の動的ボタンに関する問題
using (OleDbDataReader reader = cmd.ExecuteReader())
{
Table table = new Table();
TableRow row = new TableRow();
TableCell cell = new TableCell();
Label label = new Label();
while (reader.Read())
{
row = new TableRow();
cell = new TableCell();
label = new Label();
label.Text = reader.GetString(0);
cell.Controls.Add(label);
cell.BorderStyle = BorderStyle.Solid;
row.Cells.Add(cell);
cell = new TableCell();
label = new Label();
label.Text = reader.GetBoolean(1).ToString();
cell.Controls.Add(label);
cell.BorderStyle = BorderStyle.Solid;
row.Cells.Add(cell);
cell = new TableCell();
LinkButton button = new LinkButton();
button.Text = "Delete";
button.ID = (string) reader["uName"];
button.CommandName = (string)reader["uName"];
button.Click += new EventHandler(DeleteUser);
cell.Controls.Add(button);
cell.BorderStyle = BorderStyle.Solid;
row.Cells.Add(cell);
table.Rows.Add(row);
}
table.Style.Add(HtmlTextWriterStyle.MarginLeft, "auto");
table.Style.Add(HtmlTextWriterStyle.MarginRight, "auto");
TableHolder.Controls.Add(table);
}
は、deleteuser:
protected void DeleteUser(object sender, EventArgs e)
{
try
{
LinkButton button = (LinkButton)sender;
string path = Server.MapPath(@"App_Data/ArcadeDatabase.accdb");
using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + "; Persist Security Info = False;"))
{
try
{
con.Open();
}
catch(Exception ex)
{
helper.Log("OleDbError", ex.Message);
}
if(con.State == System.Data.ConnectionState.Open)
{
using (OleDbCommand cmd = new OleDbCommand("DELETE * FROM ArcadeDatabase WHERE uName ='" + button.CommandName + "';", con))
{
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
helper.Log("OleDbError", ex.Message);
}
}
}
con.Dispose();
}
path = "";
}
catch (Exception ex)
{
helper.Log("Admin", ex.Message);
}
}
を。少なくとも、DeleteUserイベントのコードを提供する必要があります。 – Sefe
私は自分の質問を編集して、今はDeleteUserコードを含んでいます:)しかし、私が言ったように、私はブレークポイントを置いたし、実行もしません。 – Itay080