GridView.RowCreated 이벤트
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
GridView 컨트롤에서 행을 만들면 이 이벤트가 발생합니다.
public:
event System::Web::UI::WebControls::GridViewRowEventHandler ^ RowCreated;
public event System.Web.UI.WebControls.GridViewRowEventHandler RowCreated;
member this.RowCreated : System.Web.UI.WebControls.GridViewRowEventHandler
Public Custom Event RowCreated As GridViewRowEventHandler
이벤트 유형
예제
다음 예제에서는 사용 하는 RowCreated 방법에 설명 합니다 행에 포함 된 컨트롤의 속성에 CommandArgument 생성 되는 행의 인덱스를 LinkButton 저장 하는 이벤트입니다. 이렇게 하면 사용자가 단추를 클릭할 때 컨트롤이 포함된 LinkButton 행의 인덱스를 확인할 수 있습니다.
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void ProductsGridView_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 = ProductsGridView.Rows[index];
// Create a new ListItem object for the product in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[1].Text);
// If the product is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!ProductsListBox.Items.Contains(item))
{
ProductsListBox.Items.Add(item);
}
}
}
void ProductsGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
// The GridViewCommandEventArgs class does not contain a
// property that indicates which row's command button was
// clicked. To identify which row's button was clicked, use
// the button's CommandArgument property by setting it to the
// row's index.
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
LinkButton addButton = (LinkButton)e.Row.FindControl("AddButton");
// Set the LinkButton's CommandArgument property with the
// row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString();
}
}
</script>
<html>
<head id="Head1" runat="server">
<title>GridView RowCreated Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowCreated Example</h3>
<table width="100%">
<tr>
<td style="width:50%">
<asp:GridView ID="ProductsGridView"
DataSourceID="ProductsDataSource"
AllowPaging="true"
AutoGenerateColumns="false"
OnRowCommand="ProductsGridView_RowCommand"
OnRowCreated="ProductsGridView_RowCreated"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server"
ID="AddButton"
CommandName="Add"
Text="Add" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name"
HeaderText="Product Name"/>
<asp:BoundField DataField="ProductNumber"
HeaderText="Product Number"/>
</Columns>
</asp:GridView>
</td>
<td style="vertical-align:top; width:50%">
Products: <br/>
<asp:listbox id="ProductsListBox"
runat="server" Height="200px" Width="200px"/>
</td>
</tr>
</table>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="ProductsDataSource"
selectcommand="Select [Name], [ProductNumber] From Production.Product"
connectionstring="<%$ ConnectionStrings:AdventureWorks_DataConnectionString%>"
runat="server"/>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub ProductsGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
' If multiple buttons are used in a GridView control, use the
' CommandName property to determine which button was clicked.
If e.CommandName = "Add" Then
' Convert the row index stored in the CommandArgument
' property to an Integer.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button clicked
' by the user from the Rows collection.
Dim row As GridViewRow = ProductsGridView.Rows(index)
' Create a new ListItem object for the product in the row.
Dim item As New ListItem()
item.Text = Server.HtmlDecode(row.Cells(1).Text)
' If the product is not already in the ListBox, add the ListItem
' object to the Items collection of the ListBox control.
If Not ProductsListBox.Items.Contains(item) Then
ProductsListBox.Items.Add(item)
End If
End If
End Sub
Sub ProductsGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
' The GridViewCommandEventArgs class does not contain a
' property that indicates which row's command button was
' clicked. To identify which row's button was clicked, use
' the button's CommandArgument property by setting it to the
' row's index.
If e.Row.RowType = DataControlRowType.DataRow Then
' Retrieve the LinkButton control from the first column.
Dim addButton As LinkButton = CType(e.Row.FindControl("AddButton"), LinkButton)
' Set the LinkButton's CommandArgument property with the
' row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString()
End If
End Sub
</script>
<html>
<head id="Head1" runat="server">
<title>GridView RowCreated Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowCreated Example</h3>
<table width="100%">
<tr>
<td style="width:50%">
<asp:GridView ID="ProductsGridView"
DataSourceID="ProductsDataSource"
AllowPaging="true"
AutoGenerateColumns="false"
OnRowCommand="ProductsGridView_RowCommand"
OnRowCreated="ProductsGridView_RowCreated"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server"
ID="AddButton"
CommandName="Add"
Text="Add" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name"
HeaderText="Product Name"/>
<asp:BoundField DataField="ProductNumber"
HeaderText="Product Number"/>
</Columns>
</asp:GridView>
</td>
<td style="vertical-align:top; width:50%">
Products: <br/>
<asp:listbox id="ProductsListBox"
runat="server" Height="200px" Width="200px"/>
</td>
</tr>
</table>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="ProductsDataSource"
selectcommand="Select [Name], [ProductNumber] From Production.Product"
connectionstring="<%$ ConnectionStrings:AdventureWorks_DataConnectionString%>"
runat="server"/>
</form>
</body>
</html>
설명
전에 GridView 컨트롤을 렌더링을 GridViewRow 컨트롤의 각 행에 대 한 개체를 만들어야 합니다. 이벤트는 RowCreated 컨트롤의 각 행을 GridView 만들 때 발생합니다. 이렇게 하면 이 이벤트가 발생할 때마다 행에 사용자 지정 콘텐츠를 추가하는 등 사용자 지정 루틴을 수행하는 이벤트 처리 메서드를 제공할 수 있습니다.
GridViewRowEventArgs 개체는 생성되는 행의 속성에 액세스할 수 있는 이벤트 처리 메서드에 전달됩니다. 행의 특정 셀에 액세스하려면 개체의 CellsGridViewRowEventArgs 속성을 사용합니다. 속성을 사용하여 만들 행 유형(머리글 행, 데이터 행 등)을 RowType 확인할 수 있습니다.
이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.
적용 대상
추가 정보
.NET