2016年4月22日 星期五

取得GridView中某列的Index

設計一個GridView,含有按鈕欄位與資料欄位


當按下Button後,將資料顯示在TextBox中




程式範例


//Default.aspx 前端程式
<asp:GridView ID="GridView1" runat="server" 
  onrowcommand="GridView_RowCommand" 
  onrowdatabound="GridView_RowDataBound">
  <Columns>
    <asp:TemplateField HeaderText="Button1" SortExpression="Button">
      <ItemTemplate>
        <asp:Button ID="Button" runat="server" Text="加入"
          CommandName="ButtonAdd"
          CommandArgument='<%# Container.DataItemIndex  %>'/>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>

</asp:GridView>


//Default.aspx.cs 後端程式
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "ButtonAdd")
  {
    //取得按下按鈕的那列的索引
    int index = Convert.ToInt32(e.CommandArgument);
    TextBox1.Text = GridView1.Rows[index].Cells[1].Text;
    TextBox2.Text = GridView1.Rows[index].Cells[2].Text;
    TextBox3.Text = GridView1.Rows[index].Cells[3].Text;
  }
}



參考資料

1 則留言:

  1. Playtech and Binance - KADANG PICTAR
    Playtech and Binance are two of the leading kadangpintar casino หารายได้เสริม operators 인카지노 in the world, with Binance's position as the leading technology company in the

    回覆刪除