Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将值绑定到头模板内数据库中的标签?_C#_Html_Asp.net - Fatal编程技术网

C# 如何将值绑定到头模板内数据库中的标签?

C# 如何将值绑定到头模板内数据库中的标签?,c#,html,asp.net,C#,Html,Asp.net,请给我一些建议,我能做些什么来达到这个结果。 我想通过数据库绑定一个标签(位于标题内),因为员工名称可能会根据需要更改。 我试过划船,但没用 <asp:GridView ID="Gridview2" runat="server" Width="99%" GridLines="Both" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> &

请给我一些建议,我能做些什么来达到这个结果。 我想通过数据库绑定一个标签(位于标题内),因为员工名称可能会根据需要更改。 我试过划船,但没用

<asp:GridView ID="Gridview2" runat="server" Width="99%" GridLines="Both"
    AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>

                <tr class="header1">
                    <th colspan="2">
                        <asp:Label run="server" ID="labeldesignation" Text='<%# Eval("designation") %>'>Designation</asp:Label></th>
                </tr>
                <tr class="header2">
                    <th>Emp_Id</th>
                    <th>Emp_Name</th>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><%# Eval("Emp_Id") %></td>
                    <td><%# Eval("Emp_Name") %></td>

                </tr>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

任命
Emp_Id
Emp_名称

在加载页面的背面进行

设置标签的值

this.labeldesignation.Text = dataset[0].column[0].toString();
我假设您使用DataTable而不是Dataset

添加另一个表以在UI(Gridview)中拉出表的组成部分
例如页眉、页脚、页面等。

您可以使用
HeaderRow
中的
FindControl
访问标签

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the row back to a datarowview
        DataRowView row = e.Row.DataItem as DataRowView;

        //choose from which row you want the data into the header
        if (e.Row.RowIndex == 4)
        {
            Label lbl = ((GridView)sender).HeaderRow.FindControl("labeldesignation") as Label;
            lbl.Text = row["designation"].ToString();
        }
    }
}
PS GridView生成自己的
标记。不要把你自己的放在模板里。检查简单GridView的html,看看它生成了什么html