2011-02-07 3 views

답변

0

.

ItemTemplate에 라벨을 containg의의 GridView에 TemplateField을 넣고 선까지 RowDataBound의 핸들러에의 GridView의 이벤트 : 핸들러에서

<asp:GridView runat="server" ID="PeopleGridView" AutoGenerateColumns="false" OnRowDataBound="PeopleGridView_OnDataBound" > 
    <Columns> 
     <asp:TemplateField> 
     <ItemTemplate> 
      <asp:Label runat="server" ID="NameLabel" /> 
     </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

는있는 DataItem 얻고로 캐스팅하여 Person 클래스. Label 컨트롤을 찾아 클래스에서 속성에 텍스트를 설정합니다

protected void PeopleGridView_OnDataBound(object sender, GridViewRowEventArgs e) 
{ 
    // Make sure it's a DataRow - this will fail for HeaderRow, FooterRow etc 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // Get the DataItem and cast it 
     Person currentPerson = (Person) e.Row.DataItem; 
     // Locate the Label and set it's text 
     ((Label) e.Row.FindControl("NameLabel")).Text = currentPerson.firstName + " " + currentPerson.lastName; 
    } 
} 
1

사용 tempate 필드와 평가 방법 :

<asp:GridView runat="server" ID="MyGrid" AutoGenerateColumns="false" 
    DataSourceId="...">  
    <Columns>   
    <asp:TemplateField>   
     <ItemTemplate>   
     <%# Eval("FirstName") %>&nbsp;<%# Eval("LastName") %> 
     </ItemTemplate>   
    </asp:TemplateField>  
    </Columns> 
</asp:GridView> 
관련 문제