2012-02-01 19 views
0

をクリックして知っている:は、複数のボタンのイベントを作成し、私はこのコードを持って後ろに、私はコードではasp.net、 でWebアプリケーションを書いていたボタン

foreach(UserDetails _UD in m_TeachersDetailsList)  
{ 
    Button button = new Button();// a Button control 
    button.Text = "click"; 
    button.ID = "SelectedTeacher"; 
    TableCell tableCell = new TableCell();// a Cell control 
    tableCell.Controls.Add(button); 
    TableRow tableRow = new TableRow(); 
    tableRow.Cells.Add(tableCell); // a table row 
    TableSearchResult.Rows.Add(tableRow); // a table that had been created in the aspx 
} 

にはどうすればイベントその作ることができますボタンをクリックすると、関数 に行き、どのボタンがクリックされたかを知ることができます。ありがとう。

+0

を最初に何かを試してみてください。 –

+2

あなたは同じIDで複数のコントロールを作成しようとしていますが、それはうまくいくとは思わないので、試しましたか? –

+0

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.click.aspxあなたに行く必要があります... – Chris

答えて

1

あなたはOnInitのコントロールを追加する必要があります。この

int id = 0; 
foreach(UserDetails _UD in m_TeachersDetailsList)  
{ 
    Button button = new Button();// a Button control 
    button.Text = "click"; 
    button.ID = "selectedTeacher" + id++; 
    TableCell tableCell = new TableCell();// a Cell control 
    tableCell.Controls.Add(button); 
    TableRow tableRow = new TableRow(); 
    tableRow.Cells.Add(tableCell); // a table row 
    TableSearchResult.Rows.Add(tableRow); // a table that had been created in the aspx 
    button.Click += new EventHandler(this.button_Click); 
} 

、共通のイベントハンドラ

protected void button_Click(object sender, EventArgs e) 
{ 
    //This way you will get the button clicked 
    Button button = (Button)sender; 

} 

重要

を行います。

これはうまくいきます。

+3

-1コメントなし、私にその理由を教えてください - 1 –

+0

最も重要なのは「重要」です:)。 initに配置する必要があります!私は約2時間をなぜ地獄が動作していないかを把握しようと過ごした! Thx – Makis

0

ASPX

asp:Button ID="btnTest" runat="server" onclick="btn_Click" 

分離コード

protected void btn_Click(object sender, EventArgs e) 
{ 
    Button mybutton = (Button)sender; 
    Response.Write(mybutton.ID); 
} 

ちょうどあなたのボタンのそれぞれのためのonclickとしてbtn_Clickを使用しています。次に、 "送信者"を使用して、要求を送信したコントロールを特定します。

1

ボタンのidをデフォルトに設定しないでください。次に、あなたのハンドラがCommandNameCommandArgumentの値をチェックする必要があります

foreach (UserDetails _UD in m_TeachersDetailsList)  
{ 
    Button button = new Button();// a Button control 
    button.Text = "click"; 
    button.CommandArgument = _UD.UserDetailID.ToString(); // some unique identifier 

    // this is optional, if you need multiple actions for each UserDetail: 
    button.CommandName = "SomeAction"; // optional 

    button.Command += new EventHandler(detailButton_Handler); 

    // ...etc... 
} 

を:代わりに、ボタンのCommandArgumentプロパティを設定し

public void detailButton_Handler(object sender, EventArgs e) 
{ 
    string DetailID = e.CommandArgument.ToString(); 
    switch (e.CommandName.ToString()) 
    { 
     case "SomeAction": 
     /// Now you know which Detail they clicked on 
      break; 

     case "OtherAction": 
      break; 
    } 
} 
+0

+1、 'CommandArgument'はもっと良い解決策です(また、10K = Pでおめでとう) – jadarnel27

+1

@ jadamel27:ありがとう!! Gamification ftw! – egrunin

関連する問題