에 대한 이벤트 핸들러를 추가 할 수 있지만, 그들은 나를 도와 것 같지 않았다
어떻게 동적으로 생성 된 버튼
- How to create an onClick eventHandler for a dynamically created button
- asp.net dynamically button with event handler
- asp:Button Click event not being fired
나는 C#을 aspx 페이지를 만드는거야. 이 페이지는 많은 데이터를 가져 와서 테이블을 만듭니다. 테이블의 열 중 하나에는 버튼의 작업이 테이블의 데이터에 의존하기 때문에 데이터를 작성할 때 동적으로 생성되는 버튼이 포함됩니다. 나는 일인가하고있어 어디
Default.aspx를
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="tblData" runat="server"></asp:Table>
</div>
</form>
</body>
Defafult.aspx.cs는
protected void Page_Load(object sender, EventArgs e)
{
Build_Table();
}
protected void Build_Table()
{
//create table header row and cells
TableHeaderRow hr_header = new TableHeaderRow();
TableHeaderCell hc_cell = new TableHeaderCell();
hc_cell.Text = "This column contains a button";
hr_header.Cells.Add(hc_cell);
tblData.Rows.Add(hr_header);
//create the cell to contain our button
TableRow row = new TableRow();
TableCell cell_with_button = new TableCell();
//create the button
Button btn1 = new Button();
btn1.Click += new EventHandler(this.btn1_Click);
//add button to cell, cell to row, and row to table
cell_with_button.Controls.Add(btn1);
row.Cells.Add(cell_with_button);
tblData.Rows.Add(row);
}
protected void btn1_Click(Object sender, EventArgs e)
{
//do amazing stuff
}
여기. Page_Load 메서드로 옮겨야하기 때문에 EventHandler가 실행되지 않는다는 것을 이해합니다. 그러나 btn1 만들기 및 EventHandler를 Page_Load로 이동하면 더 이상 Build_Table에서 액세스 할 수 없습니다!
내가 볼 수있는 모든 코드 예제에는 ASPX 페이지에 정적으로 btn1이 추가되었거나 Page_Load에서 동적으로 생성 된 코드 예제가 있습니다. 내가 성취하려는 일을하는 가장 좋은 방법은 무엇입니까?
Button btn1 = new Button();
btn1.ID = "btnMyButton";
btn1.Click += new EventHandler(this.btn1_Click);
모든 버튼이 고유 한 ID를 가지고 있는지 확인하십시오 : 당신이 이벤트를 바인딩하기 전에
코드를 실행했는데 작동하지만 이벤트 핸들러는 문제가 없습니다. 귀하의 처리기가 해고되었다는 것을 확인해 주셨지만 "놀라운 것들"을하는 동안 뭔가 나쁜 일이 벌어지고 있습니까? –