On your gridview in markup, assign CommandArgument attribute to whichever you want (here I choose the index of the current gridviewrow) inside the your buttons.
<asp:Button ID="lbnView" runat="server" Text="Button" OnClick="btn_Clicked"
CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"></asp:Button>
Or in your code behind, you can create a button like below
protected void GridViewDice_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable diceTable = _gm.GetDice(_gameId);
for (int i = 0; i < GameRules.ColumnsOfDice; i++)
{
if(e.Row.RowIndex > -1)
{
Button btn = new Button();
btn.CommandArgument = diceTable.Rows[e.Row.RowIndex][i].ToString();
btn.Attributes.Add("OnClick", "btn_Clicked");
e.Row.Cells[i].Controls.Add(btn);
}
}
}
then make an event handler like below
protected void btn_Clicked(object sender, EventAgrs e)
{
//get your command argument from the button here
if (sender is Button)
{
try
{
String yourAssignedValue = ((Button)sender).CommandArgument;
}
catch
{
//Check for exception
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…