Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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/1/asp.net/30.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# repeater:从数据库检索列标题_C#_Asp.net - Fatal编程技术网

C# repeater:从数据库检索列标题

C# repeater:从数据库检索列标题,c#,asp.net,C#,Asp.net,客户: 我使用中继器从数据库中检索数据 我的问题是:如何从数据库中检索列标题,而不仅仅是数据本身? 标题不是作为中继器的项进行数据绑定的(有关详细信息,请参阅此项及其答案)。但是您可以在中继器的itemscreated事件中调整标题。首先,您需要在标题中放置一些文本控件,并为ItemCreated事件添加一个处理程序: try { using (var conn = new SqlConnection(_connectionString)) {

客户:

我使用中继器从数据库中检索数据

我的问题是:如何从数据库中检索列标题,而不仅仅是数据本身?
标题不是作为中继器的项进行数据绑定的(有关详细信息,请参阅此项及其答案)。但是您可以在中继器的itemscreated事件中调整标题。首先,您需要在标题中放置一些文本控件,并为ItemCreated事件添加一个处理程序:

   try
   {
       using (var conn = new SqlConnection(_connectionString))
       {
           using (var cmd = new SqlCommand("spFilterByContinent", conn))
           {
               using (var adapter = new SqlDataAdapter(cmd))
               {
                   cmd.CommandType = CommandType.StoredProcedure;
                   adapter.Fill(table);
               }
           }
       }
       Repeater1.DataSource = table;
       Repeater1.DataBind();
   }
   catch (Exception exception)
   {
       //TODO: write excepetion 
   }
}

public void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        string header1 = string.Empty;
        string header2 = string.Empty;
        string header3 = string.Empty;
        string header4 = string.Empty;
        // Retrieve headers from database and assign to variables
        SetHeaderValue(e.Item, "litHeader1", header1);
        SetHeaderValue(e.Item, "litHeader2", header2);
        SetHeaderValue(e.Item, "litHeader3", header3);
        SetHeaderValue(e.Item, "litHeader4", header4);
    }
}

private void SetHeaderValue(RepeaterItem item, string litId, string headerText)
{
    var lit = item.FindControl(litId) as Literal;
    if (lit != null)
        lit.Text = headerText;
}
}

你使用VB.NET还是C?没关系,C#@user2487126-没关系。不起作用,可能是因为中继器还没有数据源?我将在您添加的问题中添加服务器代码。@user2487126:是的,当中继器绑定到数据时,会引发ItemDataBound。因此,您需要设置中继器的
数据源
并调用
DataBind()
。谢谢,您能告诉我将这些数据源和绑定命令放在哪里吗?@user2487126:在您的代码示例中,您已经设置了
数据源
并调用
DataBind()
。但是在标记中,缺少
ItemDataBound
事件的注册(
OnItemDataBound=“Repeater1\u ItemDataBound”
)。请验证这一点,并在调试器中检查是否调用了事件处理程序。@user2487126:您不必这样做。为避免误解:首先加载表,分配
DataSource
并调用
DataBind()
。您的服务器代码看起来正确,但缺少aspx中事件处理程序的注册:
   try
   {
       using (var conn = new SqlConnection(_connectionString))
       {
           using (var cmd = new SqlCommand("spFilterByContinent", conn))
           {
               using (var adapter = new SqlDataAdapter(cmd))
               {
                   cmd.CommandType = CommandType.StoredProcedure;
                   adapter.Fill(table);
               }
           }
       }
       Repeater1.DataSource = table;
       Repeater1.DataBind();
   }
   catch (Exception exception)
   {
       //TODO: write excepetion 
   }
}

public void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        string header1 = string.Empty;
        string header2 = string.Empty;
        string header3 = string.Empty;
        string header4 = string.Empty;
        // Retrieve headers from database and assign to variables
        SetHeaderValue(e.Item, "litHeader1", header1);
        SetHeaderValue(e.Item, "litHeader2", header2);
        SetHeaderValue(e.Item, "litHeader3", header3);
        SetHeaderValue(e.Item, "litHeader4", header4);
    }
}

private void SetHeaderValue(RepeaterItem item, string litId, string headerText)
{
    var lit = item.FindControl(litId) as Literal;
    if (lit != null)
        lit.Text = headerText;
}
}
<asp:Repeater ID="Repeater1" runat="server" OnItemCreated="Repeater1_ItemCreated">
    <HeaderTemplate>
        <table id="Table_car">
            <tr>
                <th><asp:Literal ID="litHeader1" runat="server" /></th>
                <th><asp:Literal ID="litHeader2" runat="server" /></th>
                <th><asp:Literal ID="litHeader3" runat="server" /></th>
                <th><asp:Literal ID="litHeader4" runat="server" /></th>
            </tr>
        </HeaderTemplate>
    </HeaderTemplate>
    <!-- ... -->
</asp:Repeater>
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
         var tbl = (DataTable)((Repeater)sender).DataSource;
         SetHeaderValue(e.Item, "litHeader1", tbl, 0);
         SetHeaderValue(e.Item, "litHeader2", tbl, 1);
         SetHeaderValue(e.Item, "litHeader3", tbl, 2);
         SetHeaderValue(e.Item, "litHeader4", tbl, 3);
    }
}

private void SetHeaderValue(RepeaterItem item, string litId, DataTable tbl, int colIndex)
{
    var lit = item.FindControl(litId) as Literal;
    if (lit != null)
    {
        string headerText = 
            tbl.Columns.Count > colIndex ? tbl.Columns[colIndex].ColumnName : "Not set";
        lit.Text = headerText;
    }
}