日期:2014-05-18  浏览次数:20460 次

GridView中的模板列
我在GridView的一个模板列中加了一个html的button然后在后台订阅了它的Click事件,在那个事件里怎么取得这个button列所在的行号?

------解决方案--------------------
设置你的button的CommandName,然后用RowCommand事件。

用int index = Convert.ToInt32(e.CommandArgument);取行号
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName== "Add ")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = CustomersGridView.Rows[index];

// Create a new ListItem object for the customer in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[2].Text);

// If the customer is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!CustomersListBox.Items.Contains(item))
{
CustomersListBox.Items.Add(item);
}
}
}