2012-06-17 66 views
1

在Web應用程序中,我試圖綁定gridivew的頭模板,但我無法將數據綁定到gridview頭。將數據綁定到Gridview頁眉模板?在Asp.net

<asp:GridView ID ="grdInner" runat ="server" AutoGenerateColumns ="false" > 
     <Columns > 
      <asp:TemplateField > 
      <HeaderTemplate > 
      <asp:Label ID="lblHeader" runat="server" Text='<%# Eval("title") %>'></asp:Label>  
       </HeaderTemplate> 
       <ItemTemplate > 
       <asp:Label ID ="lblDesc" runat ="server" Text ='<%# Eval("description") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 
+1

你不能將數據綁定到頁眉,因爲只有一個。您只能將數據綁定到重複項目。 –

回答

4

下面的代碼實現同樣的事情

<asp:GridView runat="server" ID="gridView" onrowdatabound="gridView_OnRowDataBound" AutoGenerateColumns="false"> 
<columns> 
    <asp:TemplateField> 
     <HeaderTemplate><asp:Label runat="server" ID="lblHeader"></asp:Label></HeaderTemplate> 
    </asp:TemplateField> 
</columns> 
</asp:GridView> 

您可以設置標籤文本OnRowDataBound事件

protected void gridView_OnRowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.Header) 
      ((Label)e.Row.FindControl("lblHeader") as Label).Text = "Your Data here"; 
    }