我有一个搜索功能,可以在OleDb数据库中查找特定用户。对于每个用户,创建一个表中包含他的名字的行,boolean表示他是否是管理员和删除按钮。删除按钮未按预期工作,它不会执行链接到它的方法。我的代码位于Search_Click事件中,该事件在管理员单击搜索按钮以搜索特定用户后立即执行。 我尝试从我的代码中放置一个预定义的按钮,但是在Page_Load中,它按预期工作。如何从Search_Click中获取按钮? 基本上,根据用户搜索文本动态创建LinkButtons并单击搜索按钮(Search_Click)后,我需要找到一种在页面加载事件上注册click事件的方法,我不知道如何做到这一点因为链接按钮必须在搜索按钮的单击事件中创建,但它们也必须通过页面加载进行注册。
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);
}
}
答案 0 :(得分:2)
动态控件的事件处理程序需要在public List<Tuple<string, string, int>> SetNumAttrb(Dictionary<string, string[]> dic)
{
List<Tuple<string, string, int>> attNumNom = new List<Tuple<string, string, int>>();
int ctrl = 1;
foreach (KeyValuePair<string, string[]> kvp in dic)
{
for (int i = 0; i < kvp.Value.Length; i++)
{
attNumNom.Add(Tuple.Create<string, string, int>(kvp.Key, dic[kvp.Key][i], ctrl));
ctrl++;
}
}
return attNumNom;
}
/ Page_Load
个事件期间注册才能触发(see this link)。
因此,您必须在page_load或init中添加Delete按钮及其事件处理程序。在Page_Init事件中找到导致回发的控件的ID,see this
更新:
好的,我添加此代码以证明它可以毫无问题地完成。
要查看其工作原理:创建一个新的Page_Init
并粘贴以下HTML&amp; Code-Behinds into it。然后运行应用并在Web Form
中输入一些名称(例如:Search TextBox
或John
),然后按Ali
过滤结果。
Search Button
这是获取public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string searchName = "";
if (SearchTextBox.Text != "") //if (this.GetPostBackControlUniqueID() == Search.UniqueID) //Search button is clicked!
{
searchName = SearchTextBox.Text;
}
addTable(searchName);
}
void addTable(string searchName)
{
string[] data = new[] {
"John, false, usrJohn"
,"Alex, false, usrAlex"
,"Ali, true, usrAli"
};
//using (OleDbDataReader reader = cmd.ExecuteReader())
//{
Table table = new Table(); table.CellPadding = 10;
TableRow row;
TableCell cell;
Label label;
foreach(string dataItem in data) //while (reader.Read())
{
string[] reader = dataItem.Split(',');
if (reader[0].IndexOf(searchName, StringComparison.OrdinalIgnoreCase) < 0) continue; //search not found (in real app, you use sql where clause for this, but this is just for test)
row = new TableRow();
cell = new TableCell();
label = new Label();
label.Text = reader[0].Trim(); //reader.GetString(0);
cell.Controls.Add(label);
cell.BorderStyle = BorderStyle.Solid;
row.Cells.Add(cell);
cell = new TableCell();
label = new Label();
label.Text = reader[1].Trim(); //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";
string uName = reader[2].Trim();
button.ID = uName; //(string)reader["uName"];
button.CommandName = uName; //(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);
//} //end using OleDbDataReader reader
}
protected void Search_Click(object sender, EventArgs e)
{
//addTable(SearchTextBox.Text); //already done in Page_Load
}
protected void DeleteUser(object sender, EventArgs e)
{
LinkButton button = (LinkButton)sender;
//using (OleDbCommand cmd = new OleDbCommand("DELETE * FROM ArcadeDatabase WHERE uName ='" + button.CommandName + "';", con))
string sql = "DELETE * FROM ArcadeDatabase WHERE uName ='" + button.CommandName + "';";
//execute the sql ... (in real app)
TableHolder.Controls.Add(new LiteralControl("The sql was: " + sql));
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString("HH:mm:ss");
}
} //end class
UniqueID
控件的辅助扩展方法:
PostBack